OSDN Git Service

classname rename to module_name
[pettanr/pettanr.git] / app / models / sheet_panel.rb
1 class SheetPanel < Peta::Element
2   load_manifest
3   belongs_to :author
4   belongs_to :panel
5   belongs_to :sheet
6   accepts_nested_attributes_for :panel, :allow_destroy => true
7   
8   validates :sheet_id, :numericality => {:allow_blank => true}
9   validates :panel_id, :numericality => {:allow_blank => true}
10   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
11   validates :x, :presence => true, :numericality => true
12   validates :y, :presence => true, :numericality => true
13   validates :z, :presence => true, :numericality => {:greater_than => 0}
14   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15   
16   def supply_default
17     self.x = 15
18     self.y = 15
19     if self.sheet
20       self.t = self.sheet.new_t 
21       self.z = self.sheet.new_z 
22     else
23       self.sheet_id = nil
24       self.panel_id = nil
25       self.z = 1
26       self.t = nil
27     end
28   end
29   
30   def overwrite operators
31     return false unless operators.author
32     self.author_id = operators.author.id
33   end
34   
35   def self.by_author_list_includes
36     {
37       :sheet => {
38         :author => {}
39       }
40     }
41   end
42   
43   def self.show_opt
44     {:include => {
45       :author => {}, 
46       :sheet => {
47         :author => {}
48       }, 
49       :panel => {
50         :author => {}, 
51         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
52         :speech_balloons =>{:balloon => {}, :speech => {}}
53       }
54     }}
55   end
56   
57   def elements
58     self.panel.elements
59   end
60   
61   def sheet_panel_as_json au
62     panel_include = if self.panel and self.panel.visible?(au)
63       {:include => {:author => {}}, :methods => :elements}
64     else
65       {:include => {:author => {}}}
66     end
67     self.to_json({:include => {:sheet => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
68   end
69   
70   def self.list_as_json_text ary, au
71     '[' + ary.map {|i| i.sheet_panel_as_json(au) }.join(',') + ']'
72   end
73   
74   def licensed_pictures
75     if self.panel
76       self.panel.licensed_pictures
77     else
78       {}
79     end
80   end
81   
82     def self.panelize elements_attributes
83       elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
84       hash = {}
85       index = 0
86       elements_attributes.each do |element_attributes|
87         hash[self.to_s.tableize + '_attributes'] ||= {}
88         n = if element_attributes['id']
89           element_attributes['id'].to_s
90         else
91           index += 1
92           'new' + index.to_s 
93         end
94         hash[self.to_s.tableize + '_attributes'][n] = element_attributes
95       end
96       hash
97     end
98     
99     def copy_attributes
100       r = self.attributes
101       r.delete 'id'
102       r.delete 'sheet_id'
103       r.delete 'panel_id'   # create panel
104       r.delete 'created_at'
105       r.delete 'updated_at'
106       r
107     end
108   
109   def copyable?
110     if self.panel and self.panel.publish? == false
111       false
112     else
113       true
114     end
115   end
116   
117   def panel_attributes
118     if self.panel
119       {'panel_attributes' => self.panel.copy}
120     else
121       {}
122     end
123   end
124   
125   def allow? operators
126     return nil if self.sheet_id == nil or self.panel_id == nil
127     self.sheet.own?(operators) and self.panel.usable?(operators)
128   end
129   
130   def store operators, old_t = nil
131     res = false
132     self.class.transaction do
133       case self.allow? operators
134       when true
135         self.rotate old_t
136       when false
137         raise ActiveRecord::Forbidden
138       else
139       end
140       res = self.save
141       raise ActiveRecord::Rollback unless res
142       res = self.class.validate_t(self.sheet_id) 
143       unless res
144         self.errors.add :t, 'unserialized'
145         raise ActiveRecord::Rollback 
146       end
147     end
148     res
149   end
150   
151 end