OSDN Git Service

fc7e8b71cf886e8fccdb92b7a4a119b475807aa3
[pettanr/pettanr.git] / app / models / story.rb
1 #ストーリー
2 class Story < ActiveRecord::Base
3   belongs_to :author
4   belongs_to :panel
5   belongs_to :comic
6   
7   validates :comic_id, :presence => true, :numericality => true, :existence => true
8   validates :panel_id, :presence => true, :numericality => true, :existence => true
9   validates :author_id, :presence => true, :numericality => true, :existence => true
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   def supply_default
13     self.t = nil
14   end
15   
16   def overwrite au
17     return false unless au
18     self.author_id = au.id
19   end
20   
21   def own? author
22     return false unless author
23     self.author_id == author.id
24   end
25   
26   def self.edit sid, au
27     res = Story.find sid
28     raise ActiveRecord::Forbidden unless res.own?(au)
29     res
30   end
31   
32   def self.new_t comic_id
33     r = Story.max_t(comic_id)
34     r.blank? ? 0 : r.to_i + 1
35   end
36   
37   def self.max_t comic_id
38     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
39   end
40   
41   def self.find_t comic_id, t
42     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
43   end
44   
45   def self.collect_t story
46     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
47     r.map {|s| s.t}
48   end
49   
50   def self.serial? ary
51     i = 0
52     ary.compact.sort.each do |t|
53       break false unless t == i
54       i += 1
55     end
56     ary.compact.size == i
57   end
58   
59   def self.validate_t story
60     Story.serial?(Story.collect_t(story))
61   end
62   
63   def insert_shift
64     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
65   end
66   
67   def lesser_shift old_t
68     self.t = 0 if self.t < 0
69     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
70   end
71   
72   def higher_shift old_t
73     nf = Story.find_t(self.comic_id, self.t)
74     max_t = Story.max_t(self.comic_id).to_i
75     self.t = max_t if self.t > max_t
76     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
77   end
78   
79   def update_shift old_t
80     if self.t > old_t
81       higher_shift old_t
82     else
83       lesser_shift old_t
84     end
85   end
86   
87   def rotate old_t = nil
88     if self.new_record?
89       if self.t.blank?
90         self.t = Story.new_t self.comic_id
91       else
92         self.insert_shift
93       end
94     else
95       if self.t.blank?
96       else
97         self.update_shift old_t
98       end
99     end
100   end
101   
102   def allow?
103     c = self.comic
104     pl = self.panel
105     c and c.own?(self.author) and pl and pl.usable?(self.author)
106   end
107   
108   def store old_t = nil
109     res = false
110     raise ActiveRecord::Forbidden unless self.allow?
111     Story.transaction do
112       self.rotate old_t
113       res = self.save
114       raise ActiveRecord::Rollback unless res
115       res = Story.validate_t(self) 
116       unless res
117         self.errors.add :t, 'unserialized'
118         raise ActiveRecord::Rollback 
119       end
120     end
121     res
122   end
123   
124   def destroy_and_shorten
125     Story.transaction do
126       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
127       raise ActiveRecord::Rollback unless self.destroy
128     end
129   end
130   
131   def self.default_page_size
132     25
133   end
134   
135   def self.max_page_size
136     100
137   end
138   
139   def self.page prm = nil
140     page = prm.to_i
141     page = 1 if page < 1
142     page
143   end
144   
145   def self.page_size prm = self.default_page_size
146     page_size = prm.to_i
147     page_size = self.max_page_size if page_size > self.max_page_size
148     page_size = self.default_page_size if page_size < 1
149     page_size
150   end
151   
152   def self.default_panel_size
153     30
154   end
155   
156   def self.max_panel_size
157     200
158   end
159   
160   def self.offset cnt, prm = nil
161     offset = prm.to_i
162     offset = cnt - 1 if offset >= cnt
163     offset = cnt - offset.abs if offset < 0
164     offset = 0 if offset < 0
165     offset
166   end
167   
168   def self.panel_count cnt, prm = self.default_panel_size
169     count = prm.to_i
170     count = self.max_panel_size if count > self.max_panel_size
171     count = self.default_panel_size if count < 1
172     count
173   end
174   
175   def self.list comic, author, offset = 0, limit = Story.default_panel_size
176     opt = self.list_opt
177     opt.merge!({:conditions => ['stories.comic_id = ? and panels.publish > 0', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
178     Story.find(:all, opt)
179   end
180   
181   def self.list_opt
182     {:include => {
183       :author => {}, 
184       :comic => {
185         :author => {}
186       }, 
187       :panel => {
188         :author => {}, 
189         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
190         :speech_balloons =>{:balloons => {}, :speeches => {}}
191       }
192     }}
193   end
194   
195   def self.list_json_opt
196     {:include => {
197       :author => {}, 
198       :comic => {
199         :author => {}
200       }, 
201       :panel => {
202         :author => {}, 
203         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
204         :speech_balloons =>{:balloons => {}, :speeches => {}}
205       }
206     }}
207   end
208   
209   def self.mylist au, opt = {}, page = 1, story_page_size = Author.default_story_page_size
210     opt.merge!(self.list_opt) unless opt[:include]
211     opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc', :limit => page_size, :offset => (page -1) * story_page_size})
212     Story.find(:all, opt)
213   end
214   
215   def to_json_list
216     self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
217   end
218   
219   
220 end