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

.has_unique_index?(model, column_names) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 116

def self.has_unique_index?(model, column_names)
  column_names = Array(column_names).map(&:to_s)
  @unique_indexes_cache.fetch([model, column_names]) do |k|
    unique_indexes = indexes(model).select(&:unique)
    columns_names_set = Set.new(column_names)

    # We check for an index whose columns are a subset of the columns we specify
    # This way, a composite column_names will find uniqueness if just a single of the column is unique
    @unique_indexes_cache[k] = unique_indexes.any? { |ui| Set.new(ui.columns) <= columns_names_set }
  end
end

.indexes(model) ⇒ Object



106
107
108
# File 'lib/active_record_where_assoc/active_record_compat.rb', line 106

def self.indexes(model)
  model.connection.schema_cache.indexes(model.table_name)
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