OSDN Git Service

fix filter include
[pettanr/pettanr.git] / lib / inspire.rb
1 module ElementInspire
2   def self.included(base)
3     base.extend(ClassMethods)
4     base.__send__ :include, InstanceMethods
5   end
6   
7   module ClassMethods
8     def panelize elements_attributes
9       elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
10       hash = {}
11       index = 0
12       elements_attributes.each do |element_attributes|
13         hash[self.to_s.tableize + '_attributes'] ||= {}
14         n = if element_attributes['id']
15           element_attributes['id'].to_s
16         else
17           index += 1
18           'new' + index.to_s 
19         end
20         hash[self.to_s.tableize + '_attributes'][n] = element_attributes
21       end
22       hash
23     end
24   end
25   
26   module InstanceMethods
27     private
28     
29     public
30     
31     def parts
32       @parts ||= []
33     end
34     
35     def copy_attributes
36       r = self.attributes
37       r.delete 'id'
38       r.delete 'panel_id'
39       r.delete 'created_at'
40       r.delete 'updated_at'
41       self.parts.each do |part|
42         new_part_attr = part ? part.class.panelize(part.copy_attributes) : {}
43         r.merge! new_part_attr
44       end
45       r
46     end
47     
48     def copy to_panel
49       new_element = self.class.new self.copy_attributes
50       new_element.t = to_panel.new_t
51       new_element.z = to_panel.new_z
52       new_attr = new_element.class.panelize new_element.copy_attributes
53       r = {} #Panel.panelize to_panel
54       r.merge! new_attr
55       r
56     end
57     
58     def copy_all index
59       new_element = self.class.new self.copy_attributes
60       new_attr = new_element.class.panelize(new_element.copy_attributes, index)
61       r = {} #Panel.panelize to_panel
62       r.merge! new_attr
63       r
64     end
65     
66   end
67   
68 end
69