Class: Og::HasMany

Inherits:
Relation show all
Defined in:
lib/og/relation/has_many.rb

Overview

A 'has_many' relation. There should be a respective 'belongs_to' relation.

Examples

article.comments << Comment.new article.comments.size

Instance Attribute Summary

Attributes inherited from Relation

#options

Instance Method Summary collapse

Methods inherited from Relation

#[], #[]=, enchant, #initialize, #method_missing, #polymorphic?, #polymorphic_marker?, resolve, resolve_names, resolve_polymorphic_markers, resolve_polymorphic_relations, resolve_targets, symbol_to_class, #to_s

Constructor Details

This class inherits a constructor from Og::Relation

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Og::Relation

Instance Method Details

#enchantObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/og/relation/has_many.rb', line 31

def enchant
  self[:owner_singular_name] = if owner_class.schema_inheritance?
    owner_class.schema_inheritance_root_class
  else
    owner_class
  end.to_s.demodulize.underscore.downcase
  
  self[:target_singular_name] = target_plural_name.to_s.singular
  self[:foreign_key] = "#{foreign_name || owner_singular_name}_#{owner_class.primary_key}"
  # gmosx: DON'T set self[:foreign_field]
  foreign_field = self[:foreign_field] || self[:foreign_key]
  
  owner_class.module_eval %{ 
    attr_accessor :#{target_plural_name}

    def #{target_plural_name}(options = nil)
      unless @#{target_plural_name}
        @#{target_plural_name} = HasManyCollection.new(
          self, 
          #{target_class},
          :add_#{target_singular_name},
          :remove_#{target_singular_name},
          :find_#{target_plural_name},
          :count_#{target_plural_name},
          options
        )
      end
      
      @#{target_plural_name}.find_options = options
      @#{target_plural_name}.reload(options) if options and options[:reload]
      @#{target_plural_name}
    end
    
    def #{target_plural_name}=(*args)
      options = args.last.is_a?(Hash) ? args.pop : nil
      
      if args.size == 1 && args.first.is_a?(HasManyCollection)
        @#{target_plural_name} = args.first
        @#{target_plural_name}.find_options = options if options
        
        return @#{target_plural_name}
      end
      
      args.flatten!
      
      args.each do |obj|
        add_#{target_singular_name}(obj, options)
      end
      
      return #{target_plural_name}
    end

    def add_#{target_singular_name}(obj, options = nil)
      return unless obj
      # save the object if needed to generate a primary_key.
      self.save unless self.saved?
      obj.#{foreign_key} = @#{owner_class.primary_key}
      obj.save
    end

    def remove_#{target_singular_name}(obj)
      obj.#{foreign_key} = nil
      obj.save
    end
    
    def find_#{target_plural_name}(options = {})
      find_options = {
        :condition => "#{foreign_field} = \#{og_quote(#{owner_class.primary_key})}"
      }
      if options
        if condition = options.delete(:condition)
          find_options[:condition] += " AND (\#{condition})"
        end        
        find_options.update(options)
      end
      #{target_class}.find(find_options)
    end
    
    def count_#{target_plural_name}
      #{target_class}.count(:condition => "#{foreign_field} = \#{og_quote(#{owner_class.primary_key})}")        
    end
  }
end

#resolve_polymorphicObject


23
24
25
26
27
28
29
# File 'lib/og/relation/has_many.rb', line 23

def resolve_polymorphic
  unless target_class.relations.empty?
    unless target_class.relations.find { |r| r.is_a?(BelongsTo) and  r.target_class == owner_class }
      target_class.belongs_to(owner_class)      
    end
  end
end