OSDN Git Service

fix peta test
[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 self.panelize elements_attributes
80       elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
81       hash = {}
82       index = 0
83       elements_attributes.each do |element_attributes|
84         hash[self.to_s.tableize + '_attributes'] ||= {}
85         n = if element_attributes['id']
86           element_attributes['id'].to_s
87         else
88           index += 1
89           'new' + index.to_s 
90         end
91         hash[self.to_s.tableize + '_attributes'][n] = element_attributes
92       end
93       hash
94     end
95     
96     def copy_attributes
97       r = self.attributes
98       r.delete 'id'
99       r.delete 'sheet_id'
100       r.delete 'panel_id'   # create panel
101       r.delete 'created_at'
102       r.delete 'updated_at'
103       r
104     end
105   
106   def copyable?
107     if self.panel and self.panel.publish? == false
108       false
109     else
110       true
111     end
112   end
113   
114   def panel_attributes
115     if self.panel
116       {'panel_attributes' => self.panel.copy}
117     else
118       {}
119     end
120   end
121   
122   def allow? operators
123     return nil if self.sheet_id == nil or self.panel_id == nil
124     self.sheet.own?(operators) and self.panel.usable?(operators)
125   end
126   
127   def store operators, old_t = nil
128     res = false
129     self.class.transaction do
130       case self.allow? operators
131       when true
132         self.rotate old_t
133       when false
134         raise ActiveRecord::Forbidden
135       else
136       end
137       res = self.save
138       raise ActiveRecord::Rollback unless res
139       res = self.class.validate_t(self.sheet_id) 
140       unless res
141         self.errors.add :t, 'unserialized'
142         raise ActiveRecord::Rollback 
143       end
144     end
145     res
146   end
147   
148 end