Module: VersionedRecord::CompositePredicates

Defined in:
lib/versioned_record/composite_predicates.rb

Instance Method Summary collapse

Instance Method Details

#cpk_join_predicate(association_table, association_key, table, key) ⇒ Object

Lets say location belongs to a company and company is versioned

class Company < ActiveRecord::Base
  include VersionedRecord
  has_many :locations
end

class Location < ActiveRecord::Base
  belongs_to :company
end

When we want to load the company from the location we do:

location.company

This will do:

Company.where(id: location.company.id, is_current_version: true)

Parameters:

  • association_table (Arel::Table)

    the associated_table (in the example, company)

  • association_key (Array, String)

    the value of the pkey of associated_table (in the example, company)

  • table (Arel::Table)

    (in the example, location)

  • key (Array, String)

    reference to the association



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/versioned_record/composite_predicates.rb', line 28

def cpk_join_predicate(association_table, association_key, table, key)
  fields             = Array(key).map { |key| table[key] }
  association_fields = Array(association_key).map { |key| association_table[key] }

  if fields.size == 1
    eq_predicates = [ association_fields[0].eq(fields[0]) ]
    case association.reflection.macro
      when :belongs_to
        add_belongs_to_predicates!(eq_predicates, association_table)
      when :has_and_belongs_to_many
        add_habtm_predicates!(eq_predicates, association, association_table)
      when :has_many, :has_one
        add_has_x_predicates!(eq_predicates, association, association_table)
    end
    cpk_and_predicate(eq_predicates)
  else
    super
  end
end