diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-09 13:53:24 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-09 14:10:35 +0900 |
commit | c7721ea6b3a2319820decccce1a26eb9ebf3b290 (patch) | |
tree | c1e7ceddd7c0677e81bdbd3a4a3d4fb4460e37d7 /activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | |
parent | 25d8afb3a6e5cdc7da3ab0bb6f18c2b14fcabbdd (diff) | |
download | rails-c7721ea6b3a2319820decccce1a26eb9ebf3b290.tar.gz rails-c7721ea6b3a2319820decccce1a26eb9ebf3b290.tar.bz2 rails-c7721ea6b3a2319820decccce1a26eb9ebf3b290.zip |
Refactor to extract defining column methods as `define_column_methods`
It makes to ease to handle all short-hand methods (e.g. validates
arguments etc).
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index b2a6109548..dfb4953cbd 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -198,41 +198,43 @@ module ActiveRecord end module ColumnMethods + extend ActiveSupport::Concern + # 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) column(name, type, options.merge(primary_key: true)) end + ## + # :method: column + # :call-seq: column(name, type, options = {}) + # # Appends a column or columns of a specified type. # # t.string(:goat) # t.string(:goat, :sheep) # # See TableDefinition#column - [ - :bigint, - :binary, - :boolean, - :date, - :datetime, - :decimal, - :float, - :integer, - :json, - :string, - :text, - :time, - :timestamp, - :virtual, - ].each do |column_type| - module_eval <<-CODE, __FILE__, __LINE__ + 1 - def #{column_type}(*args, **options) - args.each { |name| column(name, :#{column_type}, options) } + + included do + define_column_methods :bigint, :binary, :boolean, :date, :datetime, :decimal, + :float, :integer, :json, :string, :text, :time, :timestamp, :virtual + + alias :numeric :decimal + end + + 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) + names.each { |name| column(name, :#{column_type}, options) } + end + RUBY end - CODE + end end - alias_method :numeric, :decimal end # Represents the schema of an SQL table in an abstract way. This class |