1 class Sheet < Pettanr::Content
6 validates :caption, :presence => true, :length => {:maximum => 100}
7 validates :width, :presence => true, :numericality => true, :natural_number => true
8 validates :height, :presence => true, :numericality => true, :natural_number => true
9 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
10 accepts_nested_attributes_for :sheet_panels, :allow_destroy => true
12 def self.valid_encode_columns
16 def self.each_element_class_names
17 {'SheetPanel' => 'sheet_panels'}.each do |k, n|
22 def self.class_name_to_class k
26 def self.each_element_classes
27 self.each_element_class_names do |k|
28 e = self.class_name_to_class k
33 def elements_by_class_name class_name
34 self.__send__ class_name.tableize
38 self.visible = 0 if self.visible.blank?
41 def overwrite operators
42 return false unless operators.author
43 self.author_id = operators.author.id
46 def visible? operators
47 return false unless super
48 return true if self.new_record?
53 self.visible? operators
60 'sheet' + self.tag_sheet_id
64 self.new_record? ? '0' : self.id.to_s
76 'sheets.updated_at desc'
80 {:sheet_panels => {:panel => {}}, :author => {} }
83 def self.list_json_opt
84 {:include => {:sheet_panels => {:include => {:panel => {}}}, :author => {}}}
88 {:include => {:sheet_panels => {:panel => {}}, :author => {}}}
91 def self.show_json_opt
92 {:include => {:sheet_panels => {:include => {:panel => {}}}, :author => {}}}
97 self.class.each_element_class_names do |k|
98 r += (self.elements_by_class_name(k) || [])
105 self.parts_element.each do |e|
113 self.parts_element.each do |e|
120 self.sheet_elements.size
124 self.sheet_elements.size + 1
127 def self.visible_count
128 Sheet.count 'visible > 0'
131 def self.collect_element_value elements, name
134 if o['_destroy'] or o[:_destroy]
143 def self.validate_serial ary, offset = 0
145 ary.compact.sort.each do |n|
146 break false unless n == i
149 ary.compact.size == i - offset
152 def self.validate_element_serial elements, name, offset = 0
153 Sheet.validate_serial(Sheet.collect_element_value(elements, name), offset)
156 def self.validate_elements_serial c
158 Sheet.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
162 def validate_serial_list
164 self.class.each_element_class_names do |k|
165 l << self.elements_by_class_name(k)
167 # l = [self.sheet_panels]
169 {:elements => l, :name => :t, :offset => 0},
170 {:elements => l, :name => :z, :offset => 1}
175 # r1 = Panel.validate_elements_id validate_id_list
176 Sheet.validate_elements_serial validate_serial_list
180 @new_element_index = 0
181 self.panel_elements.each do |elm|
183 elm.new_index = @new_element_index
184 @new_element_index += 1
189 def store attr, operators
191 self.errors.add :base, I18n.t('errors.invalid_json')
194 self.attributes = attr
195 self.overwrite operators
198 self.sheet_elements.each do |elm|
203 unless validate_child
205 self.errors.add :base, I18n.t('errors.invalid_t')
206 raise ActiveRecord::Rollback
212 def destroy_with_sheet_panel
215 self.sheet_panels.each do |sheet_panel|
216 raise ActiveRecord::Rollback unless sheet_panel.destroy
218 raise ActiveRecord::Rollback unless self.destroy
236 def licensed_pictures
238 self.sheet_elements.each do |elm|
239 r.merge!(elm.licensed_pictures)
246 Sheet.each_element_class_names do |n|
247 self.elements_by_class_name(n).each do |elm|
248 next if elm.copyable?
258 attr = self.copy_attributes
259 Sheet.each_element_class_names do |n|
260 element_attr = Sheet.class_name_to_class(n).panelize(
261 self.elements_by_class_name(n).map {|elm|
262 elm.copy_attributes.merge elm.panel_attributes
265 attr.merge! element_attr
274 r.delete 'created_at'
275 r.delete 'updated_at'
279 def self.panelize sheet
280 attr = sheet.attributes