OSDN Git Service

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