class StorySheet < Peta::Leaf
load_manifest
- belongs_to :author
belongs_to :story
belongs_to :sheet
validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :sheet_id, :presence => true, :numericality => true, :existence => {:both => false}
- validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
def supply_default
end
def overwrite operators
- return false unless operators.author
- self.author_id = operators.author.id
end
- def self.list_where
+ def self.public_list_where list
'stories.visible > 0'
end
- def self.list_order
- 'story_sheets.updated_at desc'
- end
-
- def self.play_sheet_where sid
- ['story_sheets.story_id = ?', sid]
- end
-
- def self.page prm = nil
- page = prm.to_i
- page = 1 if page < 1
- page
- end
- def self.play_sheet story, operators, page = 1
- ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
- if ss
- if ss.sheet
- Sheet.show(ss.sheet.id, operators)
- else
- nil
- end
- else
- nil
- end
- end
-
- def self.play_paginate story, page
- Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
- end
-
def self.by_author_list_includes
{
:story => {
- :comic => {
- :author => {}
- }
+ :author => {}
}
}
end
- def self.list_opt
- {
- :author => {},
- :story => {
- },
- :sheet => {
- :author => {},
- }
- }
- end
-
- def self.list_json_opt
- {:include => {
- :author => {},
- :story => {} ,
- :sheet => {:include => {:author => {}}}
- }}
- end
-
def self.show_opt
{:include => {
- :author => {},
:story => {},
:sheet => {
:author => {}
}}
end
- def self.show_json_opt
- {:include => {
- :author => {},
- :story => {} ,
- :sheet => {:include => {:author => {}}}
- }}
- end
-
- def elements
- self.panel.elements
- end
-
- def story_sheet_as_json au
- panel_include = if self.panel and self.panel.visible?(au)
- {:include => {:author => {}}, :methods => :elements}
- else
- {:include => {:author => {}}}
- end
- self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
- end
-
- def self.list_as_json_text ary, au
- '[' + ary.map {|i| i.story_sheet_as_json(au) }.join(',') + ']'
- end
-
- def self.new_t story_id
- r = StorySheet.max_t(story_id)
- r.blank? ? 0 : r.to_i + 1
- end
-
- def self.top_sheet story, author
- StorySheet.play_list(story, author).first
- end
-
- def self.max_t story_id
- StorySheet.maximum(:t, :conditions => ['story_id = ?', story_id])
- end
-
- def self.find_t story_id, t
- StorySheet.find(:first, :conditions => ['story_id = ? and t = ?', story_id, t])
- end
-
- def self.collect_t story_sheet
- r = StorySheet.find(:all, :conditions => ['story_id = ?', story_sheet.story_id], :order => 't')
- r.map {|sp| sp.t}
- end
-
- def self.serial? ary
- i = 0
- ary.compact.sort.each do |t|
- break false unless t == i
- i += 1
- end
- ary.compact.size == i
- end
-
- def self.validate_t story_sheet
- StorySheet.serial?(StorySheet.collect_t(story_sheet))
- end
-
- def insert_shift
- StorySheet.update_all('t = t + 1', ['story_id = ? and t >= ?', self.story_id, self.t])
- end
-
- def lesser_shift old_t
- self.t = 0 if self.t < 0
- StorySheet.update_all('t = t + 1', ['story_id = ? and (t >= ? and t < ?)', self.story_id, self.t, old_t])
- end
-
- def higher_shift old_t
- nf = StorySheet.find_t(self.story_id, self.t)
- max_t = StorySheet.max_t(self.story_id).to_i
- self.t = max_t if self.t > max_t
- StorySheet.update_all('t = t - 1', ['story_id = ? and (t > ? and t <= ?)', self.story_id, old_t, self.t])
- end
-
- def update_shift old_t
- if self.t > old_t
- higher_shift old_t
- else
- lesser_shift old_t
- end
- end
-
- def rotate old_t = nil
- if self.new_record?
- if self.t.blank?
- self.t = StorySheet.new_t self.story_id
- else
- self.insert_shift
- end
- else
- if self.t.blank?
- else
- self.update_shift old_t
- end
- end
- end
-
def allow? operators
return nil if self.story_id == nil or self.sheet_id == nil
self.story.own?(operators) and self.sheet.usable?(operators)
end
- def store operators, old_t = nil
- res = false
- StorySheet.transaction do
- 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 = StorySheet.validate_t(self)
- unless res
- self.errors.add :t, 'unserialized'
- raise ActiveRecord::Rollback
- end
- end
- res
- end
-
- def destroy_and_shorten
- res = false
- StorySheet.transaction do
- StorySheet.update_all('t = t - 1', ['story_id = ? and (t > ?)', self.story_id, self.t])
- raise ActiveRecord::Rollback unless self.destroy
- res = true
- end
- res
- end
-
end