Class: ActiveRecord::TableMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/table_metadata.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, arel_table, reflection = nil) ⇒ TableMetadata

Returns a new instance of TableMetadata.



7
8
9
10
11
# File 'lib/active_record/table_metadata.rb', line 7

def initialize(klass, arel_table, reflection = nil)
  @klass = klass
  @arel_table = arel_table
  @reflection = reflection
end

Instance Attribute Details

#arel_tableObject (readonly)

Returns the value of attribute arel_table.



89
90
91
# File 'lib/active_record/table_metadata.rb', line 89

def arel_table
  @arel_table
end

Instance Method Details

#associated_table(table_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record/table_metadata.rb', line 38

def associated_table(table_name)
  reflection = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.singularize)

  if !reflection && table_name == arel_table.name
    return self
  end

  if reflection
    association_klass = reflection.klass unless reflection.polymorphic?
  elsif block_given?
    association_klass = yield table_name
  end

  if association_klass
    arel_table = association_klass.arel_table
    arel_table = arel_table.alias(table_name) if arel_table.name != table_name
    TableMetadata.new(association_klass, arel_table, reflection)
  else
    type_caster = TypeCaster::Connection.new(klass, table_name)
    arel_table = Arel::Table.new(table_name, type_caster: type_caster)
    TableMetadata.new(nil, arel_table, reflection)
  end
end

#associated_with?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/table_metadata.rb', line 25

def associated_with?(table_name)
  if reflection = klass&._reflect_on_association(table_name)
    reflection
  elsif ActiveRecord.allow_deprecated_singular_associations_name && reflection = klass&._reflect_on_association(table_name.singularize)
    ActiveRecord.deprecator.warn(<<~MSG)
      Referring to a singular association (e.g. `#{reflection.name}`) by its plural name (e.g. `#{reflection.plural_name}`) is deprecated.

      To convert this deprecation warning to an error and enable more performant behavior, set config.active_record.allow_deprecated_singular_associations_name = false.
    MSG
    reflection
  end
end

#has_column?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/active_record/table_metadata.rb', line 21

def has_column?(column_name)
  klass&.columns_hash&.key?(column_name)
end

#polymorphic_association?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/active_record/table_metadata.rb', line 62

def polymorphic_association?
  reflection&.polymorphic?
end

#polymorphic_name_associationObject



66
67
68
# File 'lib/active_record/table_metadata.rb', line 66

def polymorphic_name_association
  reflection&.polymorphic_name
end

#predicate_builderObject



79
80
81
82
83
84
85
86
87
# File 'lib/active_record/table_metadata.rb', line 79

def predicate_builder
  if klass
    predicate_builder = klass.predicate_builder.dup
    predicate_builder.instance_variable_set(:@table, self)
    predicate_builder
  else
    PredicateBuilder.new(self)
  end
end

#primary_keyObject



13
14
15
# File 'lib/active_record/table_metadata.rb', line 13

def primary_key
  klass&.primary_key
end

#reflect_on_aggregation(aggregation_name) ⇒ Object Also known as: aggregated_with?



74
75
76
# File 'lib/active_record/table_metadata.rb', line 74

def reflect_on_aggregation(aggregation_name)
  klass&.reflect_on_aggregation(aggregation_name)
end

#through_association?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_record/table_metadata.rb', line 70

def through_association?
  reflection&.through_reflection?
end

#type(column_name) ⇒ Object



17
18
19
# File 'lib/active_record/table_metadata.rb', line 17

def type(column_name)
  arel_table.type_for_attribute(column_name)
end