Module: ActiveRecordWhereAssoc::ActiveRecordCompat

Defined in:
lib/active_record_where_assoc/active_record_compat.rb

Defined Under Namespace

Classes: JoinKeys

Class Method Summary collapse

Class Method Details

.chained_reflection_and_chained_constraints(reflection) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 45

def self.chained_reflection_and_chained_constraints(reflection)
  pairs = reflection.chain.map do |ref|
    # PolymorphicReflection is a super weird thing. Like a partial reflection, I don't get it.
    # Seems like just bypassing it works for our needs.
    # When doing a has_many through that has a polymorphic source and a source_type, this ends up
    # part of the chain instead of the regular HasManyReflection that one would expect.
    ref = ref.instance_variable_get(:@reflection) if ref.is_a?(ActiveRecord::Reflection::PolymorphicReflection)

    [ref, ref.constraints]
  end

  pairs.transpose
end

.join_keys(reflection, poly_belongs_to_klass) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 7

def self.join_keys(reflection, poly_belongs_to_klass)
  if poly_belongs_to_klass
    JoinKeys.new(reflection.join_primary_key(poly_belongs_to_klass), reflection.join_foreign_key)
  else
    JoinKeys.new(reflection.join_primary_key, reflection.join_foreign_key)
  end
end

.normalize_association_name(association_name) ⇒ Object



76
77
78
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 76

def self.normalize_association_name(association_name)
  association_name.to_s
end

.null_relation?(reflection) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 96

def self.null_relation?(reflection)
  reflection.null_relation?
end

.parent_reflection(reflection) ⇒ Object



65
66
67
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 65

def self.parent_reflection(reflection)
  reflection.parent_reflection
end

.through_reflection?(reflection) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 86

def self.through_reflection?(reflection)
  reflection.through_reflection?
end