Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define column methods on the ColumnMethods module #51730

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions activerecord/lib/active_record/connection_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def resolve(adapter_name) # :nodoc:
autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
autoload :IndexDefinition
autoload :ColumnDefinition
autoload :ColumnMethods
autoload :ChangeColumnDefinition
autoload :ChangeColumnDefaultDefinition
autoload :ForeignKeyDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ def foreign_table_name
module ColumnMethods
extend ActiveSupport::Concern

class_methods do
private

def define_column_methods(*column_types) # :nodoc:
column_types.each do |column_type|
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{column_type}(*names, **options)
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
names.each { |name| column(name, :#{column_type}, **options) }
end
RUBY
end
end
end
extend ClassMethods

# Appends a primary key definition to the table definition.
# Can be called multiple times, but this is probably not a good idea.
def primary_key(name, type = :primary_key, **options)
Expand All @@ -317,27 +333,11 @@ def primary_key(name, type = :primary_key, **options)
#
# See TableDefinition#column

included do
define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal,
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual
define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal,
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual

alias :blob :binary
alias :numeric :decimal
end

class_methods do
def define_column_methods(*column_types) # :nodoc:
column_types.each do |column_type|
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{column_type}(*names, **options)
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
names.each { |name| column(name, :#{column_type}, **options) }
end
RUBY
end
end
private :define_column_methods
end
alias :blob :binary
alias :numeric :decimal
end

# = Active Record Connection Adapters \Table \Definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ConnectionAdapters
module MySQL
module ColumnMethods
extend ActiveSupport::Concern
extend ConnectionAdapters::ColumnMethods::ClassMethods

##
# :method: blob
Expand Down Expand Up @@ -50,11 +51,9 @@ module ColumnMethods
# :method: unsigned_decimal
# :call-seq: unsigned_decimal(*names, **options)

included do
define_column_methods :blob, :tinyblob, :mediumblob, :longblob,
:tinytext, :mediumtext, :longtext, :unsigned_integer, :unsigned_bigint,
:unsigned_float, :unsigned_decimal
end
define_column_methods :blob, :tinyblob, :mediumblob, :longblob,
:tinytext, :mediumtext, :longtext, :unsigned_integer, :unsigned_bigint,
:unsigned_float, :unsigned_decimal
end

# = Active Record MySQL Adapter \Table Definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ConnectionAdapters
module PostgreSQL
module ColumnMethods
extend ActiveSupport::Concern
extend ConnectionAdapters::ColumnMethods::ClassMethods

# Defines the primary key field.
# Use of the native PostgreSQL UUID type is supported, and can be used
Expand Down Expand Up @@ -181,12 +182,10 @@ def primary_key(name, type = :primary_key, **options)
# :method: enum
# :call-seq: enum(*names, **options)

included do
define_column_methods :bigserial, :bit, :bit_varying, :cidr, :citext, :daterange,
:hstore, :inet, :interval, :int4range, :int8range, :jsonb, :ltree, :macaddr,
:money, :numrange, :oid, :point, :line, :lseg, :box, :path, :polygon, :circle,
:serial, :tsrange, :tstzrange, :tsvector, :uuid, :xml, :timestamptz, :enum
end
define_column_methods :bigserial, :bit, :bit_varying, :cidr, :citext, :daterange,
:hstore, :inet, :interval, :int4range, :int8range, :jsonb, :ltree, :macaddr,
:money, :numrange, :oid, :point, :line, :lseg, :box, :path, :polygon, :circle,
:serial, :tsrange, :tstzrange, :tsvector, :uuid, :xml, :timestamptz, :enum
end

ExclusionConstraintDefinition = Struct.new(:table_name, :expression, :options) do
Expand Down