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 :categories joins_many Category joins_many :categories, MyCategory

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


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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/og/relation/joins_many.rb', line 35

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(self)
  
  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] = store.table(through)
    
    tmp_join_class = through
  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 => [] unless owner_class.ann.self.join_tables?
    owner_class.ann.self.join_tables! << join_table_info
    
    # Create temporary classes to be able to use ann.descendants to cascade
    # delete joins_many relations when one side of the relation is removed.
    
    tmp_join_class_name = "OgTempJ_#{join_table}"
    
    unless self.class.constants.include?(tmp_join_class_name)
      tmp_join_class = self.class.const_set(tmp_join_class_name, Class.new)
      tmp_join_class.const_set('OGTABLE', join_table)
    else
      tmp_join_class = self.class.const_get(tmp_join_class_name)
    end
    
  end
  
  owner_key = join_table_info[:owner_key]
  target_key = join_table_info[:target_key]
  
  # Add join class to ann.descendants to be able to use cascade deletes.
  
  unless owner_class.ann.self[:descendants]
    owner_class.ann(:self, :descendants => [])
  end
  owner_class.ann.self.descendants! << [tmp_join_class, owner_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      

    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}.count(find_options)
    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

#resolve_polymorphicObject

– FIXME: better enchant polymorphic parents and then add an alias to :objects. ++


29
30
31
32
33
# File 'lib/og/relation/joins_many.rb', line 29

def resolve_polymorphic
  target_class.module_eval %{
    joins_many :#{owner_class.to_s.demodulize.underscore.pluralize}
  }
end