OSDN Git Service

pull
[pettanr/pettanr.git] / app / models / sheet_panel.rb
1 class SheetPanel < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :sheet
5   accepts_nested_attributes_for :panel, :allow_destroy => true
6   #alias_attribute :attr_y, :y
7   
8   validates :sheet_id, :numericality => {:allow_blank => true}
9   validates :panel_id, :numericality => {:allow_blank => true}
10   validates :x, :presence => true, :numericality => true
11   validates :y, :presence => true, :numericality => true
12   validates :z, :presence => true, :numericality => {:greater_than => 0}
13   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14   
15   def y
16     self.attributes['y']
17   end
18   
19   def supply_default
20     self.x = 15
21     self.y = 15
22     if self.sheet
23       self.t = self.sheet.new_t 
24       self.z = self.sheet.new_z 
25     else
26       self.sheet_id = nil
27       self.panel_id = nil
28       self.z = 1
29       self.t = nil
30     end
31   end
32   
33   def self.by_author_list_includes
34     {
35       :sheet => {
36         :author => {}
37       }
38     }
39   end
40   
41   def self.show_opt
42     {:include => {
43       :sheet => {
44         :author => {}
45       }, 
46       :panel => {
47         :author => {}, 
48         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
49         :speech_balloons =>{:balloon => {}, :speech => {}}
50       }
51     }}
52   end
53   
54   def elements
55     self.panel.elements
56   end
57   
58   def sheet_panel_as_json au
59     panel_include = if self.panel and self.panel.visible?(au)
60       {:include => {}, :methods => :elements}
61     else
62       {:include => {}}
63     end
64     self.to_json({:include => {:sheet => {:include => {:author => {}}}, :panel => panel_include}})
65   end
66   
67   def self.list_as_json_text ary, au
68     '[' + ary.map {|i| i.sheet_panel_as_json(au) }.join(',') + ']'
69   end
70   
71   def licensed_pictures
72     if self.panel
73       self.panel.licensed_pictures
74     else
75       {}
76     end
77   end
78   
79   def copyable?
80     if self.panel and self.panel.publish?
81       true
82     else
83       false
84     end
85   end
86   
87   def allow? operators
88     return nil if self.sheet_id == nil or self.panel_id == nil
89     self.sheet.own?(operators) and self.panel.usable?(operators)
90   end
91   
92   def store operators, old_t = nil
93     res = false
94     self.class.transaction do
95       case self.allow? operators
96       when true
97         self.rotate old_t
98       when false
99         raise ActiveRecord::Forbidden
100       else
101       end
102       res = self.save
103       raise ActiveRecord::Rollback unless res
104       res = self.class.validate_t(self.sheet_id) 
105       unless res
106         self.errors.add :t, 'unserialized'
107         raise ActiveRecord::Rollback 
108       end
109     end
110     res
111   end
112   
113 end