OSDN Git Service

t#31868:refact element form
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < ActiveRecord::Base
2   include Element
3   include ElementInspire
4   belongs_to :panel
5   belongs_to :color
6   
7   validates :panel_id, :numericality => {:allow_blank => true}
8   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
9   validates :z, :presence => true, :numericality => {:greater_than => 0}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   before_validation :valid_encode
13   
14   def valid_encode
15     ['caption'].each do |a|
16       next if attributes[a] == nil
17       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
18     end
19   end
20   
21   def self.list_opt_for_panel
22     {
23       :ground_colors => {
24       }
25     }
26   end
27   
28   def self.show_opt_for_panel
29     {
30       :ground_colors => {
31       }
32     }
33   end
34   
35   def self.json_opt_for_panel
36     {
37     }
38   end
39   
40   def self.has_picture?
41     false
42   end
43   
44   def supply_default
45     self.code ||= 0
46     if self.panel
47       self.t = self.panel.new_t 
48       self.z = self.panel.new_z 
49     end
50   end
51   
52   def overwrite pid
53     self.panel_id = pid
54   end
55   
56   def visible? roles
57     if MagicNumber['run_mode'] == 0
58       return false unless guest_role_check(roles)
59     else
60       return false unless reader_role_check(roles)
61     end
62     return true if self.panel.own?(roles)
63     self.panel.visible? roles
64   end
65   
66   def opt_div_style spot = nil, opacity = 20
67     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
68     "position: absolute; width:#{self.get_panel.width}px; height:#{self.get_panel.height}px; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
69   end
70   
71   def new_index
72     @new_index
73   end
74   
75   def new_index= v
76     @new_index = v
77   end
78   
79   def new_panel
80     @new_panel
81   end
82   
83   def new_panel= v
84     @new_panel = v
85   end
86   
87   def get_panel
88     self.panel || @new_panel
89   end
90   
91   def boost
92   end
93   
94   def tag_id c = nil
95     'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
96   end
97   
98   def field_tag_id f
99     self.tag_id + f.to_s
100   end
101   
102   def tag_panel_id
103     self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
104   end
105   
106   def tag_element_id
107     self.new_record? ? '0' : self.id.to_s
108   end
109   
110   def tag_element_type
111     'ground_color'
112   end
113   
114   def tag_new_index
115     self.new_index.to_s
116   end
117   
118   def self.default_page_size
119     25
120   end
121   
122   def self.max_page_size
123     100
124   end
125   
126   def self.page prm = nil
127     page = prm.to_i
128     page = 1 if page < 1
129     page
130   end
131   
132   def self.page_size prm = self.default_page_size
133     page_size = prm.to_i
134     page_size = self.max_page_size if page_size > self.max_page_size
135     page_size = self.default_page_size if page_size < 1
136     page_size
137   end
138   
139   def self.list_where
140     'panels.publish > 0'
141   end
142   
143   def self.mylist_where au
144     ['panels.author_id = ?', au.id]
145   end
146   
147   def self.himlist_where au
148     ['panels.author_id = ? and panels.publish > 0', au.id]
149   end
150   
151   def self.list page = 1, page_size = self.default_page_size
152     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
153   end
154   
155   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
156     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
157   end
158   
159   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
160     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
161   end
162   
163   def self.list_paginate page = 1, page_size = self.default_page_size
164     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
165   end
166   
167   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
168     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
169   end
170   
171   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
172     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
173   end
174   
175   def self.list_opt
176     {:panel => {:author => {}} }
177   end
178   
179   def self.list_json_opt
180     {:include => {:panel => {:include => {:author => {}}} }}
181   end
182   
183   def self.show cid, roles
184     opt = {}
185     opt.merge!(GroundColor.show_opt)
186     res = GroundColor.find(cid, opt)
187     raise ActiveRecord::Forbidden unless res.visible?(roles)
188     res
189   end
190   
191   def self.show_opt
192     {:include => {:panel => {:author => {}} }}
193   end
194   
195   def self.show_json_opt
196     {:include => {:panel => {:include => {:author => {}}} }}
197   end
198   
199   def store au
200     if self.new_record?
201       self.panel.ground_colors.build(self.attributes)
202     else
203       self.panel.ground_colors.each do |ground_color|
204         next unless ground_color == self
205         attr = self.attributes
206         attr.delete 'id'
207         ground_color.attributes = attr
208         break
209       end
210     end
211     self.panel.store({}, au)
212   end
213   
214   def remove au
215     self.panel.remove_element(self, au)
216   end
217   
218   def scenario
219     if caption.blank?
220       ''
221     else
222       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
223     end
224   end
225   
226   def plain_scenario
227     if caption.blank?
228       ''
229     else
230       self.caption + "\n"
231     end
232   end
233   
234 end