OSDN Git Service

fix form
[pettanr/pettanr.git] / app / models / story.rb
1 #ストーリー
2 class Story < Peta::Content
3   has_many :story_sheets
4   belongs_to :comic
5   
6   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
7   validates :title, :presence => true, :length => {:maximum => 100}
8   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   def tag_attributes column = nil, opt = {}
12     {
13     }
14   end
15   
16   def self.owner_model
17     Comic
18   end
19   
20   def self.valid_encode_columns
21     super + ['title', 'description']
22   end
23   
24   def supply_default
25     self.comic_id = nil
26     self.visible = 0 if self.visible.blank?
27     self.t = nil
28   end
29   
30   def overwrite
31   end
32   
33   def visible? operators
34     return false unless super
35     self.owner_model.visible? operators
36   end
37   
38   def disp_t
39     self.t + 1
40   end
41   
42   def disp_t_by_text
43     I18n.t('stories.show.t', :t => self.disp_t)
44   end
45   
46   def title_with_t
47     self.disp_t_by_text  + ':' + self.title
48   end
49   
50   def story_sheets_count
51     StorySheet.where(['story_sheets.story_id = ?', self.id]).count
52   end
53   
54   def self.list_where
55     'stories.visible > 0'
56   end
57   
58   def self.list_order
59     'stories.updated_at desc'
60   end
61   
62   def self.list_opt
63     {:comic => {:author => {}} }
64   end
65   
66   def self.list_json_opt
67     {:include => {:comic => {:include => {:author => {}}} }}
68   end
69   
70   def self.show_opt
71     {:include => {:comic => {:author => {}} }}
72   end
73   
74   def self.show_json_opt
75     {:include => {:comic => {:include => {:author => {}}} }}
76   end
77   
78   def self.visible_count
79     Story.count 'visible > 0'
80   end
81   
82   def destroy_with_story_sheet
83     res = false
84     Story.transaction do
85       self.story_sheets.each do |story_sheet|
86         raise ActiveRecord::Rollback unless story_sheet.destroy
87       end
88       raise ActiveRecord::Rollback unless self.destroy
89       res = true
90     end
91     res
92   end
93   
94   def self.new_t comic_id
95     r = Story.max_t(comic_id)
96     r.blank? ? 0 : r.to_i + 1
97   end
98   
99   def self.max_t comic_id
100     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
101   end
102   
103   def self.find_t comic_id, t
104     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
105   end
106   
107   def self.collect_t story
108     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
109     r.map {|s| s.t}
110   end
111   
112   def self.serial? ary
113     i = 0
114     ary.compact.sort.each do |t|
115       break false unless t == i
116       i += 1
117     end
118     ary.compact.size == i
119   end
120   
121   def self.validate_t story
122     Story.serial?(Story.collect_t(story))
123   end
124   
125   def insert_shift
126     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
127   end
128   
129   def lesser_shift old_t
130     self.t = 0 if self.t < 0
131     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
132   end
133   
134   def higher_shift old_t
135     nf = Story.find_t(self.comic_id, self.t)
136     max_t = Story.max_t(self.comic_id).to_i
137     self.t = max_t if self.t > max_t
138     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
139   end
140   
141   def update_shift old_t
142     if self.t > old_t
143       higher_shift old_t
144     else
145       lesser_shift old_t
146     end
147   end
148   
149   def rotate old_t = nil
150     if self.new_record?
151       if self.t.blank?
152         self.t = Story.new_t self.comic_id
153       else
154         self.insert_shift
155       end
156     else
157       if self.t.blank?
158       else
159         self.update_shift old_t
160       end
161     end
162   end
163   
164   def allow? operators
165     return nil if self.comic_id == nil
166     self.comic.own?(operators)
167   end
168   
169   def store operators, old_t = nil
170     res = false
171     Story.transaction do
172       case self.allow? operators
173       when true
174         self.rotate old_t
175       when false
176         raise ActiveRecord::Forbidden
177       else
178       end
179       res = self.save
180       raise ActiveRecord::Rollback unless res
181       res = Story.validate_t(self) 
182       unless res
183         self.errors.add :t, 'unserialized'
184         raise ActiveRecord::Rollback 
185       end
186     end
187     res
188   end
189   
190   def destroy_and_shorten
191     res = false
192     Story.transaction do
193       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
194       raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
195       res = true
196     end
197     res
198   end
199   
200   
201 end