Class: ActiveRecord::Associations::AssociationProxy

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#composite_join_clause(full_keys1, full_keys2) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/composite_primary_keys/associations.rb', line 252

def composite_join_clause(full_keys1, full_keys2)
  full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String)
  full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String)

  where_clause = [full_keys1, full_keys2].transpose.map do |key1, key2|
    "#{key1}=#{key2}"
  end.join(" AND ")

  "(#{where_clause})"
end

#composite_where_clause(full_keys, ids) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/composite_primary_keys/associations.rb', line 233

def composite_where_clause(full_keys, ids)
  full_keys = full_keys.split(CompositePrimaryKeys::ID_SEP) if full_keys.is_a?(String)

  if ids.is_a?(String)
    ids = [[ids]]
  elsif not ids.first.is_a?(Array) # if single comp key passed, turn into an array of 1
    ids = [ids.to_composite_ids]
  end

  where_clause = ids.map do |id_set|
    transposed = id_set.size == 1 ? [[full_keys, id_set.first]] : [full_keys, id_set].transpose
    transposed.map do |full_key, id|
      "#{full_key.to_s}=#{@reflection.klass.sanitize(id)}"
    end.join(" AND ")
  end.join(") OR (")

  "(#{where_clause})"
end

#full_columns_equals(table_name, keys, quoted_ids) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/composite_primary_keys/associations.rb', line 289

def full_columns_equals(table_name, keys, quoted_ids)
  connection = @reflection.active_record.connection
  quoted_table_name = connection.quote_table_name(table_name)
  if keys.is_a?(Symbol) or (keys.is_a?(String) and keys == keys.to_s.split(CompositePrimaryKeys::ID_SEP))
    return "#{quoted_table_name}.#{connection.quote_column_name(keys)} = #{quoted_ids}"
  end
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
  quoted_ids = quoted_ids.split(CompositePrimaryKeys::ID_SEP) if quoted_ids.is_a?(String)
  keys_ids = [keys, quoted_ids].transpose
  keys_ids.collect {|key, id| "(#{quoted_table_name}.#{connection.quote_column_name(key)} = #{id})"}.join(' AND ')
end

#full_composite_join_clause(table1, full_keys1, table2, full_keys2) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/composite_primary_keys/associations.rb', line 263

def full_composite_join_clause(table1, full_keys1, table2, full_keys2)
  connection = @reflection.active_record.connection
  full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String)
  full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String)

  quoted1 = connection.quote_table_name(table1)
  quoted2 = connection.quote_table_name(table2)
  
  where_clause = [full_keys1, full_keys2].transpose.map do |key_pair|
    "#{quoted1}.#{connection.quote_column_name(key_pair.first)}=#{quoted2}.#{connection.quote_column_name(key_pair.last)}"
  end.join(" AND ")

  "(#{where_clause})"
end

#full_keys(table_name, keys) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/composite_primary_keys/associations.rb', line 278

def full_keys(table_name, keys)
  connection = @reflection.active_record.connection
  quoted_table_name = connection.quote_table_name(table_name)
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
  if keys.is_a?(Array) 
    keys.collect {|key| "#{quoted_table_name}.#{connection.quote_column_name(key)}"}.join(CompositePrimaryKeys::ID_SEP) 
  else
    "#{quoted_table_name}.#{connection.quote_column_name(keys)}"
  end
end

#set_belongs_to_association_for(record) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/composite_primary_keys/associations.rb', line 301

def set_belongs_to_association_for(record)
  if @reflection.options[:as]
    record["#{@reflection.options[:as]}_id"]   = @owner.id unless @owner.new_record?
    record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
  else
    key_values = @reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP).zip([@owner.id].flatten)
    key_values.each{|key, value| record[key] = value} unless @owner.new_record?
  end
end