OSDN Git Service

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