OSDN Git Service

fix foreign_filter list includes
[pettanr/pettanr.git] / lib / manifest / model / association.rb
1 ManifestBase.require_modules "manifest/model/association/", 
2   %w|belongs_to has_many has_one|
3
4 module Manifest
5   module ModelModule
6     class Association < ManifestBase::Values
7       include AssociationModule
8       
9       attr :belongs_to, :has_many, :has_one
10       
11       def set_default
12         super
13         @values['belongs_to'] ||= {}
14         @values['has_many'] ||= {}
15         @values['has_one'] ||= {}
16       end
17       
18       def init
19         super
20         @belongs_to = ManifestBase.load_name_values self, @values, 'belongs_to', BelongsTo
21         @has_many = ManifestBase.load_name_values self, @values, 'has_many', HasMany
22         @has_one = ManifestBase.load_name_values self, @values, 'has_one', HasOne
23       end
24       
25       def model_name
26         @parent.name
27       end
28       
29       def each_parent_model
30         @belongs_to.each do |name, belongs_to_manifest|
31           next unless @parent.classify.element?
32           if @parent.classify.my_peta.type == 'element'
33             next unless belongs_to_manifest.model.my_peta.type == 'root'
34           end
35           if @parent.classify.my_peta.type == 'leaf'
36             next unless belongs_to_manifest.model.my_peta.type == 'binder'
37           end
38           yield belongs_to_manifest.model
39         end
40       end
41       
42       def child_element_name child_model
43         if @has_one[child_model.item_name]
44           child_model.item_name
45         else
46           child_model.table_name
47         end
48       end
49       
50     end
51     
52   end
53 end