1 class Panel < ActiveRecord::Base
4 has_many :panel_pictures, :dependent => :destroy
5 has_many :balloons, :dependent => :destroy
6 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
7 accepts_nested_attributes_for :balloons, :allow_destroy => true
9 def self.max_t comic_id
10 Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id]).to_i
13 def self.find_t comic_id, t
14 Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
17 #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
21 max_t = Panel.max_t self.comic_id
22 f = Panel.find_t(self.comic_id, self.t) if self.t
24 Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
32 return true if self.t == new_t
34 Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
36 nf = Panel.find_t(self.comic_id, new_t)
37 max_t = Panel.max_t self.comic_id
38 new_t = max_t if new_t > max_t
39 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
45 def destroy_and_shorten
46 Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
51 return false unless author
52 self.author_id == author.id
57 self.panel_pictures.each do |picture|
58 pe[picture.t] = picture
60 self.balloons.each do |balloon|
61 pe[balloon.t] = balloon
67 self.sort_by_time.each do |e|
74 self.each_element do |elm|
75 if elm.kind_of?(PanelPicture)
76 res[elm.t] = elm.to_json({:include => :resource_picture})
78 if elm.kind_of?(Balloon)
79 res[elm.t] = elm.to_json({:include => :speaches})
86 self.to_json :methods => :panel_elements