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)
53 def self.default_page_size
57 def self.max_page_size
61 def self.default_panel_size
65 def self.max_panel_size
69 def self.page prm = nil
75 def self.page_size prm = self.default_page_size
77 page_size = self.max_page_size if page_size > self.max_page_size
78 page_size = self.default_page_size if page_size < 1
86 def self.mylist_where au
87 ['stories.author_id = ?', au.id]
90 def self.himlist_where au
91 ['stories.author_id = ? and stories.visible > 0', au.id]
94 def self.list page = 1, page_size = self.default_page_size
95 Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
98 def self.mylist au, page = 1, page_size = Author.default_story_page_size
99 Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
102 def self.himlist au, page = 1, page_size = Author.default_story_page_size
103 Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
106 def self.list_paginate page = 1, page_size = self.default_page_size
107 Kaminari.paginate_array(Array.new(Story.where(self.list_where()).count, nil)).page(page).per(page_size)
110 def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
111 Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
114 def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
115 Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
119 {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }
122 def self.list_json_opt
123 {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
126 def self.show sid, roles
128 opt.merge!(Story.show_opt)
129 res = Story.find(sid, opt)
130 raise ActiveRecord::Forbidden unless res.visible?(roles)
134 def self.edit sid, au
136 opt.merge!(Story.show_opt)
137 res = Story.find(sid, opt)
138 raise ActiveRecord::Forbidden unless res.own?(au)
143 {:include => {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }}
146 def self.show_json_opt
147 {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
150 def self.visible_count
151 Story.count 'visible > 0'
154 def destroy_with_story_sheet
157 self.story_sheets.each do |story_sheet|
158 raise ActiveRecord::Rollback unless story_sheet.destroy
160 raise ActiveRecord::Rollback unless self.destroy
166 def self.new_t comic_id
167 r = Story.max_t(comic_id)
168 r.blank? ? 0 : r.to_i + 1
171 def self.max_t comic_id
172 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
175 def self.find_t comic_id, t
176 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
179 def self.collect_t story
180 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
186 ary.compact.sort.each do |t|
187 break false unless t == i
190 ary.compact.size == i
193 def self.validate_t story
194 Story.serial?(Story.collect_t(story))
198 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
201 def lesser_shift old_t
202 self.t = 0 if self.t < 0
203 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
206 def higher_shift old_t
207 nf = Story.find_t(self.comic_id, self.t)
208 max_t = Story.max_t(self.comic_id).to_i
209 self.t = max_t if self.t > max_t
210 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
213 def update_shift old_t
221 def rotate old_t = nil
224 self.t = Story.new_t self.comic_id
231 self.update_shift old_t
237 return nil if self.comic_id == nil
238 self.comic.own?(self.author)
241 def store old_t = nil
248 raise ActiveRecord::Forbidden
252 raise ActiveRecord::Rollback unless res
253 res = Story.validate_t(self)
255 self.errors.add :t, 'unserialized'
256 raise ActiveRecord::Rollback
262 def destroy_and_shorten
265 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
266 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet