OSDN Git Service

7ee2e9f6457ac91c78b94ce65254508db10bcd4f
[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     # Instance Methods
25     
26     def boosts level
27       super
28       self.elements_items.each do |item|
29         item.boosts level
30       end
31     end
32     
33     def post_attributes opt = {}
34       attr = self.copy_attributes(opt)
35       associations_attr = {}
36       self.class.child_models.each do |element_model|
37         name = self.class.my_manifest.associations.child_element_name(element_model) + '_attributes'
38         if self.class.my_manifest.associations.has_one?(element_model)
39           e = self.element_items(element_model).post_attributes(opt)
40         else
41           e = {}
42           self.element_items(element_model).each do |element|
43             e[element.post_attribute_key] = element.post_attributes(opt)
44           end
45         end
46         associations_attr[name] = e
47       end
48       attr.merge! associations_attr
49       attr
50     end
51     
52   end
53 end
54