OSDN Git Service

add story
[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 au
13     self.t = nil
14     return false unless au
15     self.author_id = au.id
16   end
17   
18   def own? author
19     return false unless author
20     self.author_id == author.id
21   end
22   
23   def self.show sid, au = nil
24     res = Story.find sid
25     if au
26       raise ActiveRecord::Forbidden unless res.own?(au)
27     end
28     res
29   end
30   
31   def self.new_t comic_id
32     r = Story.max_t(comic_id)
33     r.blank? ? 0 : r.to_i + 1
34   end
35   
36   def self.max_t comic_id
37     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
38   end
39   
40   def self.find_t comic_id, t
41     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
42   end
43   
44   def self.collect_t story
45     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
46     r << story if story.new_record?
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     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
69   end
70   
71   def higher_shift old_t
72     nf = Story.find_t(self.comic_id, self.t)
73     max_t = Story.max_t(self.comic_id).to_i
74     self.t = max_t if self.t > max_t
75     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
76   end
77   
78   def update_shift old_t
79     if self.t > old_t
80       higher_shift old_t
81     else
82       lesser_shift old_t
83     end
84   end
85   
86   def rotate old_t = nil
87     if self.new_record?
88       if self.t.blank?
89         self.t = Story.new_t self.comic_id
90       else
91         self.insert_shift
92       end
93     else
94       if self.t.blank?
95       else
96         self.update_shift old_t
97       end
98     end
99   end
100   
101   def allow?
102     c = self.comic
103     pl = self.panel
104     c and c.own?(self.author) and pl and pl.usable?(self.author)
105   end
106   
107   def store old_t = nil
108     res = false
109     raise ActiveRecord::Forbidden unless self.allow?
110     Story.transaction do
111       self.rotate old_t
112       res = self.save
113       raise ActiveRecord::Rollback unless res
114       res = Story.validate_t(self) 
115       unless res
116         self.errors.add :t, 'unserialized'
117         raise ActiveRecord::Rollback 
118       end
119     end
120     res
121   end
122   
123   def destroy_and_shorten
124     Story.transaction do
125       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
126       raise ActiveRecord::Rollback unless self.destroy
127     end
128   end
129   
130   def self.default_panel_size
131     30
132   end
133   
134   def self.max_panel_size
135     200
136   end
137   
138   def self.offset cnt, prm = nil
139     offset = prm.to_i
140     offset = cnt - 1 if offset >= cnt
141     offset = cnt - offset.abs if offset < 0
142     offset = 0 if offset < 0
143     offset
144   end
145   
146   def self.panel_count cnt, prm = self.default_panel_size
147     count = prm.to_i
148     count = self.max_panel_size if count > self.max_panel_size
149     count = self.default_panel_size if count < 1
150     count
151   end
152   
153   def self.list comic, author, offset = 0, limit = Story.default_panel_size
154     opt = self.list_opt
155     opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
156     Story.find(:all, opt)
157   end
158   
159   def self.list_opt
160     {:include => {
161       :author => {}, 
162       :comic => {
163         :author => {}
164       }, 
165       :panel => {
166         :author => {}, 
167         :panel_pictures => {:resource_picture => {:artist => {}}}, 
168         :speech_balloons =>{:balloons => {}, :speeches => {}}
169       }
170     }}
171   end
172   
173   def self.list_json_opt
174     {:include => {
175       :author => {}, 
176       :comic => {
177         :author => {}
178       }, 
179       :panel => {
180         :author => {}, 
181         :panel_pictures => {:resource_picture => {:artist => {}}}, 
182         :speech_balloons =>{:balloons => {}, :speeches => {}}
183       }
184     }}
185   end
186   
187   def to_json_list
188     self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
189   end
190   
191   
192 end