Class: ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

Inherits:
JoinBase
  • Object
show all
Defined in:
lib/composite_primary_keys/associations.rb

Instance Method Summary collapse

Methods inherited from JoinBase

#aliased_primary_key, #column_names_with_alias, #record_id

Instance Method Details

#association_joinObject



119
120
121
122
123
# File 'lib/composite_primary_keys/associations.rb', line 119

def association_join
  reflection.active_record.composite? ?
    composite_association_join :
    single_association_join
end

#composite_association_joinObject



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/composite_primary_keys/associations.rb', line 125

def composite_association_join
  join = case reflection.macro
    when :has_and_belongs_to_many
      " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
         table_alias_for(options[:join_table], aliased_join_table_name),
         full_keys(aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key),
         full_keys(reflection.active_record.table_name, reflection.active_record.primary_key)] +
      " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
         table_name_and_alias, 
         full_keys(aliased_table_name, klass.primary_key),
         full_keys(aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key)
         ]
    when :has_many, :has_one
      case
        when reflection.macro == :has_many && reflection.options[:through]
          through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
          if through_reflection.options[:as] # has_many :through against a polymorphic join
            raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
          else
            if source_reflection.macro == :has_many && source_reflection.options[:as]
              raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
            else
              case source_reflection.macro
                when :belongs_to
                  first_key  = primary_key
                  second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
                when :has_many
                  first_key  = through_reflection.klass.to_s.classify.foreign_key
                  second_key = options[:foreign_key] || primary_key
              end
              
              " LEFT OUTER JOIN %s ON (%s) = (%s) "  % [
                table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), 
                full_keys(aliased_join_table_name, through_reflection.primary_key_name),
                full_keys(parent.aliased_table_name, parent.primary_key)] +
              " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
                table_name_and_alias,
                full_keys(aliased_table_name, first_key), 
                full_keys(aliased_join_table_name, second_key)
              ]
            end
          end
        
        when reflection.macro == :has_many && reflection.options[:as]
          raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
        when reflection.macro == :has_one && reflection.options[:as]
          raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
        else
          foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
          " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
            table_name_and_alias,
            full_keys(aliased_table_name, foreign_key),
            full_keys(parent.aliased_table_name, parent.primary_key)
          ]
      end
    when :belongs_to
      " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
         table_name_and_alias, 
         full_keys(aliased_table_name, reflection.klass.primary_key),
         full_keys(parent.aliased_table_name, options[:foreign_key] || klass.to_s.foreign_key)
        ]
    else
      ""
  end || ''
  join << %(AND %s.%s = %s ) % [
    aliased_table_name, 
    reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), 
    klass.quote(klass.name)] unless klass.descends_from_active_record?
  join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
  join
end

#full_keys(table_name, keys) ⇒ Object



197
198
199
# File 'lib/composite_primary_keys/associations.rb', line 197

def full_keys(table_name, keys)
  keys.collect {|key| "#{table_name}.#{key}"}.join(CompositePrimaryKeys::ID_SEP)
end

#single_association_joinObject



118
# File 'lib/composite_primary_keys/associations.rb', line 118

alias single_association_join association_join