OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr
[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_panel_size
132     30
133   end
134   
135   def self.max_panel_size
136     200
137   end
138   
139   def self.offset cnt, prm = nil
140     offset = prm.to_i
141     offset = cnt - 1 if offset >= cnt
142     offset = cnt - offset.abs if offset < 0
143     offset = 0 if offset < 0
144     offset
145   end
146   
147   def self.panel_count cnt, prm = self.default_panel_size
148     count = prm.to_i
149     count = self.max_panel_size if count > self.max_panel_size
150     count = self.default_panel_size if count < 1
151     count
152   end
153   
154   def self.list comic, author, offset = 0, limit = Story.default_panel_size
155     opt = self.list_opt
156     opt.merge!({:conditions => ['stories.comic_id = ? and panels.publish > 0', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
157     Story.find(:all, opt)
158   end
159   
160   def self.list_opt
161     {:include => {
162       :author => {}, 
163       :comic => {
164         :author => {}
165       }, 
166       :panel => {
167         :author => {}, 
168         :panel_pictures => {:resource_picture => {:artist => {}}}, 
169         :speech_balloons =>{:balloons => {}, :speeches => {}}
170       }
171     }}
172   end
173   
174   def self.list_json_opt
175     {:include => {
176       :author => {}, 
177       :comic => {
178         :author => {}
179       }, 
180       :panel => {
181         :author => {}, 
182         :panel_pictures => {:resource_picture => {:artist => {}}}, 
183         :speech_balloons =>{:balloons => {}, :speeches => {}}
184       }
185     }}
186   end
187   
188   def to_json_list
189     self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
190   end
191   
192   
193 end