2 class Story < ActiveRecord::Base
7 validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :title, :presence => true, :length => {:maximum => 100}
9 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
10 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
11 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
12 before_validation :valid_encode
15 ['title', 'description'].each do |a|
16 next if attributes[a] == nil
17 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
23 self.visible = 0 if self.visible.blank?
28 return false unless au
29 self.author_id = au.id
33 roles = [roles] unless roles.respond_to?(:each)
34 au = Story.get_author_from_roles roles
35 return false unless au
36 self.author_id == au.id
40 if MagicNumber['run_mode'] == 0
41 return false unless guest_role_check(roles)
43 return false unless reader_role_check(roles)
45 return true if self.own?(roles)
49 def self.default_page_size
53 def self.max_page_size
57 def self.default_panel_size
61 def self.max_panel_size
65 def self.page prm = nil
71 def self.page_size prm = self.default_page_size
73 page_size = self.max_page_size if page_size > self.max_page_size
74 page_size = self.default_page_size if page_size < 1
82 def self.mylist_where au
83 ['stories.author_id = ?', au.id]
86 def self.himlist_where au
87 ['stories.author_id = ? and stories.visible > 0', au.id]
90 def self.list page = 1, page_size = self.default_page_size
91 Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
94 def self.mylist au, page = 1, page_size = Author.default_story_page_size
95 Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
98 def self.himlist au, page = 1, page_size = Author.default_story_page_size
99 Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
102 def self.list_paginate page = 1, page_size = self.default_page_size
103 Kaminari.paginate_array(Array.new(Story.where(self.list_where()).count, nil)).page(page).per(page_size)
106 def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
107 Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
110 def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
111 Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
115 {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }
118 def self.list_json_opt
119 {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
122 def self.show sid, roles
124 opt.merge!(Story.show_opt)
125 res = Story.find(sid, opt)
126 raise ActiveRecord::Forbidden unless res.visible?(roles)
130 def self.edit sid, au
132 opt.merge!(Story.show_opt)
133 res = Story.find(sid, opt)
134 raise ActiveRecord::Forbidden unless res.own?(au)
139 {:include => {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }}
142 def self.show_json_opt
143 {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
146 def self.visible_count
147 Story.count 'visible > 0'
150 def destroy_with_story_sheet
153 self.story_sheets.each do |story_sheet|
154 raise ActiveRecord::Rollback unless story_sheet.destroy
156 raise ActiveRecord::Rollback unless self.destroy
162 def self.new_t comic_id
163 r = Story.max_t(comic_id)
164 r.blank? ? 0 : r.to_i + 1
167 def self.max_t comic_id
168 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
171 def self.find_t comic_id, t
172 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
175 def self.collect_t story
176 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
182 ary.compact.sort.each do |t|
183 break false unless t == i
186 ary.compact.size == i
189 def self.validate_t story
190 Story.serial?(Story.collect_t(story))
194 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
197 def lesser_shift old_t
198 self.t = 0 if self.t < 0
199 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
202 def higher_shift old_t
203 nf = Story.find_t(self.comic_id, self.t)
204 max_t = Story.max_t(self.comic_id).to_i
205 self.t = max_t if self.t > max_t
206 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
209 def update_shift old_t
217 def rotate old_t = nil
220 self.t = Story.new_t self.comic_id
227 self.update_shift old_t
233 return nil if self.comic_id == nil
234 self.comic.own?(self.author)
237 def store old_t = nil
244 raise ActiveRecord::Forbidden
248 raise ActiveRecord::Rollback unless res
249 res = Story.validate_t(self)
251 self.errors.add :t, 'unserialized'
252 raise ActiveRecord::Rollback
258 def destroy_and_shorten
261 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
262 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet