OSDN Git Service

fix template manifest
[pettanr/pettanr.git] / app / models / comic_story.rb
1 class ComicStory < Peta::Leaf
2   load_manifest
3   belongs_to :author
4   belongs_to :comic
5   belongs_to :story
6   
7   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   def supply_default
13     self.comic_id = nil
14     self.story_id = nil
15     self.t = nil
16   end
17   
18   def overwrite operators
19     return false unless operators.author
20     self.author_id = operators.author.id
21   end
22   
23   def disp_t
24     self.t + 1
25   end
26   
27   def disp_t_by_text
28     I18n.t('comic_stories.show.t', :t => self.disp_t)
29   end
30   
31   def title
32     self.disp_t_by_text + ':' + self.story.title
33   end
34   
35   def self.public_list_where
36     'comics.visible > 0'
37   end
38   
39   def self.list_opt
40     {
41       :comic => {
42         :author => {}, 
43       }
44     }
45   end
46   
47   def self.list_json_opt
48     {:include => {
49       :comic => {
50         :author => {}, 
51       }
52     }}
53   end
54   
55   def self.show_opt
56     {:include => {
57       :comic => {
58       }
59     }}
60   end
61   
62   def allow? operators
63     return nil if self.story_id == nil or self.comic_id == nil
64     self.comic.own?(operators) and self.story.own?(operators)
65   end
66   
67   def store operators, old_t = nil
68     res = false
69     self.class.transaction do
70       case self.allow? operators
71       when true
72         self.rotate old_t
73       when false
74         raise ActiveRecord::Forbidden
75       else
76       end
77       res = self.save
78       raise ActiveRecord::Rollback unless res
79       res = self.class.validate_t(self.comic_id) 
80       unless res
81         self.errors.add :t, 'unserialized'
82         raise ActiveRecord::Rollback 
83       end
84     end
85     res
86   end
87   
88 end