Class: ActiveRecord::TableMetadata

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(klass, arel_table, association = nil, types = klass) ⇒ TableMetadata

Returns a new instance of TableMetadata.


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

def initialize(klass, arel_table, association = nil, types = klass)
  @klass = klass
  @types = types
  @arel_table = arel_table
  @association = association
end

Instance Method Details

#aggregated_with?(aggregation_name) ⇒ Boolean

Returns:

  • (Boolean)

64
65
66
# File 'activerecord/lib/active_record/table_metadata.rb', line 64

def aggregated_with?(aggregation_name)
  klass && reflect_on_aggregation(aggregation_name)
end

#arel_attribute(column_name) ⇒ Object


24
25
26
27
28
29
30
# File 'activerecord/lib/active_record/table_metadata.rb', line 24

def arel_attribute(column_name)
  if klass
    klass.arel_attribute(column_name, arel_table)
  else
    arel_table[column_name]
  end
end

#associated_table(table_name) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'activerecord/lib/active_record/table_metadata.rb', line 44

def associated_table(table_name)
  association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.to_s.singularize)

  if !association && table_name == arel_table.name
    self
  elsif association && !association.polymorphic?
    association_klass = association.klass
    arel_table = association_klass.arel_table.alias(table_name)
    TableMetadata.new(association_klass, arel_table, association)
  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, association, type_caster)
  end
end

#associated_with?(association_name) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'activerecord/lib/active_record/table_metadata.rb', line 40

def associated_with?(association_name)
  klass && klass._reflect_on_association(association_name)
end

#has_column?(column_name) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
# File 'activerecord/lib/active_record/table_metadata.rb', line 36

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

#polymorphic_association?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'activerecord/lib/active_record/table_metadata.rb', line 60

def polymorphic_association?
  association && association.polymorphic?
end

#reflect_on_aggregation(aggregation_name) ⇒ Object


68
69
70
# File 'activerecord/lib/active_record/table_metadata.rb', line 68

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

#resolve_column_aliases(hash) ⇒ Object


14
15
16
17
18
19
20
21
22
# File 'activerecord/lib/active_record/table_metadata.rb', line 14

def resolve_column_aliases(hash)
  new_hash = hash.dup
  hash.each_key do |key|
    if key.is_a?(Symbol) && new_key = klass.attribute_aliases[key.to_s]
      new_hash[new_key] = new_hash.delete(key)
    end
  end
  new_hash
end

#type(column_name) ⇒ Object


32
33
34
# File 'activerecord/lib/active_record/table_metadata.rb', line 32

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