OSDN Git Service

t#31485:editate ground_pictures
[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.himlist au, page = 1, page_size = Author.default_story_page_size
137     opt = {}
138     opt.merge!(Story.list_opt)
139     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
140     opt.merge!({:conditions => ['stories.author_id = ? and comics.visible > 0', au.id], :order => 'stories.updated_at desc'})
141     Story.find(:all, opt)
142   end
143   
144   def self.show sid, roles
145     opt = {}
146     opt.merge!(Story.show_opt)
147     res = Story.find sid, opt
148     raise ActiveRecord::Forbidden unless res.visible?(roles)
149     res
150   end
151   
152   def self.edit sid, au
153     opt = {}
154     opt.merge!(Story.show_opt)
155     res = Story.find sid, opt
156     raise ActiveRecord::Forbidden unless res.own?(au)
157     res
158   end
159   
160   def self.show_opt
161     {:include => {
162       :author => {}, 
163       :comic => {
164         :author => {}
165       }, 
166       :panel => {
167         :author => {}, 
168         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
169         :speech_balloons =>{:balloons => {}, :speeches => {}}
170       }
171     }}
172   end
173   
174   def elements
175     self.panel.elements
176   end
177   
178   def story_as_json au
179     panel_include = if self.panel and self.panel.visible?(au)
180       {:include => {:author => {}}, :methods => :elements}
181     else
182       {:include => {:author => {}}}
183     end
184     self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
185   end
186   
187   def self.list_as_json_text ary, au
188     '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
189   end
190   
191   def self.licensed_pictures stories
192     r = {}
193     stories.each do |story|
194       r.merge!(story.panel.licensed_pictures) if story.panel
195     end
196     r
197   end
198   
199   def self.new_t comic_id
200     r = Story.max_t(comic_id)
201     r.blank? ? 0 : r.to_i + 1
202   end
203   
204   def self.max_t comic_id
205     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
206   end
207   
208   def self.find_t comic_id, t
209     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
210   end
211   
212   def self.collect_t story
213     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
214     r.map {|s| s.t}
215   end
216   
217   def self.serial? ary
218     i = 0
219     ary.compact.sort.each do |t|
220       break false unless t == i
221       i += 1
222     end
223     ary.compact.size == i
224   end
225   
226   def self.validate_t story
227     Story.serial?(Story.collect_t(story))
228   end
229   
230   def insert_shift
231     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
232   end
233   
234   def lesser_shift old_t
235     self.t = 0 if self.t < 0
236     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
237   end
238   
239   def higher_shift old_t
240     nf = Story.find_t(self.comic_id, self.t)
241     max_t = Story.max_t(self.comic_id).to_i
242     self.t = max_t if self.t > max_t
243     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
244   end
245   
246   def update_shift old_t
247     if self.t > old_t
248       higher_shift old_t
249     else
250       lesser_shift old_t
251     end
252   end
253   
254   def rotate old_t = nil
255     if self.new_record?
256       if self.t.blank?
257         self.t = Story.new_t self.comic_id
258       else
259         self.insert_shift
260       end
261     else
262       if self.t.blank?
263       else
264         self.update_shift old_t
265       end
266     end
267   end
268   
269   def allow?
270     return nil if self.comic_id == nil or self.panel_id == nil
271     self.comic.own?(self.author) and self.panel.usable?(self.author)
272   end
273   
274   def store old_t = nil
275     res = false
276     Story.transaction do
277       case self.allow?
278       when true
279         self.rotate old_t
280       when false
281         raise ActiveRecord::Forbidden
282       else
283       end
284       res = self.save
285       raise ActiveRecord::Rollback unless res
286       res = Story.validate_t(self) 
287       unless res
288         self.errors.add :t, 'unserialized'
289         raise ActiveRecord::Rollback 
290       end
291     end
292     res
293   end
294   
295   def destroy_and_shorten
296     res = false
297     Story.transaction do
298       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
299       raise ActiveRecord::Rollback unless self.destroy
300       res = true
301     end
302     res
303   end
304   
305   
306 end