OSDN Git Service

fix foreign_filter list includes
[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   def self.by_author_list_includes
15     {
16       :panel => {
17         :author => {}
18       }
19     }
20   end
21   
22   def self.list_opt_for_panel
23     {
24       :ground_colors => {
25       }
26     }
27   end
28   
29   def self.show_opt_for_panel
30     {
31       :ground_colors => {
32       }
33     }
34   end
35   
36   def self.json_opt_for_panel
37     {
38     }
39   end
40   
41   def self.has_picture?
42     false
43   end
44   
45   def supply_default
46     self.code ||= 0
47     if self.panel
48       self.t = self.panel.new_t 
49       self.z = self.panel.new_z 
50     end
51   end
52   
53   def overwrite pid
54     self.panel_id = pid
55   end
56   
57   def visible? operators
58     return false unless super
59     self.owner_model.visible? operators
60   end
61   
62   def div_offset
63     xy ? xy : 0
64   end
65   
66   def div_size
67     wh ? wh : 100 - self.div_offset
68   end
69   
70   def div_x
71     if self.orientation == 0
72       0
73     else
74       self.div_offset
75     end
76   end
77   
78   def div_y
79     if self.orientation == 0
80       self.div_offset
81     else
82       0
83     end
84   end
85   
86   def div_width
87     if self.orientation == 0
88       100
89     else
90       self.div_size
91     end
92   end
93   
94   def div_height 
95     if self.orientation == 0
96       self.div_size
97     else
98       100
99     end
100   end
101   
102   def style spot = nil, opacity = 20
103     r = {
104       'position' => 'absolute', 'z-index' => self.z, 
105       'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
106       'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
107       'background-color' => '#' + format("%06x", self.code)
108     }
109     self.merge_opacity(r, opacity) if spot and spot != self
110     r
111   end
112   
113   def self.list_where
114     'panels.publish > 0'
115   end
116   
117   def self.list_order
118     'ground_colors.updated_at desc'
119   end
120   
121   def self.list_opt
122     {:panel => {:author => {}} }
123   end
124   
125   def self.list_json_opt
126     {:include => {:panel => {:include => {:author => {}}} }}
127   end
128   
129   def self.show_opt
130     {:include => {:panel => {:author => {}} }}
131   end
132   
133   def self.show_json_opt
134     {:include => {:panel => {:include => {:author => {}}} }}
135   end
136   
137   def store operators
138     if self.new_record?
139       self.panel.ground_colors.build(self.attributes)
140     else
141       self.panel.ground_colors.each do |ground_color|
142         next unless ground_color == self
143         attr = self.attributes
144         attr.delete 'id'
145         ground_color.attributes = attr
146         break
147       end
148     end
149     self.panel.store({}, operators)
150   end
151   
152   def remove operators
153     self.panel.remove_element(self, operators)
154   end
155   
156   def scenario
157     if caption.blank?
158       ''
159     else
160       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
161     end
162   end
163   
164   def plain_scenario
165     if caption.blank?
166       ''
167     else
168       self.caption + "\n"
169     end
170   end
171   
172 end