#ストーリー
-class Story < ActiveRecord::Base
- belongs_to :author
- belongs_to :panel
+class Story < Peta::Binder
+ load_manifest
+ has_many :story_sheets
belongs_to :comic
- validates :comic_id, :presence => true, :numericality => true, :existence => true
- validates :panel_id, :presence => true, :numericality => true, :existence => true
- validates :author_id, :presence => true, :numericality => true, :existence => true
+ validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :title, :presence => true, :length => {:maximum => 100}
+ validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
def supply_default
+ self.comic_id = nil
+ self.visible = 0 if self.visible.blank?
self.t = nil
end
- def overwrite au
- return false unless au
- self.author_id = au.id
+ def overwrite
end
- def own? author
- return false unless author
- self.author_id == author.id
+ def visible? operators
+ return false unless super
+ self.visible > 0
end
- def self.edit sid, au
- res = Story.find sid
- raise ActiveRecord::Forbidden unless res.own?(au)
+ def disp_t
+ self.t + 1
+ end
+
+ def disp_t_by_text
+ I18n.t('stories.show.t', :t => self.disp_t)
+ end
+
+ def title_with_t
+ self.disp_t_by_text + ':' + self.title
+ end
+
+ def story_sheets_count
+ StorySheet.where(['story_sheets.story_id = ?', self.id]).count
+ end
+
+ def self.public_list_where
+ 'stories.visible > 0'
+ end
+
+ def self.list_order
+ 'stories.updated_at desc'
+ end
+
+ def self.list_opt
+ {:comic => {:author => {}} }
+ end
+
+ def self.by_author_list_includes
+ {
+ :comic => {
+ :author => {}
+ }
+ }
+ end
+
+ def self.list_json_opt
+ {:include => {:comic => {:include => {:author => {}}} }}
+ end
+
+ def self.show_opt
+ {:include => {:comic => {:author => {}} }}
+ end
+
+ def self.show_json_opt
+ {:include => {:comic => {:include => {:author => {}}} }}
+ end
+
+ def self.visible_count
+ Story.count 'visible > 0'
+ end
+
+ def destroy_with_story_sheet
+ res = false
+ Story.transaction do
+ self.story_sheets.each do |story_sheet|
+ raise ActiveRecord::Rollback unless story_sheet.destroy
+ end
+ raise ActiveRecord::Rollback unless self.destroy
+ res = true
+ end
res
end
end
end
- def allow?
- c = self.comic
- pl = self.panel
- c and c.own?(self.author) and pl and pl.usable?(self.author)
+ def allow? operators
+ return nil if self.comic_id == nil
+ self.comic.own?(operators)
end
- def store old_t = nil
+ def store operators, old_t = nil
res = false
- raise ActiveRecord::Forbidden unless self.allow?
Story.transaction do
- self.rotate old_t
+ case self.allow? operators
+ when true
+ self.rotate old_t
+ when false
+ raise ActiveRecord::Forbidden
+ else
+ end
res = self.save
raise ActiveRecord::Rollback unless res
res = Story.validate_t(self)
end
def destroy_and_shorten
+ res = false
Story.transaction do
Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
- raise ActiveRecord::Rollback unless self.destroy
+ raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
+ res = true
end
- end
-
- def self.default_panel_size
- 30
- end
-
- def self.max_panel_size
- 200
- end
-
- def self.offset cnt, prm = nil
- offset = prm.to_i
- offset = cnt - 1 if offset >= cnt
- offset = cnt - offset.abs if offset < 0
- offset = 0 if offset < 0
- offset
- end
-
- def self.panel_count cnt, prm = self.default_panel_size
- count = prm.to_i
- count = self.max_panel_size if count > self.max_panel_size
- count = self.default_panel_size if count < 1
- count
- end
-
- def self.list comic, author, offset = 0, limit = Story.default_panel_size
- opt = self.list_opt
- opt.merge!({:conditions => ['stories.comic_id = ? and panels.publish > 0', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
- Story.find(:all, opt)
- end
-
- def self.list_opt
- {:include => {
- :author => {},
- :comic => {
- :author => {}
- },
- :panel => {
- :author => {},
- :panel_pictures => {:resource_picture => {:artist => {}}},
- :speech_balloons =>{:balloons => {}, :speeches => {}}
- }
- }}
- end
-
- def self.list_json_opt
- {:include => {
- :author => {},
- :comic => {
- :author => {}
- },
- :panel => {
- :author => {},
- :panel_pictures => {:resource_picture => {:artist => {}}},
- :speech_balloons =>{:balloons => {}, :speeches => {}}
- }
- }}
- end
-
- def to_json_list
- self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
+ res
end