OSDN Git Service

Item Dynamic ClassMethods
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < Peta::Content
2   load_manifest
3   include Peta::Element
4   include ElementInspire
5   belongs_to :panel
6   belongs_to :color
7   
8   validates :panel_id, :numericality => {:allow_blank => true}
9   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
10   validates :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
11   validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
12   validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
13   validates :z, :presence => true, :numericality => {:greater_than => 0}
14   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15   
16   def self.colum_structures
17     @@colum_structures ||= {
18       :code => {
19         :helper => 'panels/color_helper'
20       }
21     }
22   end
23   
24   def self.list_opt_for_panel
25     {
26       :ground_colors => {
27       }
28     }
29   end
30   
31   def self.show_opt_for_panel
32     {
33       :ground_colors => {
34       }
35     }
36   end
37   
38   def self.json_opt_for_panel
39     {
40     }
41   end
42   
43   def self.has_picture?
44     false
45   end
46   
47   def supply_default
48     self.code ||= 0
49     if self.panel
50       self.t = self.panel.new_t 
51       self.z = self.panel.new_z 
52     end
53   end
54   
55   def overwrite pid
56     self.panel_id = pid
57   end
58   
59   def visible? operators
60     return false unless super
61     self.owner_model.visible? operators
62   end
63   
64   def div_offset
65     xy ? xy : 0
66   end
67   
68   def div_size
69     wh ? wh : 100 - self.div_offset
70   end
71   
72   def div_x
73     if self.orientation == 0
74       0
75     else
76       self.div_offset
77     end
78   end
79   
80   def div_y
81     if self.orientation == 0
82       self.div_offset
83     else
84       0
85     end
86   end
87   
88   def div_width
89     if self.orientation == 0
90       100
91     else
92       self.div_size
93     end
94   end
95   
96   def div_height 
97     if self.orientation == 0
98       self.div_size
99     else
100       100
101     end
102   end
103   
104   def opt_div_style spot = nil, opacity = 20
105     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
106     "position: absolute; left:#{self.div_x}%; top:#{self.div_y}%; width:#{self.div_width}%; height:#{self.div_height}%; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
107   end
108   
109   def boost
110   end
111   
112   def tag_element_type
113     'ground_color'
114   end
115   
116   def self.list_where
117     'panels.publish > 0'
118   end
119   
120   def self.list_order
121     'ground_colors.updated_at desc'
122   end
123   
124   def self.list_opt
125     {:panel => {:author => {}} }
126   end
127   
128   def self.list_json_opt
129     {:include => {:panel => {:include => {:author => {}}} }}
130   end
131   
132   def self.show_opt
133     {:include => {:panel => {:author => {}} }}
134   end
135   
136   def self.show_json_opt
137     {:include => {:panel => {:include => {:author => {}}} }}
138   end
139   
140   def store operators
141     if self.new_record?
142       self.panel.ground_colors.build(self.attributes)
143     else
144       self.panel.ground_colors.each do |ground_color|
145         next unless ground_color == self
146         attr = self.attributes
147         attr.delete 'id'
148         ground_color.attributes = attr
149         break
150       end
151     end
152     self.panel.store({}, operators)
153   end
154   
155   def remove operators
156     self.panel.remove_element(self, operators)
157   end
158   
159   def scenario
160     if caption.blank?
161       ''
162     else
163       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
164     end
165   end
166   
167   def plain_scenario
168     if caption.blank?
169       ''
170     else
171       self.caption + "\n"
172     end
173   end
174   
175 end