OSDN Git Service

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