OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < Pettanr::Item
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 :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
10   validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
11   validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
12   validates :z, :presence => true, :numericality => {:greater_than => 0}
13   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14   
15   def self.singular
16     'GroundColor'
17   end
18   
19   def self.plural
20     'GroundColors'
21   end
22   
23   def self.owner_type
24     :author
25   end
26   
27   def self.valid_encode_columns
28     super.merge ['caption']
29   end
30   
31   def self.colum_structures
32     @@colum_structures ||= {
33       :code => {
34         :helper => 'panels/color_helper'
35       }
36     }
37   end
38   
39   def self.list_opt_for_panel
40     {
41       :ground_colors => {
42       }
43     }
44   end
45   
46   def self.show_opt_for_panel
47     {
48       :ground_colors => {
49       }
50     }
51   end
52   
53   def self.json_opt_for_panel
54     {
55     }
56   end
57   
58   def self.has_picture?
59     false
60   end
61   
62   def supply_default
63     self.code ||= 0
64     if self.panel
65       self.t = self.panel.new_t 
66       self.z = self.panel.new_z 
67     end
68   end
69   
70   def overwrite pid
71     self.panel_id = pid
72   end
73   
74   def visible? roles
75     if MagicNumber['run_mode'] == 0
76       return false unless guest_role_check(roles)
77     else
78       return false unless reader_role_check(roles)
79     end
80     return true if self.panel.own?(roles)
81     self.panel.visible? roles
82   end
83   
84   def div_offset
85     xy ? xy : 0
86   end
87   
88   def div_size
89     wh ? wh : 100 - self.div_offset
90   end
91   
92   def div_x
93     if self.orientation == 0
94       0
95     else
96       self.div_offset
97     end
98   end
99   
100   def div_y
101     if self.orientation == 0
102       self.div_offset
103     else
104       0
105     end
106   end
107   
108   def div_width
109     if self.orientation == 0
110       100
111     else
112       self.div_size
113     end
114   end
115   
116   def div_height 
117     if self.orientation == 0
118       self.div_size
119     else
120       100
121     end
122   end
123   
124   def opt_div_style spot = nil, opacity = 20
125     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
126     "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}"
127   end
128   
129   def boost
130   end
131   
132   def tag_element_type
133     'ground_color'
134   end
135   
136   def self.default_page_size
137     25
138   end
139   
140   def self.max_page_size
141     100
142   end
143   
144   def self.page prm = nil
145     page = prm.to_i
146     page = 1 if page < 1
147     page
148   end
149   
150   def self.page_size prm = self.default_page_size
151     page_size = prm.to_i
152     page_size = self.max_page_size if page_size > self.max_page_size
153     page_size = self.default_page_size if page_size < 1
154     page_size
155   end
156   
157   def self.list_where
158     'panels.publish > 0'
159   end
160   
161   def self.mylist_where au
162     ['panels.author_id = ?', au.id]
163   end
164   
165   def self.himlist_where au
166     ['panels.author_id = ? and panels.publish > 0', au.id]
167   end
168   
169   def self.list page = 1, page_size = self.default_page_size
170     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
171   end
172   
173   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
174     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
175   end
176   
177   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
178     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
179   end
180   
181   def self.list_paginate page = 1, page_size = self.default_page_size
182     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
183   end
184   
185   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
186     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
187   end
188   
189   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
190     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
191   end
192   
193   def self.list_by_panel_where panel_id
194     ['ground_colors.panel_id = ?', panel_id]
195   end
196   
197   def self.list_by_panel panel_id, roles, page = 1, page_size = self.default_page_size
198     self.where(self.list_by_panel_where(panel_id)).includes(self.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
199   end
200   
201   def self.list_opt
202     {:panel => {:author => {}} }
203   end
204   
205   def self.list_json_opt
206     {:include => {:panel => {:include => {:author => {}}} }}
207   end
208   
209   def self.show cid, roles
210     opt = {}
211     opt.merge!(GroundColor.show_opt)
212     res = GroundColor.find(cid, opt)
213     raise ActiveRecord::Forbidden unless res.visible?(roles)
214     res
215   end
216   
217   def self.show_opt
218     {:include => {:panel => {:author => {}} }}
219   end
220   
221   def self.show_json_opt
222     {:include => {:panel => {:include => {:author => {}}} }}
223   end
224   
225   def store au
226     if self.new_record?
227       self.panel.ground_colors.build(self.attributes)
228     else
229       self.panel.ground_colors.each do |ground_color|
230         next unless ground_color == self
231         attr = self.attributes
232         attr.delete 'id'
233         ground_color.attributes = attr
234         break
235       end
236     end
237     self.panel.store({}, au)
238   end
239   
240   def remove au
241     self.panel.remove_element(self, au)
242   end
243   
244   def scenario
245     if caption.blank?
246       ''
247     else
248       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
249     end
250   end
251   
252   def plain_scenario
253     if caption.blank?
254       ''
255     else
256       self.caption + "\n"
257     end
258   end
259   
260 end