OSDN Git Service

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