OSDN Git Service

merge
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :color
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
8   validates :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
10   validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
11   validates :z, :presence => true, :numericality => {:greater_than => 0}
12   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13   
14   scope :with_panel, -> do
15     includes(:panel)
16   end
17   
18   scope :find_index, -> do
19     with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
20   end
21   
22   scope :find_private, -> (operators) do 
23     with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
24   end
25   
26   scope :find_by_panel, -> (panel_id) do 
27     find_index.where(panel_id: panel_id).references(:panel)
28   end
29   
30   scope :find_by_author, -> (author_id) do 
31     find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
32   end
33   
34   def self.by_author_list_includes
35     {
36       :panel => {
37         :author => {}
38       }
39     }
40   end
41   
42   def self.has_picture?
43     false
44   end
45   
46   def supply_default
47     self.code ||= 0
48     if self.panel
49       self.t = self.panel.new_t 
50       self.z = self.panel.new_z 
51     end
52   end
53   
54   def overwrite pid
55     self.panel_id = pid
56   end
57   
58   def div_offset
59     xy ? xy : 0
60   end
61   
62   def div_size
63     wh ? wh : 100 - self.div_offset
64   end
65   
66   def div_x
67     if self.orientation == 0
68       0
69     else
70       self.div_offset
71     end
72   end
73   
74   def div_y
75     if self.orientation == 0
76       self.div_offset
77     else
78       0
79     end
80   end
81   
82   def div_width
83     if self.orientation == 0
84       100
85     else
86       self.div_size
87     end
88   end
89   
90   def div_height 
91     if self.orientation == 0
92       self.div_size
93     else
94       100
95     end
96   end
97   
98   def style spot = nil, opacity = 20
99     r = {
100       'position' => 'absolute', 'z-index' => self.z, 
101       'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
102       'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
103       'background-color' => '#' + format("%06x", self.code)
104     }
105     self.merge_opacity(r, opacity) if spot and spot != self
106     r
107   end
108   
109   def self.public_list_where list
110     'panels.publish > 0'
111   end
112   
113   def self.show_opt
114     {:include => {:panel => {:author => {}} }}
115   end
116   
117   def scenario
118     if caption.blank?
119       ''
120     else
121       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
122     end
123   end
124   
125   def plain_scenario
126     if caption.blank?
127       ''
128     else
129       self.caption + "\n"
130     end
131   end
132   
133 end