OSDN Git Service

562cabad1e334e72b1856545cc74dcd964c983a0
[pettanr/pettanr.git] / app / models / story_sheet.rb
1 class StorySheet < Peta::Leaf
2   load_manifest
3   belongs_to :author
4   belongs_to :story
5   belongs_to :sheet
6   
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}
11   
12   def supply_default
13     self.story_id = nil
14     self.sheet_id = nil
15     self.t = nil
16   end
17   
18   def overwrite operators
19     return false unless operators.author
20     self.author_id = operators.author.id
21   end
22   
23   def self.public_list_where
24     'stories.visible > 0'
25   end
26   
27   def self.list_order
28     'story_sheets.updated_at desc'
29   end
30   
31   def self.play_sheet_where sid
32     ['story_sheets.story_id = ?', sid]
33   end
34   
35  def self.page prm = nil
36     page = prm.to_i
37     page = 1 if page < 1
38     page
39   end
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
42     if ss 
43       if ss.sheet
44         Sheet.show(ss.sheet.id, operators)
45       else
46         nil
47       end
48     else
49       nil
50     end
51   end
52   
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)
55   end
56   
57   def self.by_author_list_includes
58     {
59       :story => {
60         :comic => {
61           :author => {}
62         }
63       }
64     }
65   end
66   
67   def self.list_opt
68     {
69       :author => {}, 
70       :story => {
71       }, 
72       :sheet => {
73         :author => {},
74       }
75     }
76   end
77   
78   def self.list_json_opt
79     {:include => {
80       :author => {}, 
81       :story => {} ,
82       :sheet => {:include => {:author => {}}} 
83     }}
84   end
85   
86   def self.show_opt
87     {:include => {
88       :author => {}, 
89       :story => {}, 
90       :sheet => {
91         :author => {} 
92       }
93     }}
94   end
95   
96   def self.show_json_opt
97     {:include => {
98       :author => {}, 
99       :story => {} ,
100       :sheet => {:include => {:author => {}}} 
101     }}
102   end
103   
104   def elements
105     self.panel.elements
106   end
107   
108   def story_sheet_as_json au
109     panel_include = if self.panel and self.panel.visible?(au)
110       {:include => {:author => {}}, :methods => :elements}
111     else
112       {:include => {:author => {}}}
113     end
114     self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
115   end
116   
117   def self.list_as_json_text ary, au
118     '[' + ary.map {|i| i.story_sheet_as_json(au) }.join(',') + ']'
119   end
120   
121   def self.top_sheet story, author
122     StorySheet.play_list(story, author).first
123   end
124   
125   def allow? operators
126     return nil if self.story_id == nil or self.sheet_id == nil
127     self.story.own?(operators) and self.sheet.usable?(operators)
128   end
129   
130   def store operators, old_t = nil
131     res = false
132     self.class.transaction do
133       case self.allow? operators
134       when true
135         self.rotate old_t
136       when false
137         raise ActiveRecord::Forbidden
138       else
139       end
140       res = self.save
141       raise ActiveRecord::Rollback unless res
142       res = self.class.validate_t(self.story_id) 
143       unless res
144         self.errors.add :t, 'unserialized'
145         raise ActiveRecord::Rollback 
146       end
147     end
148     res
149   end
150   
151 end