Class: ActiveRecord::TableMetadata

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TableMetadata.



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

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

Instance Method Details

#arel_attribute(column_name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/active_record/table_metadata.rb', line 22

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



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

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
    return self
  elsif association && !association.polymorphic?
    association_klass = association.klass
    arel_table = association_klass.arel_table.alias(table_name)
  else
    type_caster = TypeCaster::Connection.new(klass, table_name)
    association_klass = nil
    arel_table = Arel::Table.new(table_name, type_caster: type_caster)
  end

  TableMetadata.new(association_klass, arel_table, association)
end

#associated_with?(association_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/active_record/table_metadata.rb', line 42

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

#has_column?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/active_record/table_metadata.rb', line 38

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

#polymorphic_association?Boolean

Returns:

  • (Boolean)


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

def polymorphic_association?
  association && association.polymorphic?
end

#resolve_column_aliases(hash) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/active_record/table_metadata.rb', line 12

def resolve_column_aliases(hash)
  new_hash = hash.dup
  hash.each do |key, _|
    if (key.is_a?(Symbol)) && klass.attribute_alias?(key)
      new_hash[klass.attribute_alias(key)] = new_hash.delete(key)
    end
  end
  new_hash
end

#type(column_name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/active_record/table_metadata.rb', line 30

def type(column_name)
  if klass
    klass.type_for_attribute(column_name.to_s)
  else
    Type.default_value
  end
end