Skip to content

Commit dfaee03

Browse files
ccutrerbyroot
authored andcommitted
Define column methods on the ColumnMethods module
The way they were being defined inside the `included` hook meant they ended up being defined directly on TableDefinition, contrary to the documentation: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQL/ColumnMethods.html I left `define_column_methods` as a class method that gets extended onto TableDefinition, since there's at least one gem that relies on being able to call it there to define their own column types
1 parent 2b26fd0 commit dfaee03

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

activerecord/lib/active_record/connection_adapters.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def resolve(adapter_name) # :nodoc:
8484
autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
8585
autoload :IndexDefinition
8686
autoload :ColumnDefinition
87+
autoload :ColumnMethods
8788
autoload :ChangeColumnDefinition
8889
autoload :ChangeColumnDefaultDefinition
8990
autoload :ForeignKeyDefinition

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ def foreign_table_name
299299
module ColumnMethods
300300
extend ActiveSupport::Concern
301301

302+
class_methods do
303+
private
304+
def define_column_methods(*column_types) # :nodoc:
305+
column_types.each do |column_type|
306+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
307+
def #{column_type}(*names, **options)
308+
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
309+
names.each { |name| column(name, :#{column_type}, **options) }
310+
end
311+
RUBY
312+
end
313+
end
314+
end
315+
extend ClassMethods
316+
302317
# Appends a primary key definition to the table definition.
303318
# Can be called multiple times, but this is probably not a good idea.
304319
def primary_key(name, type = :primary_key, **options)
@@ -316,27 +331,11 @@ def primary_key(name, type = :primary_key, **options)
316331
#
317332
# See TableDefinition#column
318333

319-
included do
320-
define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal,
321-
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual
334+
define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal,
335+
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual
322336

323-
alias :blob :binary
324-
alias :numeric :decimal
325-
end
326-
327-
class_methods do
328-
def define_column_methods(*column_types) # :nodoc:
329-
column_types.each do |column_type|
330-
module_eval <<-RUBY, __FILE__, __LINE__ + 1
331-
def #{column_type}(*names, **options)
332-
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
333-
names.each { |name| column(name, :#{column_type}, **options) }
334-
end
335-
RUBY
336-
end
337-
end
338-
private :define_column_methods
339-
end
337+
alias :blob :binary
338+
alias :numeric :decimal
340339
end
341340

342341
# = Active Record Connection Adapters \Table \Definition

activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module ConnectionAdapters
55
module MySQL
66
module ColumnMethods
77
extend ActiveSupport::Concern
8+
extend ConnectionAdapters::ColumnMethods::ClassMethods
89

910
##
1011
# :method: blob
@@ -42,13 +43,11 @@ module ColumnMethods
4243
# :method: unsigned_bigint
4344
# :call-seq: unsigned_bigint(*names, **options)
4445

45-
included do
46-
define_column_methods :blob, :tinyblob, :mediumblob, :longblob,
47-
:tinytext, :mediumtext, :longtext, :unsigned_integer, :unsigned_bigint,
48-
:unsigned_float, :unsigned_decimal
46+
define_column_methods :blob, :tinyblob, :mediumblob, :longblob,
47+
:tinytext, :mediumtext, :longtext, :unsigned_integer, :unsigned_bigint,
48+
:unsigned_float, :unsigned_decimal
4949

50-
deprecate :unsigned_float, :unsigned_decimal, deprecator: ActiveRecord.deprecator
51-
end
50+
deprecate :unsigned_float, :unsigned_decimal, deprecator: ActiveRecord.deprecator
5251
end
5352

5453
# = Active Record MySQL Adapter \Table Definition

activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module ConnectionAdapters
55
module PostgreSQL
66
module ColumnMethods
77
extend ActiveSupport::Concern
8+
extend ConnectionAdapters::ColumnMethods::ClassMethods
89

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

184-
included do
185-
define_column_methods :bigserial, :bit, :bit_varying, :cidr, :citext, :daterange,
186-
:hstore, :inet, :interval, :int4range, :int8range, :jsonb, :ltree, :macaddr,
187-
:money, :numrange, :oid, :point, :line, :lseg, :box, :path, :polygon, :circle,
188-
:serial, :tsrange, :tstzrange, :tsvector, :uuid, :xml, :timestamptz, :enum
189-
end
185+
define_column_methods :bigserial, :bit, :bit_varying, :cidr, :citext, :daterange,
186+
:hstore, :inet, :interval, :int4range, :int8range, :jsonb, :ltree, :macaddr,
187+
:money, :numrange, :oid, :point, :line, :lseg, :box, :path, :polygon, :circle,
188+
:serial, :tsrange, :tstzrange, :tsvector, :uuid, :xml, :timestamptz, :enum
190189
end
191190

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

0 commit comments

Comments
 (0)