2 class Story < ActiveRecord::Base
7 validates :comic_id, :presence => true, :numericality => true, :existence => true
8 validates :panel_id, :presence => true, :numericality => true, :existence => true
9 validates :author_id, :presence => true, :numericality => true, :existence => true
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14 return false unless au
15 self.author_id = au.id
19 return false unless author
20 self.author_id == author.id
23 def self.show sid, au = nil
26 raise ActiveRecord::Forbidden unless res.own?(au)
31 def self.new_t comic_id
32 r = Story.max_t(comic_id)
33 r.blank? ? 0 : r.to_i + 1
36 def self.max_t comic_id
37 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
40 def self.find_t comic_id, t
41 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
44 def self.collect_t story
45 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
46 r << story if story.new_record?
52 ary.compact.sort.each do |t|
53 break false unless t == i
59 def self.validate_t story
60 Story.serial?(Story.collect_t(story))
64 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
67 def lesser_shift old_t
68 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
71 def higher_shift old_t
72 nf = Story.find_t(self.comic_id, self.t)
73 max_t = Story.max_t(self.comic_id).to_i
74 self.t = max_t if self.t > max_t
75 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
78 def update_shift old_t
86 def rotate old_t = nil
89 self.t = Story.new_t self.comic_id
96 self.update_shift old_t
104 c and c.own?(self.author) and pl and pl.usable?(self.author)
107 def store old_t = nil
109 raise ActiveRecord::Forbidden unless self.allow?
113 raise ActiveRecord::Rollback unless res
114 res = Story.validate_t(self)
116 self.errors.add :t, 'unserialized'
117 raise ActiveRecord::Rollback
123 def destroy_and_shorten
125 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
126 raise ActiveRecord::Rollback unless self.destroy
130 def self.default_panel_size
134 def self.max_panel_size
138 def self.offset cnt, prm = nil
140 offset = cnt - 1 if offset >= cnt
141 offset = cnt - offset.abs if offset < 0
142 offset = 0 if offset < 0
146 def self.panel_count cnt, prm = self.default_panel_size
148 count = self.max_panel_size if count > self.max_panel_size
149 count = self.default_panel_size if count < 1
153 def self.list comic, author, offset = 0, limit = Story.default_panel_size
155 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
156 Story.find(:all, opt)
167 :panel_pictures => {:resource_picture => {:artist => {}}},
168 :speech_balloons =>{:balloons => {}, :speeches => {}}
173 def self.list_json_opt
181 :panel_pictures => {:resource_picture => {:artist => {}}},
182 :speech_balloons =>{:balloons => {}, :speeches => {}}
188 self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})