OSDN Git Service

t#31687:fiix remove_element
[pettanr/pettanr.git] / app / models / panel.rb
1 #コマ
2 class Panel < ActiveRecord::Base
3   belongs_to :author
4   belongs_to :resource_picture
5   has_many :stories
6   has_many :panel_pictures, :dependent => :destroy
7   has_many :speech_balloons, :dependent => :destroy
8   has_many :ground_pictures, :dependent => :destroy
9   has_many :ground_colors, :dependent => :destroy
10   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
11   accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
12   accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
13   accepts_nested_attributes_for :ground_colors, :allow_destroy => true
14
15   validates :width, :presence => true, :numericality => true, :natural_number => true
16   validates :height, :presence => true, :numericality => true, :natural_number => true
17   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
18   validates :x, :numericality => {:allow_blank => true}
19   validates :y, :numericality => {:allow_blank => true}
20   validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
21   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
22   validates :publish, :presence => true, :numericality => true
23   
24   before_validation :valid_encode
25   
26   def valid_encode
27     ['caption'].each do |a|
28       next if attributes[a] == nil
29       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
30     end
31   end
32   
33   def supply_default
34     self.border = 2
35     self.publish = 0
36   end
37   
38   def overwrite au
39     self.author_id = au.id
40   end
41   
42   def own? roles
43     roles = [roles] unless roles.respond_to?(:each)
44     au = Panel.get_author_from_roles roles
45     return false unless au
46     self.author_id == au.id
47   end
48   
49   def visible? roles
50     if MagicNumber['run_mode'] == 0
51       return false unless guest_role_check(roles)
52     else
53       return false unless reader_role_check(roles)
54     end
55     return true if self.own?(roles)
56     self.publish?
57   end
58   
59   def usable? au
60     visible? au
61   end
62   
63   def publish?
64     self.publish > 0
65   end
66   
67   def tag_id
68     'panel' + self.tag_panel_id
69   end
70   
71   def tag_panel_id
72     self.new_record? ? '0' : self.id.to_s
73   end
74   
75   def field_tag_id f
76     self.tag_id + f.to_s
77   end
78   
79   def field_tree f
80     'panels-' + tag_panel_id + '-' + f.to_s
81   end
82   
83   def self.default_page_size
84     25
85   end
86   
87   def self.max_page_size
88     100
89   end
90   
91   def self.page prm = nil
92     page = prm.to_i
93     page = 1 if page < 1
94     page
95   end
96   
97   def self.page_size prm = self.default_page_size
98     page_size = prm.to_i
99     page_size = self.max_page_size if page_size > self.max_page_size
100     page_size = self.default_page_size if page_size < 1
101     page_size
102   end
103   
104   def self.list_where
105     'panels.publish > 0'
106   end
107   
108   def self.mylist_where au
109     ['panels.author_id = ?', au.id]
110   end
111   
112   def self.himlist_where au
113     ['panels.author_id = ? and panels.publish > 0', au.id]
114   end
115   
116   def self.list page = 1, page_size = self.default_page_size
117     Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
118   end
119   
120   def self.mylist au, page = 1, page_size = Author.default_panel_page_size
121     Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
122   end
123   
124   def self.himlist au, page = 1, page_size = Author.default_panel_page_size
125     Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
126   end
127   
128   def self.list_paginate page = 1, page_size = self.default_page_size
129     Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
130   end
131   
132   def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
133     Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
134   end
135   
136   def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
137     Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
138   end
139   
140   def self.list_opt
141     {
142       :panel_pictures => {
143         :picture => {:artist => {}, :license => {}}
144       }, 
145       :speech_balloons => {:balloon => {}, :speech => {}},
146       :ground_pictures => {
147         :picture => {:artist => {}, :license => {}}
148       }, 
149       :ground_colors => {
150       }, 
151       :author => {}
152     }
153   end
154   
155   def self.show rid, roles
156     opt = {}
157     opt.merge!(Panel.show_opt)
158     res = Panel.find(rid, opt)
159     raise ActiveRecord::Forbidden unless res.visible?(roles)
160     res
161   end
162   
163   def self.edit rid, au
164     opt = {}
165     opt.merge!(Panel.show_opt)
166     res = Panel.find(rid, opt)
167     raise ActiveRecord::Forbidden unless res.own?(au)
168     res
169   end
170   
171   def self.show_opt
172     {:include => {
173       :panel_pictures => {
174         :picture => {:artist => {}, :license => {}}
175       }, 
176       :speech_balloons => {:balloon => {}, :speech => {}},
177       :ground_pictures => {
178         :picture => {:artist => {}, :license => {}}
179       }, 
180       :ground_colors => {
181       }, 
182       :author => {}
183     }}
184   end
185   
186   def parts_element
187     ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
188   end
189   
190   def parts
191     res = []
192     self.parts_element.each do |e|
193       res[e.t] = e
194     end
195     res
196   end
197   
198   def zorderd_elements
199     res = []
200     self.parts_element.each do |e|
201       res[e.z-1] = e
202     end
203     res
204   end
205   
206   def panel_elements
207     parts
208   end
209   
210   @@elm_json_opt = {
211     'PanelPicture' => {
212       :picture => {:artist => {}, :license => {}}
213     }, 
214     'SpeechBalloon' => {:balloon => {}, :speech => {}},
215     'GroundPicture' => {
216       :picture => {:artist => {}, :license => {}}
217     }, 
218     'GroundColor' => {
219     }, 
220   }
221   
222   def self.elm_json_opt e
223     @@elm_json_opt[e.class.to_s]
224   end
225   
226   def elements
227     self.panel_elements.map {|e|
228       #(-_-;)<... kore wa hidoi
229       JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
230     }
231   end
232   
233   def panel_elements_as_json
234     self.to_json({:include => {:author => {}}, :methods => :elements})
235   end
236   
237   def self.list_as_json_text ary
238     '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
239   end
240   
241   def new_t
242     self.parts.size
243   end
244   
245   def new_z
246     self.parts.size + 1
247   end
248   
249   def scenario
250     panel_elements.map { |e|
251       e.scenario
252     }.join
253   end
254   
255   def plain_scenario
256     panel_elements.map { |e|
257       e.plain_scenario
258     }.join
259   end
260   
261   def licensed_pictures
262     r = {}
263     ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
264       r[elm.picture_id] = elm.picture unless r[elm.picture_id]
265     end
266     r
267   end
268   
269   def self.visible_count
270     Panel.count
271   end
272   
273   def self.collect_element_value elements, name
274     elements.map {|e|
275       e.map {|o|
276         if o['_destroy'] or o[:_destroy]
277           nil
278         else
279           o[name]
280         end
281       }.compact
282     }.flatten
283   end
284   
285   def self.validate_serial ary, offset = 0
286     i = offset
287     ary.compact.sort.each do |n|
288       break false unless n == i
289       i += 1
290     end
291     ary.compact.size == i - offset
292   end
293   
294   def self.validate_element_serial elements, name, offset = 0
295     Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
296   end
297   
298   def self.validate_elements_serial c
299     c.map {|conf|
300       Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
301     }.compact.empty?
302   end
303   
304   def validate_serial_list
305     [
306       {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0}, 
307       {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1}
308     ]
309   end
310   def validate_child
311 #    r1 = Panel.validate_elements_id validate_id_list
312     Panel.validate_elements_serial validate_serial_list
313   end
314   
315   def store attr, au
316     if attr == false
317       self.errors.add :base, I18n.t('errors.invalid_json')
318       return false
319     end
320     self.attributes = attr
321     self.overwrite au
322     res = false
323     Panel.transaction do
324       res = self.save
325       unless validate_child
326         res = false
327         self.errors.add :base, I18n.t('errors.invalid_t')
328         raise ActiveRecord::Rollback
329       end
330     end
331     res
332   end
333   
334   def remove_element target, au
335     ct = target.t
336     cz = target.z
337     panel_attributes = {}
338     self.panel_elements.each do |elm|
339       attr = elm.attributes
340       if elm == target
341         attr['_destroy'] = true
342       end
343       if elm.t > ct
344         attr['t']  -= 1 
345       end
346       if elm.z > cz
347         attr['z']  -= 1 
348       end
349       panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
350       panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
351     end
352     self.store(panel_attributes, au)
353   end
354   
355   def destroy_with_elements
356     res = false
357     Panel.transaction do
358       self.parts_element.each do |element|
359         raise ActiveRecord::Rollback unless element.destroy
360       end
361       raise ActiveRecord::Rollback unless self.destroy
362       res = true
363     end
364     res
365   end
366   
367 =begin
368   def self.validate_id ary, pid
369     ary.map {|v|
370       if pid
371         (v == pid or v == nil) ? nil : false
372       else
373         v ? false : nil
374       end
375     }.compact.empty?
376   end
377   
378   def self.validate_element_id elements, name, pid
379     Panel.validate_id(Panel.collect_element_value(elements, name), pid)
380   end
381   
382   def self.validate_elements_id c
383     c.map {|conf|
384       Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
385     }.compact.empty?
386   end
387   
388   def validate_id_list
389     r = self.speech_balloons.map {|sb|
390       {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
391     }
392     r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})
393     r
394   end
395 =end
396   
397 end