Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation

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

Instance Method Summary collapse

Instance Method Details

#construct_sqlObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb', line 4

def construct_sql
  if @reflection.options[:finder_sql]
    @finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
  else
    # CPK
    # @finder_sql = "#{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{owner_quoted_id} "
    @finder_sql = full_columns_equals(@reflection.options[:join_table], @reflection.cpk_primary_key, owner_quoted_id)
    @finder_sql << " AND (#{conditions})" if conditions
  end

  join_condition = if composite?
    conditions = Array.new
    primary_keys.length.times do |i|
      conditions << "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key[i]} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key[i]}"
    end
    conditions.join(' AND ')
  else
    "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
  end
  #@join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
  @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON (#{join_condition})"

  construct_counter_sql
end

#insert_record(record, force = true, validate = true) ⇒ Object



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
# File 'lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb', line 29

def insert_record(record, force = true, validate = true)
  unless record.persisted?
    if force
      record.save!
    else
      return false unless record.save(:validate => validate)
    end
  end

  if @reflection.options[:insert_sql]
    @owner.connection.insert(interpolate_and_sanitize_sql(@reflection.options[:insert_sql], record))
  else
    relation   = Arel::Table.new(@reflection.options[:join_table])
    timestamps = record_timestamp_columns(record)
    timezone   = record.send(:current_time_from_proper_timezone) if timestamps.any?

    # CPK
    #attributes = Hash[columns.map do |column|
    #  name = column.name
    #  value = case name.to_s
    #    when @reflection.primary_key_name.to_s
    #      @owner.id
    #    when @reflection.association_foreign_key.to_s
    #      record.id
    #    when *timestamps
    #      timezone
    #    else
    #      @owner.send(:quote_value, record[name], column) if record.has_attribute?(name)
    #  end
    #  [relation[name], value] unless value.nil?
    #end]

    # CPK
    owner_foreign_keys = @reflection.cpk_primary_key.map{|key| key.to_s}
    association_foreign_keys = Array(@reflection.association_foreign_key).map{|key| key.to_s}

    attributes = Hash[columns.map do |column|
      name = column.name.to_s
      value = case
        when owner_foreign_keys.include?(name)
          index = owner_foreign_keys.index(name)
          primary_keys = Array(@owner.class.primary_key)
          primary_key = primary_keys[index]
          @owner[primary_key]
        when association_foreign_keys.include?(name)
          index = association_foreign_keys.index(name)
          primary_keys = Array(@reflection.klass.primary_key)
          primary_key = primary_keys[index]
          record[primary_key]
        when timestamps.include?(name)
          timezone
        else
          @owner.send(:quote_value, record[name], column) if record.has_attribute?(name)
      end
      [relation[name], value] unless value.nil?
    end]

    relation.insert(attributes)
  end

  return true
end