Class: ActiveRecord::Associations::AssociationProxy

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

Overview

:nodoc:

Direct Known Subclasses

HasManyThroughAssociation

Instance Method Summary collapse

Instance Method Details

#composite_join_clause(full_keys1, full_keys2) ⇒ Object



240
241
242
243
244
245
246
247
# File 'lib/composite_primary_keys/associations.rb', line 240

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 |key_pair|
    "#{key_pair.first}=#{key_pair.last}"
  end.join(" AND ")
  "(#{where_clause})"
end

#composite_where_clause(full_keys, ids) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/composite_primary_keys/associations.rb', line 224

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



256
257
258
259
260
261
262
263
264
# File 'lib/composite_primary_keys/associations.rb', line 256

def full_columns_equals(table_name, keys, quoted_ids)
  if keys.is_a?(Symbol) or (keys.is_a?(String) and keys == keys.to_s.split(CompositePrimaryKeys::ID_SEP))
    return "#{table_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| "(#{table_name}.#{key} = #{id})"}.join(' AND ')
end

#full_keys(table_name, keys) ⇒ Object



249
250
251
252
253
254
# File 'lib/composite_primary_keys/associations.rb', line 249

def full_keys(table_name, keys)
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
  keys.is_a?(Array) ?
    keys.collect {|key| "#{table_name}.#{key}"}.join(CompositePrimaryKeys::ID_SEP) : 
    "#{table_name}.#{keys}"
end