OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / ground_color.rb
index 58fa057..a4c9a66 100644 (file)
-class GroundColor < ActiveRecord::Base
+class GroundColor < Peta::Element
+  load_manifest
   belongs_to :panel
   belongs_to :color
   
   validates :panel_id, :numericality => {:allow_blank => true}
-  validates :color_id, :numericality => true, :existence => {:both => false}
+  validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
+  validates :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
+  validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
+  validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
+  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
-  def supply_default
+  scope :with_panel, -> do
+    includes(:panel)
   end
   
-  def overwrite
+  scope :find_index, -> do
+    with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
   end
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-    elsif au.is_a?(Admin)
-      return true
-    else
-      return false
+  scope :find_private, -> (operators) do 
+    with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
+  end
+  
+  scope :find_by_panel, -> (panel_id) do 
+    find_index.where(panel_id: panel_id).references(:panel)
+  end
+  
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
+  end
+  
+  def self.by_author_list_includes
+    {
+      :panel => {
+        :author => {}
+      }
+    }
+  end
+  
+  def self.has_picture?
+    false
+  end
+  
+  def supply_default
+    self.code ||= 0
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
     end
-    self.panel.publish?
   end
   
-  def self.default_page_size
-    25
+  def overwrite pid
+    self.panel_id = pid
   end
   
-  def self.max_page_size
-    100
+  def div_offset
+    xy ? xy : 0
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  def div_size
+    wh ? wh : 100 - self.div_offset
   end
   
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
+  def div_x
+    if self.orientation == 0
+      0
+    else
+      self.div_offset
+    end
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(GroundColor.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_colors.updated_at desc'})
-    GroundColor.find(:all, opt)
+  def div_y
+    if self.orientation == 0
+      self.div_offset
+    else
+      0
+    end
   end
   
-  def self.list_opt
-    {:include => {:panel => {:author => {}}, :color => {} }}
+  def div_width
+    if self.orientation == 0
+      100
+    else
+      self.div_size
+    end
   end
   
-  def self.list_json_opt
-    {:include => {:panel => {:include => {:author => {}}}, :color => {} }}
+  def div_height 
+    if self.orientation == 0
+      self.div_size
+    else
+      100
+    end
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_colors.updated_at desc'})
-    GroundColor.find(:all, opt)
+  def style spot = nil, opacity = 20
+    r = {
+      'position' => 'absolute', 'z-index' => self.z, 
+      'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
+      'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
+      'background-color' => '#' + format("%06x", self.code)
+    }
+    self.merge_opacity(r, opacity) if spot and spot != self
+    r
   end
   
-  def self.show cid, au
-    opt = {}
-    opt.merge!(GroundColor.show_opt)
-    res = GroundColor.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(au)
-    res
+  def self.public_list_where list
+    'panels.publish > 0'
   end
   
   def self.show_opt
-    {:include => {:panel => {:author => {}}, :color => {} }}
+    {:include => {:panel => {:author => {}} }}
   end
   
-  def self.show_json_opt
-    {:include => {:panel => {:include => {:author => {}}}, :color => {} }}
+  def scenario
+    if caption.blank?
+      ''
+    else
+      '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
+    end
+  end
+  
+  def plain_scenario
+    if caption.blank?
+      ''
+    else
+      self.caption + "\n"
+    end
   end
   
 end