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



29
30
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
# File 'lib/og/relation/has_many.rb', line 29

def enchant
  self[:owner_singular_name] = owner_class.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 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



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

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