1 class Panel < ActiveRecord::Base
4 belongs_to :resource_picture
5 # belongs_to :background_picture, :class_name => 'ResourcePicture'
6 has_many :panel_pictures, :dependent => :destroy
7 has_many :speech_balloons, :dependent => :destroy
8 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
9 accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
11 # validates :comic_id, :presence => true, :numericality => true, :existence => true, :uniqueness => {:scope => :t}
12 validates :resource_picture_id, :numericality => {:allow_blank => true}
13 validates :width, :presence => true, :numericality => true, :natural_number => true
14 validates :height, :presence => true, :numericality => true, :natural_number => true
15 validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16 validates :x, :numericality => {:allow_blank => true}
17 validates :y, :numericality => {:allow_blank => true}
18 validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
19 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
20 validates :author_id, :presence => true, :numericality => true, :existence => true
23 return false unless au
24 c = self.comic_id ? Comic.show(self.comic_id, au) : nil
26 self.width = c.width if self.width.blank?
27 self.height = c.height if self.height.blank?
28 self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record?
30 self.border = 0 if self.border.blank?
31 self.author_id = au.id
34 def self.new_t comic_id
35 r = Panel.max_t(comic_id)
36 r.blank? ? 0 : r.to_i + 1
39 def self.max_t comic_id
40 Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id])
43 def self.find_t comic_id, t
44 Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
47 #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
51 f = Panel.find_t(self.comic_id, self.t) if self.t
53 Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
55 self.t = Panel.new_t self.comic_id
61 return true if self.t == new_t
63 Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
65 nf = Panel.find_t(self.comic_id, new_t)
66 max_t = Panel.max_t.to_i self.comic_id
67 new_t = max_t if new_t > max_t
68 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
74 def destroy_and_shorten
75 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
79 def self.default_page_size
83 def self.max_page_size
87 def self.page prm = nil
93 def self.page_size prm = self.default_page_size
95 page_size = self.max_page_size if page_size > self.max_page_size
96 page_size = self.default_page_size if page_size < 1
100 def self.offset cnt, prm = nil
102 offset = cnt - 1 if offset >= cnt
103 offset = cnt - offset.abs if offset < 0
104 offset = 0 if offset < 0
108 def self.list opt = {}, page = 1, page_size = self.default_page_size
109 opt.merge!(self.list_opt) unless opt[:include]
110 opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
111 Panel.find(:all, opt)
118 :resource_picture => {:artist => {}, :license => {}}
120 :speech_balloons => {:balloons => {}, :speeches => {}},
125 def self.list_json_opt
127 :comic => {:author => {}},
129 :resource_picture => {:artist => {}, :license => {}}
131 :speech_balloons => {:balloons => {}, :speeches => {}},
136 def self.show rid, au, opt = {}
137 r = Panel.find(rid, :include => self.show_include_opt(opt))
138 raise ActiveRecord::Forbidden unless r.visible?(au)
142 def self.show_include_opt opt = {}
146 :resource_picture => {:artist => {}, :license => {}}
148 :speech_balloons => {:balloons => {}, :speeches => {}},
151 res.merge!(opt[:include]) if opt[:include]
155 def self.show_json_include_opt
157 :comic => {:author => {}},
159 :resource_picture => {:artist => {}, :license => {}}
161 :speech_balloons => {:balloons => {}, :speeches => {}},
167 return false unless au
168 return false unless self.comic
169 self.comic.visible?(au) or self.author_id == au.id
173 return false unless author
174 self.author_id == author.id
179 self.panel_pictures.each do |picture|
180 pe[picture.t] = picture
182 self.balloons.each do |balloon|
183 pe[balloon.t] = balloon
189 self.sort_by_time.each do |e|
196 self.each_element do |elm|
197 if elm.kind_of?(PanelPicture)
198 res[elm.t] = elm.to_json({:include => :resource_picture})
200 if elm.kind_of?(Balloon)
201 res[elm.t] = elm.to_json({:include => :speeches})
208 self.to_json :methods => :panel_elements
211 def self.visible_count