123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::ConnectionAdapters::ReferenceDefinition

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, polymorphic: false, index: true, foreign_key: false, type: :bigint, **options) ⇒ ReferenceDefinition

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 195

def initialize(
  name,
  polymorphic: false,
  index: true,
  foreign_key: false,
  type: :bigint,
  **options
)
  @name = name
  @polymorphic = polymorphic
  @index = index
  @foreign_key = foreign_key
  @type = type
  @options = options

  if polymorphic && foreign_key
    raise ArgumentError, "Cannot add a foreign key to a polymorphic relation"
  end
end

Instance Attribute Details

#foreign_key (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

#index (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

#name (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

#options (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

#polymorphic (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

#type (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 244

attr_reader :name, :polymorphic, :index, :foreign_key, :type, :options

Instance Method Details

#add(table_name, connection)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 215

def add(table_name, connection)
  columns.each do |name, type, options|
    connection.add_column(table_name, name, type, **options)
  end

  if index
    connection.add_index(table_name, column_names, **index_options(table_name))
  end

  if foreign_key
    connection.add_foreign_key(table_name, foreign_table_name, **foreign_key_options)
  end
end

#add_to(table)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 229

def add_to(table)
  columns.each do |name, type, options|
    table.column(name, type, **options)
  end

  if index
    table.index(column_names, **index_options(table.name))
  end

  if foreign_key
    table.foreign_key(foreign_table_name, **foreign_key_options)
  end
end

#as_options(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 246

def as_options(value)
  value.is_a?(Hash) ? value : {}
end

#column_name (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 284

def column_name
  "#{name}_id"
end

#column_names (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 288

def column_names
  columns.map(&:first)
end

#columns (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 276

def columns
  result = [[column_name, type, options]]
  if polymorphic
    result.unshift(["#{name}_type", :string, polymorphic_options])
  end
  result
end

#conditional_options (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 250

def conditional_options
  options.slice(:if_exists, :if_not_exists)
end

#foreign_key_options (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 272

def foreign_key_options
  as_options(foreign_key).merge(column: column_name, **conditional_options)
end

#foreign_table_name (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 292

def foreign_table_name
  foreign_key_options.fetch(:to_table) do
    Base.pluralize_table_names ? name.to_s.pluralize : name
  end
end

#index_options(table_name) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 262

def index_options(table_name)
  index_options = as_options(index).merge(conditional_options)

  # legacy reference index names are used on versions 6.0 and earlier
  return index_options if options[:_uses_legacy_reference_index_name]

  index_options[:name] ||= polymorphic_index_name(table_name) if polymorphic
  index_options
end

#polymorphic_index_name(table_name) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 258

def polymorphic_index_name(table_name)
  "index_#{table_name}_on_#{name}"
end

#polymorphic_options (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 254

def polymorphic_options
  as_options(polymorphic).merge(conditional_options).merge(options.slice(:null, :first, :after))
end