Class: ActiveRecord::TableMetadata
- Inherits:
-
Object
- Object
- ActiveRecord::TableMetadata
- Defined in:
- lib/active_record/table_metadata.rb
Overview
:nodoc:
Instance Method Summary collapse
- #arel_attribute(column_name) ⇒ Object
- #associated_table(table_name) ⇒ Object
- #associated_with?(association_name) ⇒ Boolean
-
#initialize(klass, arel_table, association = nil) ⇒ TableMetadata
constructor
A new instance of TableMetadata.
- #polymorphic_association? ⇒ Boolean
- #resolve_column_aliases(hash) ⇒ Object
- #type(column_name) ⇒ Object
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
24 25 26 27 28 29 30 |
# File '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 'lib/active_record/table_metadata.rb', line 44 def associated_table(table_name) return self if table_name == arel_table.name association = klass._reflect_on_association(table_name) if 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
40 41 42 |
# File 'lib/active_record/table_metadata.rb', line 40 def associated_with?(association_name) klass && klass._reflect_on_association(association_name) end |
#polymorphic_association? ⇒ Boolean
60 61 62 |
# File 'lib/active_record/table_metadata.rb', line 60 def polymorphic_association? association && association.polymorphic? end |
#resolve_column_aliases(hash) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_record/table_metadata.rb', line 12 def resolve_column_aliases(hash) # This method is a hot spot, so for now, use Hash[] to dup the hash. # https://bugs.ruby-lang.org/issues/7166 new_hash = Hash[hash] 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 |