1 class StorySheet < Peta::Leaf
7 validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :sheet_id, :presence => true, :numericality => true, :existence => {:both => false}
9 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
18 def overwrite operators
19 return false unless operators.author
20 self.author_id = operators.author.id
23 def self.public_list_where
28 'story_sheets.updated_at desc'
31 def self.play_sheet_where sid
32 ['story_sheets.story_id = ?', sid]
35 def self.page prm = nil
40 def self.play_sheet story, operators, page = 1
41 ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
44 Sheet.show(ss.sheet.id, operators)
53 def self.play_paginate story, page
54 Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
57 def self.by_author_list_includes
78 def self.list_json_opt
82 :sheet => {:include => {:author => {}}}
96 def self.show_json_opt
100 :sheet => {:include => {:author => {}}}
108 def story_sheet_as_json au
109 panel_include = if self.panel and self.panel.visible?(au)
110 {:include => {:author => {}}, :methods => :elements}
112 {:include => {:author => {}}}
114 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
117 def self.list_as_json_text ary, au
118 '[' + ary.map {|i| i.story_sheet_as_json(au) }.join(',') + ']'
121 def self.top_sheet story, author
122 StorySheet.play_list(story, author).first
126 return nil if self.story_id == nil or self.sheet_id == nil
127 self.story.own?(operators) and self.sheet.usable?(operators)
130 def store operators, old_t = nil
132 self.class.transaction do
133 case self.allow? operators
137 raise ActiveRecord::Forbidden
141 raise ActiveRecord::Rollback unless res
142 res = self.class.validate_t(self.story_id)
144 self.errors.add :t, 'unserialized'
145 raise ActiveRecord::Rollback