123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::ModelSchema::SchemaContext

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: activerecord/lib/active_record/model_schema/schema_context.rb

Overview

SchemaContext owns all schema-derived state for a model: columns, attribute types, and column defaults.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(model_class) ⇒ SchemaContext

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 42

def initialize(model_class)
  @model_class = model_class
  @schema_loaded = false
  @attributes_key = :"active_record_schema_attributes_#{object_id}"
end

Instance Attribute Details

#column_names (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 39

attr_reader :model_class, :columns_hash, :columns, :column_names,
            :content_columns

#columns (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 39

attr_reader :model_class, :columns_hash, :columns, :column_names,
            :content_columns

#columns_hash (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 39

attr_reader :model_class, :columns_hash, :columns, :column_names,
            :content_columns

#content_columns (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 39

attr_reader :model_class, :columns_hash, :columns, :column_names,
            :content_columns

#model_class (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 39

attr_reader :model_class, :columns_hash, :columns, :column_names,
            :content_columns

#schema_loaded?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 87

def schema_loaded?
  @schema_loaded
end

Instance Method Details

#_returning_columns_for_insert(connection)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 60

def _returning_columns_for_insert(connection)
  auto_populated_columns = columns.filter_map do |c|
    -c.name if connection.return_value_after_insert?(c)
  end

  (auto_populated_columns.empty? ? Array(primary_key) : auto_populated_columns).freeze
end

#_returning_columns_for_update(connection)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 68

def _returning_columns_for_update(connection)
  columns.filter_map do |c|
    c.name if connection.return_value_after_update?(c)
  end.freeze
end

#attribute_set

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 91

def attribute_set
  attributes_hash = model_class.columns_hash.transform_values do |column|
    ActiveModel::Attribute.from_database(column.name, column.default, model_class.type_for_column(column))
  end
  ActiveModel::AttributeSet.new(attributes_hash)
end

#attributes

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 48

def attributes
  ActiveSupport::Ractors[@attributes_key] ||= Attributes.new(self)
end

#cached_find_by_statement(connection, key, &block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 74

def cached_find_by_statement(connection, key, &block) # :nodoc:
  cache = find_by_statement_cache[connection.prepared_statements]
  cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
end

#find_by_statement_cache

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 83

def find_by_statement_cache # :nodoc:
  ActiveSupport::Ractors[model_class.find_by_statement_cache_key] || initialize_find_by_cache
end

#freeze

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 98

def freeze
  load_schema!
  super
end

#initialize_find_by_cache

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 79

def initialize_find_by_cache # :nodoc:
  ActiveSupport::Ractors[model_class.find_by_statement_cache_key] = { true => Concurrent::Map.new, false => Concurrent::Map.new }
end

#load_schema!

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 103

def load_schema!
  return if @schema_loaded

  unless table_name
    raise ActiveRecord::TableNotSpecified, "#{model_class} has no table configured. Set one with #{model_class}.table_name="
  end

  columns_hash = model_class.connection_pool.schema_cache.columns_hash(table_name)
  if model_class.only_columns.present?
    columns_hash = columns_hash.slice(*model_class.only_columns)
  elsif model_class.ignored_columns.present?
    columns_hash = columns_hash.except(*model_class.ignored_columns)
  end
  @columns_hash = columns_hash.freeze

  @columns = @columns_hash.values.freeze
  @column_names = @columns.map(&:name).freeze

  @content_columns = @columns.reject do |c|
    Array(primary_key).include?(c.name) ||
    c.name == model_class.inheritance_column ||
    c.name.end_with?("_id", "_count")
  end.freeze

  model_class.make_pending_attribute_modifications_shareable
  attributes

  @schema_loaded = true
end

#primary_key

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 56

def primary_key
  model_class.primary_key
end

#table_name

[ GitHub ]

  
# File 'activerecord/lib/active_record/model_schema/schema_context.rb', line 52

def table_name
  model_class.table_name
end