OSDN Git Service

fix:element remove func failed
[pettanr/pettanr.git] / lib / peta / element_nestable_content.rb
1 module Peta
2   class ElementNestableContent < Content
3     self.abstract_class = true
4     
5     # Dynamic Methods
6     
7     def self.load_manifest
8       super
9       return nil if self._skip_load?
10       # Class Methods
11       # Instance Methods
12       define_method("element_items") do |element_model|
13         self.__send__ self.class.my_manifest.associations.child_element_name(element_model)
14       end
15       define_method("elements_items") do 
16         self.class.child_models.map {|element_model|
17           self.element_items element_model
18         }.flatten
19       end
20     end
21     
22     # Class Methods
23     
24     def self.permit_attributes default_permit_attributes = []
25       attrs = super
26       self.child_models.each do |element_model|
27         name = self.my_manifest.associations.child_element_name(element_model) + '_attributes'
28         child_attrs = {}
29         child_attrs[name] = element_model.permit_attributes(default_permit_attributes)
30         attrs.push child_attrs
31       end
32       attrs
33     end
34     
35     # Instance Methods
36     
37     def boosts level
38       super
39       self.elements_items.each do |item|
40         item.boosts level
41       end
42     end
43     
44     def post_attributes opt = {}
45       attr = self.copy_attributes(opt)
46       associations_attr = {}
47       self.class.child_models.each do |element_model|
48         name = self.class.my_manifest.associations.child_element_name(element_model) + '_attributes'
49         if self.class.my_manifest.associations.has_one?(element_model)
50           e = self.element_items(element_model).post_attributes(opt)
51         else
52           e = {}
53           self.element_items(element_model).each do |element|
54             e[element.post_attribute_key] = element.post_attributes(opt)
55           end
56         end
57         associations_attr[name] = e
58       end
59       attr.merge! associations_attr
60       attr
61     end
62     
63   end
64 end
65