Class: Og::JoinsMany

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

Overview

A ‘joins_many’ relation. This objects is associated with an other using an intermediate join table.

Examples

joins_many Category joins_many :categories, Category

Direct Known Subclasses

ManyToMany

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, 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



22
23
24
25
26
27
28
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
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
114
115
116
117
118
119
120
# File 'lib/og/relation/joins_many.rb', line 22

def enchant
  self[:owner_singular_name] = owner_class.to_s.demodulize.underscore.downcase 
  self[:target_singular_name] = target_plural_name.to_s.singular

  store = owner_class.ogmanager.store

  # handle schema_inheritance
  
  join_class = owner_class
  if sclass = owner_class.ann.self[:superclass]
    join_class = sclass
  end
  join_table_info = store.join_table_info(join_class, target_class)

  if through = self[:through] 
    # A custom class is used for the join. Use the class 
    # table and don't create a new table.
    through = Relation.symbol_to_class(through, owner_class) if through.is_a?(Symbol)
    join_table = self[:join_table] = through.table
  else    
    # Calculate the name of the join table
    join_table = self[:join_table] = join_table_info[:table]
    # Create a join table.
    owner_class.ann :self, :join_tables => [] if owner_class.ann.self.join_tables.nil?
    owner_class.ann.self.join_tables! << join_table_info
  end
  
  owner_key = join_table_info[:owner_key]
  target_key = join_table_info[:target_key]

  owner_class.module_eval %{ 
    attr_accessor :#{target_plural_name}

    def #{target_plural_name}(options = nil)
      reload = options and options[:reload]

      unless @#{target_plural_name}
        @#{target_plural_name} = JoinsManyCollection.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)
      obj.save if obj.unsaved?
      obj.class.ogmanager.store.join(self, obj, "#{join_table}", options)
    end

    def remove_#{target_singular_name}(obj)
      obj.class.ogmanager.store.unjoin(self, obj, "#{join_table}")
    end

    def find_#{target_plural_name}(options = {})
      find_options = {
        :join_table => "#{join_table}",
        :join_condition => "#{join_table}.#{target_key}=\#{#{target_class}::OGTABLE}.oid",
        :condition => "#{join_table}.#{owner_key}=\#\{#{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      

    #--
    # TODO: optimize this!!
    #++
          
    def count_#{target_plural_name}
      find_options = {
        :join_table => "#{join_table}",
        :join_condition => "#{join_table}.#{target_key}=\#{#{target_class}::OGTABLE}.oid",
        :condition => "#{join_table}.#{owner_key}=\#\{#{owner_class.primary_key}\}"          
      }
      #{target_class}.find(find_options).size
    end
  }
  
  if through
    owner_class.module_eval %{
      def #{target_singular_name}_join_data(t)
        #{through}.find_one(:condition => "#{owner_key}=\#{#{owner_class.primary_key}} and #{target_key}=\#{t.pk}")
      end
    }
  end    
end