Module: VersionedRecord::CompositePredicates
- Defined in:
- lib/versioned_record/composite_predicates.rb
Instance Method Summary collapse
-
#cpk_join_predicate(association_table, association_key, table, key) ⇒ Object
Lets say location belongs to a company and company is versioned.
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)
28 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 |
# 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 # We don't handle Polymorphic associations at this stage if !association.[:polymorphic] if association.reflection.klass.versioned? eq_predicates << association_table[:is_current_version].eq(true) end end when :has_and_belongs_to_many if association.reflection.klass.versioned? if association.reflection.klass.table_name == association_table.name eq_predicates << association_table[:is_current_version].eq(true) end end when :has_many, :has_one if association.reflection.klass.versioned? if association.reflection.klass.table_name == association_table.name eq_predicates << association_table[:is_current_version].eq(true) end end end cpk_and_predicate(eq_predicates) else super end end |