OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[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 => {:both => false}
8   validates :panel_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.comic_id = nil
14     self.panel_id = nil
15     self.t = nil
16   end
17   
18   def overwrite au
19     return false unless au
20     self.author_id = au.id
21   end
22   
23   def own? roles
24     roles = [roles] unless roles.respond_to?(:each)
25     au = Story.get_author_from_roles roles
26     return false unless au
27     self.author_id == au.id
28   end
29   
30   def visible? roles
31     if MagicNumber['run_mode'] == 0
32       return false unless guest_role_check(roles)
33     else
34       return false unless reader_role_check(roles)
35     end
36     return true if self.comic.own?(roles)
37     self.comic.visible? roles
38   end
39   
40   def self.default_panel_size
41     30
42   end
43   
44   def self.max_panel_size
45     200
46   end
47   
48   def self.offset cnt, prm = nil
49     offset = prm.to_i
50     offset = cnt - 1 if offset >= cnt
51     offset = cnt - offset.abs if offset < 0
52     offset = 0 if offset < 0
53     offset
54   end
55   
56   def self.panel_count cnt, prm = self.default_panel_size
57     count = prm.to_i
58     count = self.max_panel_size if count > self.max_panel_size
59     count = self.default_panel_size if count < 1
60     count
61   end
62   
63   def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
64     opt = {}
65     opt.merge!(Story.list_opt)
66     opt.merge!({:offset => offset, :limit => limit}) if limit > 0
67     opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
68     Story.find(:all, opt)
69   end
70   
71   def self.list_opt
72     {:include => {
73       :author => {}, 
74       :comic => {
75         :author => {}
76       }, 
77       :panel => {
78         :author => {}, 
79         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
80         :speech_balloons =>{:balloons => {}, :speeches => {}}
81       }
82     }}
83   end
84   
85   def self.list_json_opt
86     {:include => {
87       :author => {}, 
88       :comic => {
89         :author => {}
90       }, 
91       :panel => {
92         :author => {}, 
93         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
94         :speech_balloons =>{:balloons => {}, :speeches => {}}
95       }
96     }}
97   end
98   
99   def self.default_page_size
100     25
101   end
102   
103   def self.max_page_size
104     100
105   end
106   
107   def self.page prm = nil
108     page = prm.to_i
109     page = 1 if page < 1
110     page
111   end
112   
113   def self.page_size prm = self.default_page_size
114     page_size = prm.to_i
115     page_size = self.max_page_size if page_size > self.max_page_size
116     page_size = self.default_page_size if page_size < 1
117     page_size
118   end
119   
120   def self.list page = 1, page_size = self.default_page_size
121     opt = {}
122     opt.merge!(self.list_opt)
123     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
124     opt.merge!({:conditions => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
125     Story.find(:all, opt)
126   end
127   
128   def self.mylist au, page = 1, page_size = Author.default_story_page_size
129     opt = {}
130     opt.merge!(Story.list_opt)
131     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
132     opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
133     Story.find(:all, opt)
134   end
135   
136   def self.show sid, roles
137     opt = {}
138     opt.merge!(Story.show_opt)
139     res = Story.find sid, opt
140     raise ActiveRecord::Forbidden unless res.visible?(roles)
141     res
142   end
143   
144   def self.edit sid, au
145     opt = {}
146     opt.merge!(Story.show_opt)
147     res = Story.find sid, opt
148     raise ActiveRecord::Forbidden unless res.own?(au)
149     res
150   end
151   
152   def self.show_opt
153     {:include => {
154       :author => {}, 
155       :comic => {
156         :author => {}
157       }, 
158       :panel => {
159         :author => {}, 
160         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
161         :speech_balloons =>{:balloons => {}, :speeches => {}}
162       }
163     }}
164   end
165   
166   def elements
167     self.panel.elements
168   end
169   
170   def story_as_json au
171     panel_include = if self.panel and self.panel.visible?(au)
172       {:include => {:author => {}}, :methods => :elements}
173     else
174       {:include => {:author => {}}}
175     end
176     self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
177   end
178   
179   def self.list_as_json_text ary, au
180     '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
181   end
182   
183   def self.licensed_pictures stories
184     r = {}
185     stories.each do |story|
186       r.merge!(story.panel.licensed_pictures) if story.panel
187     end
188     r
189   end
190   
191   def self.new_t comic_id
192     r = Story.max_t(comic_id)
193     r.blank? ? 0 : r.to_i + 1
194   end
195   
196   def self.max_t comic_id
197     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
198   end
199   
200   def self.find_t comic_id, t
201     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
202   end
203   
204   def self.collect_t story
205     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
206     r.map {|s| s.t}
207   end
208   
209   def self.serial? ary
210     i = 0
211     ary.compact.sort.each do |t|
212       break false unless t == i
213       i += 1
214     end
215     ary.compact.size == i
216   end
217   
218   def self.validate_t story
219     Story.serial?(Story.collect_t(story))
220   end
221   
222   def insert_shift
223     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
224   end
225   
226   def lesser_shift old_t
227     self.t = 0 if self.t < 0
228     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
229   end
230   
231   def higher_shift old_t
232     nf = Story.find_t(self.comic_id, self.t)
233     max_t = Story.max_t(self.comic_id).to_i
234     self.t = max_t if self.t > max_t
235     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
236   end
237   
238   def update_shift old_t
239     if self.t > old_t
240       higher_shift old_t
241     else
242       lesser_shift old_t
243     end
244   end
245   
246   def rotate old_t = nil
247     if self.new_record?
248       if self.t.blank?
249         self.t = Story.new_t self.comic_id
250       else
251         self.insert_shift
252       end
253     else
254       if self.t.blank?
255       else
256         self.update_shift old_t
257       end
258     end
259   end
260   
261   def allow?
262     return nil if self.comic_id == nil or self.panel_id == nil
263     self.comic.own?(self.author) and self.panel.usable?(self.author)
264   end
265   
266   def store old_t = nil
267     res = false
268     Story.transaction do
269       case self.allow?
270       when true
271         self.rotate old_t
272       when false
273         raise ActiveRecord::Forbidden
274       else
275       end
276       res = self.save
277       raise ActiveRecord::Rollback unless res
278       res = Story.validate_t(self) 
279       unless res
280         self.errors.add :t, 'unserialized'
281         raise ActiveRecord::Rollback 
282       end
283     end
284     res
285   end
286   
287   def destroy_and_shorten
288     res = false
289     Story.transaction do
290       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
291       raise ActiveRecord::Rollback unless self.destroy
292       res = true
293     end
294     res
295   end
296   
297   
298 end