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 :balloons, :dependent => :destroy
8 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
9 accepts_nested_attributes_for :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 c = self.comic_id ? Comic.show(self.comic_id, au) : nil
25 self.width = c.width if self.width.blank?
26 self.height = c.height if self.height.blank?
27 self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record?
29 self.border = 0 if self.border.blank?
30 self.author_id = au.id
33 def self.new_t comic_id
34 r = Panel.max_t(comic_id)
35 r.blank? ? 0 : r.to_i + 1
38 def self.max_t comic_id
39 Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id])
42 def self.find_t comic_id, t
43 Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
46 #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
50 f = Panel.find_t(self.comic_id, self.t) if self.t
52 Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
54 self.t = Panel.new_t self.comic_id
60 return true if self.t == new_t
62 Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
64 nf = Panel.find_t(self.comic_id, new_t)
65 max_t = Panel.max_t.to_i self.comic_id
66 new_t = max_t if new_t > max_t
67 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
73 def destroy_and_shorten
74 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
78 def self.default_page_size
82 def self.max_page_size
86 def self.page prm = nil
92 def self.page_size prm = self.default_page_size
94 page_size = self.max_page_size if page_size > self.max_page_size
95 page_size = self.default_page_size if page_size < 1
99 def self.offset cnt, prm = nil
101 offset = cnt - 1 if offset >= cnt
102 offset = cnt - offset.abs if offset < 0
103 offset = 0 if offset < 0
107 def self.list opt = {}, page = 1, page_size = self.default_page_size
108 opt.merge!(self.list_opt) unless opt[:include]
109 opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
110 Panel.find(:all, opt)
117 :resource_picture => {:artist => {}, :license => {}}
119 :balloons => {:speaches => {}},
124 def self.list_json_opt
128 :resource_picture => {:artist => {}, :license => {}}
130 :balloons => {:speaches => {}},
135 def self.show rid, au, opt = {}
136 r = Panel.find(rid, :include => self.show_include_opt(opt))
137 raise ActiveRecord::Forbidden unless r.visible?(au)
141 def self.show_include_opt opt = {}
145 :resource_picture => {:artist => {}, :license => {}}
147 :balloons => {:speaches => {}},
150 res.merge!(opt[:include]) if opt[:include]
154 def self.show_json_include_opt
158 :resource_picture => {:artist => {}, :license => {}}
160 :balloons => {:speaches => {}},
166 return false unless au
167 return false unless self.comic
168 self.comic.visible?(au) or self.author_id == au.id
172 return false unless author
173 self.author_id == author.id
178 self.panel_pictures.each do |picture|
179 pe[picture.t] = picture
181 self.balloons.each do |balloon|
182 pe[balloon.t] = balloon
188 self.sort_by_time.each do |e|
195 self.each_element do |elm|
196 if elm.kind_of?(PanelPicture)
197 res[elm.t] = elm.to_json({:include => :resource_picture})
199 if elm.kind_of?(Balloon)
200 res[elm.t] = elm.to_json({:include => :speaches})
207 self.to_json :methods => :panel_elements