提交 657520fa 编写于 作者: J Jean Boussier

Fix most Ruby 2.7 warnings in Active Record 6.0

上级 52a13732
...@@ -476,7 +476,7 @@ GEM ...@@ -476,7 +476,7 @@ GEM
actionpack (>= 4.0) actionpack (>= 4.0)
activesupport (>= 4.0) activesupport (>= 4.0)
sprockets (>= 3.0.0) sprockets (>= 3.0.0)
sqlite3 (1.4.1) sqlite3 (1.4.2)
stackprof (0.2.13) stackprof (0.2.13)
sucker_punch (2.1.2) sucker_punch (2.1.2)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
......
...@@ -166,7 +166,7 @@ def self.start_application # :nodoc: ...@@ -166,7 +166,7 @@ def self.start_application # :nodoc:
def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities) def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities)
driver_options = { using: using, screen_size: screen_size, options: options } driver_options = { using: using, screen_size: screen_size, options: options }
self.driver = SystemTesting::Driver.new(driver, driver_options, &capabilities) self.driver = SystemTesting::Driver.new(driver, **driver_options, &capabilities)
end end
driven_by :selenium driven_by :selenium
......
...@@ -1857,7 +1857,7 @@ def destroy_associations ...@@ -1857,7 +1857,7 @@ def destroy_associations
hm_options[k] = options[k] if options.key? k hm_options[k] = options[k] if options.key? k
end end
has_many name, scope, hm_options, &extension has_many name, scope, **hm_options, &extension
_reflections[name.to_s].parent_reflection = habtm_reflection _reflections[name.to_s].parent_reflection = habtm_reflection
end end
end end
......
...@@ -49,7 +49,7 @@ def reload(*) ...@@ -49,7 +49,7 @@ def reload(*)
# +to+ When passed, this method will return false unless the value was # +to+ When passed, this method will return false unless the value was
# changed to the given value # changed to the given value
def saved_change_to_attribute?(attr_name, **options) def saved_change_to_attribute?(attr_name, **options)
mutations_before_last_save.changed?(attr_name.to_s, options) mutations_before_last_save.changed?(attr_name.to_s, **options)
end end
# Returns the change to an attribute during the last save. If the # Returns the change to an attribute during the last save. If the
...@@ -99,7 +99,7 @@ def saved_changes ...@@ -99,7 +99,7 @@ def saved_changes
# +to+ When passed, this method will return false unless the value will be # +to+ When passed, this method will return false unless the value will be
# changed to the given value # changed to the given value
def will_save_change_to_attribute?(attr_name, **options) def will_save_change_to_attribute?(attr_name, **options)
mutations_from_database.changed?(attr_name.to_s, options) mutations_from_database.changed?(attr_name.to_s, **options)
end end
# Returns the change to an attribute that will be persisted during the # Returns the change to an attribute that will be persisted during the
......
...@@ -314,7 +314,7 @@ def destroy #:nodoc: ...@@ -314,7 +314,7 @@ def destroy #:nodoc:
@_destroy_callback_already_called = false @_destroy_callback_already_called = false
end end
def touch(*) #:nodoc: def touch(*, **) #:nodoc:
_run_touch_callbacks { super } _run_touch_callbacks { super }
end end
......
...@@ -27,7 +27,7 @@ def visit_AlterTable(o) ...@@ -27,7 +27,7 @@ def visit_AlterTable(o)
end end
def visit_ColumnDefinition(o) def visit_ColumnDefinition(o)
o.sql_type = type_to_sql(o.type, o.options) o.sql_type = type_to_sql(o.type, **o.options)
column_sql = +"#{quote_column_name(o.name)} #{o.sql_type}" column_sql = +"#{quote_column_name(o.name)} #{o.sql_type}"
add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
column_sql column_sql
......
...@@ -139,7 +139,8 @@ def initialize( ...@@ -139,7 +139,8 @@ def initialize(
def add_to(table) def add_to(table)
columns.each do |column_options| columns.each do |column_options|
table.column(*column_options) kwargs = column_options.extract_options!
table.column(*column_options, **kwargs)
end end
if index if index
...@@ -199,7 +200,7 @@ module ColumnMethods ...@@ -199,7 +200,7 @@ module ColumnMethods
# Appends a primary key definition to the table definition. # Appends a primary key definition to the table definition.
# Can be called multiple times, but this is probably not a good idea. # Can be called multiple times, but this is probably not a good idea.
def primary_key(name, type = :primary_key, **options) def primary_key(name, type = :primary_key, **options)
column(name, type, options.merge(primary_key: true)) column(name, type, **options.merge(primary_key: true))
end end
## ##
...@@ -226,7 +227,7 @@ def primary_key(name, type = :primary_key, **options) ...@@ -226,7 +227,7 @@ def primary_key(name, type = :primary_key, **options)
module_eval <<-RUBY, __FILE__, __LINE__ + 1 module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{column_type}(*names, **options) def #{column_type}(*names, **options)
raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty? raise ArgumentError, "Missing column name(s) for #{column_type}" if names.empty?
names.each { |name| column(name, :#{column_type}, options) } names.each { |name| column(name, :#{column_type}, **options) }
end end
RUBY RUBY
end end
...@@ -375,7 +376,7 @@ def column(name, type, **options) ...@@ -375,7 +376,7 @@ def column(name, type, **options)
index_options = options.delete(:index) index_options = options.delete(:index)
index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options
@columns_hash[name] = new_column_definition(name, type, options) @columns_hash[name] = new_column_definition(name, type, **options)
self self
end end
...@@ -408,8 +409,8 @@ def timestamps(**options) ...@@ -408,8 +409,8 @@ def timestamps(**options)
options[:precision] = 6 options[:precision] = 6
end end
column(:created_at, :datetime, options) column(:created_at, :datetime, **options)
column(:updated_at, :datetime, options) column(:updated_at, :datetime, **options)
end end
# Adds a reference. # Adds a reference.
...@@ -421,7 +422,7 @@ def timestamps(**options) ...@@ -421,7 +422,7 @@ def timestamps(**options)
# See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use. # See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.
def references(*args, **options) def references(*args, **options)
args.each do |ref_name| args.each do |ref_name|
ReferenceDefinition.new(ref_name, options).add_to(self) ReferenceDefinition.new(ref_name, **options).add_to(self)
end end
end end
alias :belongs_to :references alias :belongs_to :references
...@@ -479,7 +480,7 @@ def drop_foreign_key(name) ...@@ -479,7 +480,7 @@ def drop_foreign_key(name)
def add_column(name, type, options) def add_column(name, type, options)
name = name.to_s name = name.to_s
type = type.to_sym type = type.to_sym
@adds << AddColumnDefinition.new(@td.new_column_definition(name, type, options)) @adds << AddColumnDefinition.new(@td.new_column_definition(name, type, **options))
end end
end end
...@@ -540,7 +541,7 @@ def initialize(table_name, base) ...@@ -540,7 +541,7 @@ def initialize(table_name, base)
# See TableDefinition#column for details of the options you can use. # See TableDefinition#column for details of the options you can use.
def column(column_name, type, **options) def column(column_name, type, **options)
index_options = options.delete(:index) index_options = options.delete(:index)
@base.add_column(name, column_name, type, options) @base.add_column(name, column_name, type, **options)
index(column_name, index_options.is_a?(Hash) ? index_options : {}) if index_options index(column_name, index_options.is_a?(Hash) ? index_options : {}) if index_options
end end
...@@ -550,7 +551,7 @@ def column(column_name, type, **options) ...@@ -550,7 +551,7 @@ def column(column_name, type, **options)
# #
# See {connection.column_exists?}[rdoc-ref:SchemaStatements#column_exists?] # See {connection.column_exists?}[rdoc-ref:SchemaStatements#column_exists?]
def column_exists?(column_name, type = nil, options = {}) def column_exists?(column_name, type = nil, options = {})
@base.column_exists?(name, column_name, type, options) @base.column_exists?(name, column_name, type, **options)
end end
# Adds a new index to the table. +column_name+ can be a single Symbol, or # Adds a new index to the table. +column_name+ can be a single Symbol, or
...@@ -662,7 +663,7 @@ def rename(column_name, new_column_name) ...@@ -662,7 +663,7 @@ def rename(column_name, new_column_name)
# See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use. # See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.
def references(*args, **options) def references(*args, **options)
args.each do |ref_name| args.each do |ref_name|
@base.add_reference(name, ref_name, options) @base.add_reference(name, ref_name, **options)
end end
end end
alias :belongs_to :references alias :belongs_to :references
...@@ -675,7 +676,7 @@ def references(*args, **options) ...@@ -675,7 +676,7 @@ def references(*args, **options)
# See {connection.remove_reference}[rdoc-ref:SchemaStatements#remove_reference] # See {connection.remove_reference}[rdoc-ref:SchemaStatements#remove_reference]
def remove_references(*args, **options) def remove_references(*args, **options)
args.each do |ref_name| args.each do |ref_name|
@base.remove_reference(name, ref_name, options) @base.remove_reference(name, ref_name, **options)
end end
end end
alias :remove_belongs_to :remove_references alias :remove_belongs_to :remove_references
...@@ -686,8 +687,8 @@ def remove_references(*args, **options) ...@@ -686,8 +687,8 @@ def remove_references(*args, **options)
# t.foreign_key(:authors, column: :author_id, primary_key: "id") # t.foreign_key(:authors, column: :author_id, primary_key: "id")
# #
# See {connection.add_foreign_key}[rdoc-ref:SchemaStatements#add_foreign_key] # See {connection.add_foreign_key}[rdoc-ref:SchemaStatements#add_foreign_key]
def foreign_key(*args) def foreign_key(*args, **options)
@base.add_foreign_key(name, *args) @base.add_foreign_key(name, *args, **options)
end end
# Removes the given foreign key from the table. # Removes the given foreign key from the table.
...@@ -696,8 +697,8 @@ def foreign_key(*args) ...@@ -696,8 +697,8 @@ def foreign_key(*args)
# t.remove_foreign_key(column: :author_id) # t.remove_foreign_key(column: :author_id)
# #
# See {connection.remove_foreign_key}[rdoc-ref:SchemaStatements#remove_foreign_key] # See {connection.remove_foreign_key}[rdoc-ref:SchemaStatements#remove_foreign_key]
def remove_foreign_key(*args) def remove_foreign_key(*args, **options)
@base.remove_foreign_key(name, *args) @base.remove_foreign_key(name, *args, **options)
end end
# Checks to see if a foreign key exists. # Checks to see if a foreign key exists.
...@@ -705,8 +706,8 @@ def remove_foreign_key(*args) ...@@ -705,8 +706,8 @@ def remove_foreign_key(*args)
# t.foreign_key(:authors) unless t.foreign_key_exists?(:authors) # t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)
# #
# See {connection.foreign_key_exists?}[rdoc-ref:SchemaStatements#foreign_key_exists?] # See {connection.foreign_key_exists?}[rdoc-ref:SchemaStatements#foreign_key_exists?]
def foreign_key_exists?(*args) def foreign_key_exists?(*args, **options)
@base.foreign_key_exists?(name, *args) @base.foreign_key_exists?(name, *args, **options)
end end
end end
end end
......
...@@ -292,7 +292,7 @@ def primary_key(table_name) ...@@ -292,7 +292,7 @@ def primary_key(table_name)
# #
# See also TableDefinition#column for details on how to create columns. # See also TableDefinition#column for details on how to create columns.
def create_table(table_name, **options) def create_table(table_name, **options)
td = create_table_definition(table_name, options) td = create_table_definition(table_name, **options)
if options[:id] != false && !options[:as] if options[:id] != false && !options[:as]
pk = options.fetch(:primary_key) do pk = options.fetch(:primary_key) do
...@@ -302,7 +302,7 @@ def create_table(table_name, **options) ...@@ -302,7 +302,7 @@ def create_table(table_name, **options)
if pk.is_a?(Array) if pk.is_a?(Array)
td.primary_keys pk td.primary_keys pk
else else
td.primary_key pk, options.fetch(:id, :primary_key), options.except(:comment) td.primary_key pk, options.fetch(:id, :primary_key), **options.except(:comment)
end end
end end
...@@ -378,7 +378,7 @@ def create_join_table(table_1, table_2, column_options: {}, **options) ...@@ -378,7 +378,7 @@ def create_join_table(table_1, table_2, column_options: {}, **options)
t1_ref, t2_ref = [table_1, table_2].map { |t| t.to_s.singularize } t1_ref, t2_ref = [table_1, table_2].map { |t| t.to_s.singularize }
create_table(join_table_name, options.merge!(id: false)) do |td| create_table(join_table_name, **options.merge!(id: false)) do |td|
td.references t1_ref, column_options td.references t1_ref, column_options
td.references t2_ref, column_options td.references t2_ref, column_options
yield td if block_given? yield td if block_given?
...@@ -783,7 +783,7 @@ def rename_column(table_name, column_name, new_column_name) ...@@ -783,7 +783,7 @@ def rename_column(table_name, column_name, new_column_name)
# #
# For more information see the {"Transactional Migrations" section}[rdoc-ref:Migration]. # For more information see the {"Transactional Migrations" section}[rdoc-ref:Migration].
def add_index(table_name, column_name, options = {}) def add_index(table_name, column_name, options = {})
index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options) index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, **options)
execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns})#{index_options}" execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns})#{index_options}"
end end
...@@ -1196,7 +1196,7 @@ def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc ...@@ -1196,7 +1196,7 @@ def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc
if data_source_exists?(table_name) && index_name_exists?(table_name, index_name) if data_source_exists?(table_name) && index_name_exists?(table_name, index_name)
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists" raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
end end
index_columns = quoted_columns_for_index(column_names, options).join(", ") index_columns = quoted_columns_for_index(column_names, **options).join(", ")
[index_name, index_type, index_columns, index_options, algorithm, using, comment] [index_name, index_type, index_columns, index_options, algorithm, using, comment]
end end
...@@ -1253,7 +1253,7 @@ def options_for_index_columns(options) ...@@ -1253,7 +1253,7 @@ def options_for_index_columns(options)
# the PostgreSQL adapter for supporting operator classes. # the PostgreSQL adapter for supporting operator classes.
def add_options_for_index_columns(quoted_columns, **options) def add_options_for_index_columns(quoted_columns, **options)
if supports_index_sort_order? if supports_index_sort_order?
quoted_columns = add_index_sort_order(quoted_columns, options) quoted_columns = add_index_sort_order(quoted_columns, **options)
end end
quoted_columns quoted_columns
...@@ -1263,7 +1263,7 @@ def quoted_columns_for_index(column_names, **options) ...@@ -1263,7 +1263,7 @@ def quoted_columns_for_index(column_names, **options)
return [column_names] if column_names.is_a?(String) return [column_names] if column_names.is_a?(String)
quoted_columns = Hash[column_names.map { |name| [name.to_sym, quote_column_name(name).dup] }] quoted_columns = Hash[column_names.map { |name| [name.to_sym, quote_column_name(name).dup] }]
add_options_for_index_columns(quoted_columns, options).values add_options_for_index_columns(quoted_columns, **options).values
end end
def index_name_for_remove(table_name, options = {}) def index_name_for_remove(table_name, options = {})
......
...@@ -141,8 +141,8 @@ def open?; !closed?; end ...@@ -141,8 +141,8 @@ def open?; !closed?; end
end end
class SavepointTransaction < Transaction class SavepointTransaction < Transaction
def initialize(connection, savepoint_name, parent_transaction, *args) def initialize(connection, savepoint_name, parent_transaction, *args, **options)
super(connection, *args) super(connection, *args, **options)
parent_transaction.state.add_child(@state) parent_transaction.state.add_child(@state)
......
...@@ -154,8 +154,8 @@ def schema_creation ...@@ -154,8 +154,8 @@ def schema_creation
MySQL::SchemaCreation.new(self) MySQL::SchemaCreation.new(self)
end end
def create_table_definition(*args) def create_table_definition(*args, **options)
MySQL::TableDefinition.new(self, *args) MySQL::TableDefinition.new(self, *args, **options)
end end
def new_column_from_field(table_name, field) def new_column_from_field(table_name, field)
......
...@@ -390,7 +390,7 @@ def rename_table(table_name, new_name) ...@@ -390,7 +390,7 @@ def rename_table(table_name, new_name)
rename_table_indexes(table_name, new_name) rename_table_indexes(table_name, new_name)
end end
def add_column(table_name, column_name, type, options = {}) #:nodoc: def add_column(table_name, column_name, type, **options) #:nodoc:
clear_cache! clear_cache!
super super
change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment) change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
......
...@@ -87,8 +87,8 @@ def schema_creation ...@@ -87,8 +87,8 @@ def schema_creation
SQLite3::SchemaCreation.new(self) SQLite3::SchemaCreation.new(self)
end end
def create_table_definition(*args) def create_table_definition(*args, **options)
SQLite3::TableDefinition.new(self, *args) SQLite3::TableDefinition.new(self, *args, **options)
end end
def new_column_from_field(table_name, field) def new_column_from_field(table_name, field)
......
...@@ -244,7 +244,7 @@ def rename_table(table_name, new_name) ...@@ -244,7 +244,7 @@ def rename_table(table_name, new_name)
rename_table_indexes(table_name, new_name) rename_table_indexes(table_name, new_name)
end end
def add_column(table_name, column_name, type, options = {}) #:nodoc: def add_column(table_name, column_name, type, **options) #:nodoc:
if invalid_alter_table_type?(type, options) if invalid_alter_table_type?(type, options)
alter_table(table_name) do |definition| alter_table(table_name) do |definition|
definition.column(column_name, type, options) definition.column(column_name, type, options)
...@@ -398,7 +398,7 @@ def move_table(from, to, options = {}, &block) ...@@ -398,7 +398,7 @@ def move_table(from, to, options = {}, &block)
def copy_table(from, to, options = {}) def copy_table(from, to, options = {})
from_primary_key = primary_key(from) from_primary_key = primary_key(from)
options[:id] = false options[:id] = false
create_table(to, options) do |definition| create_table(to, **options) do |definition|
@definition = definition @definition = definition
if from_primary_key.is_a?(Array) if from_primary_key.is_a?(Array)
@definition.primary_keys from_primary_key @definition.primary_keys from_primary_key
......
...@@ -54,11 +54,11 @@ def no_touching? ...@@ -54,11 +54,11 @@ def no_touching?
NoTouching.applied_to?(self.class) NoTouching.applied_to?(self.class)
end end
def touch_later(*) # :nodoc: def touch_later(*, **) # :nodoc:
super unless no_touching? super unless no_touching?
end end
def touch(*) # :nodoc: def touch(*, **) # :nodoc:
super unless no_touching? super unless no_touching?
end end
end end
......
...@@ -466,8 +466,8 @@ def persisted? ...@@ -466,8 +466,8 @@ def persisted?
# #
# Attributes marked as readonly are silently ignored if the record is # Attributes marked as readonly are silently ignored if the record is
# being updated. # being updated.
def save(*args, &block) def save(*args, **options, &block)
create_or_update(*args, &block) create_or_update(*args, **options, &block)
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
false false
end end
...@@ -499,8 +499,8 @@ def save(*args, &block) ...@@ -499,8 +499,8 @@ def save(*args, &block)
# being updated. # being updated.
# #
# Unless an error is raised, returns true. # Unless an error is raised, returns true.
def save!(*args, &block) def save!(*args, **options, &block)
create_or_update(*args, &block) || raise(RecordNotSaved.new("Failed to save the record", self)) create_or_update(*args, **options, &block) || raise(RecordNotSaved.new("Failed to save the record", self))
end end
# Deletes the record in the database and freezes this instance to # Deletes the record in the database and freezes this instance to
......
...@@ -110,8 +110,12 @@ def method_missing(method, *args, &block) ...@@ -110,8 +110,12 @@ def method_missing(method, *args, &block)
end end
module ClassMethods # :nodoc: module ClassMethods # :nodoc:
def create(klass, *args) def create(klass, *args, **options)
relation_class_for(klass).new(klass, *args) if options.empty?
relation_class_for(klass).new(klass, *args)
else
relation_class_for(klass).new(klass, *args, **options)
end
end end
private private
......
...@@ -103,7 +103,7 @@ class << self ...@@ -103,7 +103,7 @@ class << self
module ClassMethods module ClassMethods
def store(store_attribute, options = {}) def store(store_attribute, options = {})
serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder]) serialize store_attribute, IndifferentCoder.new(store_attribute, options[:coder])
store_accessor(store_attribute, options[:accessors], options.slice(:prefix, :suffix)) if options.has_key? :accessors store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors
end end
def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil) def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
......
...@@ -40,11 +40,11 @@ def suppress(&block) ...@@ -40,11 +40,11 @@ def suppress(&block)
end end
end end
def save(*) # :nodoc: def save(*, **) # :nodoc:
SuppressorRegistry.suppressed[self.class.name] ? true : super SuppressorRegistry.suppressed[self.class.name] ? true : super
end end
def save!(*) # :nodoc: def save!(*, **) # :nodoc:
SuppressorRegistry.suppressed[self.class.name] ? true : super SuppressorRegistry.suppressed[self.class.name] ? true : super
end end
end end
......
...@@ -9,7 +9,7 @@ module TouchLater # :nodoc: ...@@ -9,7 +9,7 @@ module TouchLater # :nodoc:
before_commit_without_transaction_enrollment :touch_deferred_attributes before_commit_without_transaction_enrollment :touch_deferred_attributes
end end
def touch_later(*names) # :nodoc: def touch_later(*names, **) # :nodoc:
unless persisted? unless persisted?
raise ActiveRecordError, <<-MSG.squish raise ActiveRecordError, <<-MSG.squish
cannot touch on a new or destroyed record object. Consider using cannot touch on a new or destroyed record object. Consider using
......
...@@ -208,8 +208,8 @@ module Transactions ...@@ -208,8 +208,8 @@ module Transactions
# Note that "TRUNCATE" is also a MySQL DDL statement! # Note that "TRUNCATE" is also a MySQL DDL statement!
module ClassMethods module ClassMethods
# See the ConnectionAdapters::DatabaseStatements#transaction API docs. # See the ConnectionAdapters::DatabaseStatements#transaction API docs.
def transaction(options = {}, &block) def transaction(**options, &block)
connection.transaction(options, &block) connection.transaction(**options, &block)
end end
def before_commit(*args, &block) # :nodoc: def before_commit(*args, &block) # :nodoc:
...@@ -310,15 +310,15 @@ def destroy #:nodoc: ...@@ -310,15 +310,15 @@ def destroy #:nodoc:
with_transaction_returning_status { super } with_transaction_returning_status { super }
end end
def save(*) #:nodoc: def save(*, **) #:nodoc:
with_transaction_returning_status { super } with_transaction_returning_status { super }
end end
def save!(*) #:nodoc: def save!(*, **) #:nodoc:
with_transaction_returning_status { super } with_transaction_returning_status { super }
end end
def touch(*) #:nodoc: def touch(*, **) #:nodoc:
with_transaction_returning_status { super } with_transaction_returning_status { super }
end end
......
...@@ -43,13 +43,13 @@ module Validations ...@@ -43,13 +43,13 @@ module Validations
# The validation context can be changed by passing <tt>context: context</tt>. # The validation context can be changed by passing <tt>context: context</tt>.
# The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced # The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced
# with this when the validations module is mixed in, which it is by default. # with this when the validations module is mixed in, which it is by default.
def save(options = {}) def save(**options)
perform_validations(options) ? super : false perform_validations(options) ? super : false
end end
# Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but # Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
# will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid. # will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid.
def save!(options = {}) def save!(**options)
perform_validations(options) ? super : raise_validation_error perform_validations(options) ? super : raise_validation_error
end end
......
...@@ -16,17 +16,17 @@ def test_insert ...@@ -16,17 +16,17 @@ def test_insert
id = 1_000_000 id = 1_000_000
assert_difference "Book.count", +1 do assert_difference "Book.count", +1 do
Book.insert(id: id, name: "Rework", author_id: 1) Book.insert({ id: id, name: "Rework", author_id: 1 })
end end
Book.upsert(id: id, name: "Remote", author_id: 1) Book.upsert({ id: id, name: "Remote", author_id: 1 })
assert_equal "Remote", Book.find(id).name assert_equal "Remote", Book.find(id).name
end end
def test_insert! def test_insert!
assert_difference "Book.count", +1 do assert_difference "Book.count", +1 do
Book.insert! name: "Rework", author_id: 1 Book.insert!({ name: "Rework", author_id: 1 })
end end
end end
...@@ -136,7 +136,7 @@ def test_skip_duplicates_strategy_does_not_secretly_upsert ...@@ -136,7 +136,7 @@ def test_skip_duplicates_strategy_does_not_secretly_upsert
book = Book.create!(author_id: 8, name: "Refactoring", format: "EXPECTED") book = Book.create!(author_id: 8, name: "Refactoring", format: "EXPECTED")
assert_no_difference "Book.count" do assert_no_difference "Book.count" do
Book.insert(author_id: 8, name: "Refactoring", format: "UNEXPECTED") Book.insert({ author_id: 8, name: "Refactoring", format: "UNEXPECTED" })
end end
assert_equal "EXPECTED", book.reload.format assert_equal "EXPECTED", book.reload.format
...@@ -185,7 +185,7 @@ def test_insert_logs_message_including_model_name ...@@ -185,7 +185,7 @@ def test_insert_logs_message_including_model_name
skip unless supports_insert_conflict_target? skip unless supports_insert_conflict_target?
capture_log_output do |output| capture_log_output do |output|
Book.insert(name: "Rework", author_id: 1) Book.insert({ name: "Rework", author_id: 1 })
assert_match "Book Insert", output.string assert_match "Book Insert", output.string
end end
end end
...@@ -203,7 +203,7 @@ def test_upsert_logs_message_including_model_name ...@@ -203,7 +203,7 @@ def test_upsert_logs_message_including_model_name
skip unless supports_insert_on_duplicate_update? skip unless supports_insert_on_duplicate_update?
capture_log_output do |output| capture_log_output do |output|
Book.upsert(name: "Remote", author_id: 1) Book.upsert({ name: "Remote", author_id: 1 })
assert_match "Book Upsert", output.string assert_match "Book Upsert", output.string
end end
end end
......
...@@ -19,28 +19,44 @@ def with_change_table ...@@ -19,28 +19,44 @@ def with_change_table
def test_references_column_type_adds_id def test_references_column_type_adds_id
with_change_table do |t| with_change_table do |t|
@connection.expect :add_reference, nil, [:delete_me, :customer, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_reference, nil, [:delete_me, :customer, {}]
else
@connection.expect :add_reference, nil, [:delete_me, :customer]
end
t.references :customer t.references :customer
end end
end end
def test_remove_references_column_type_removes_id def test_remove_references_column_type_removes_id
with_change_table do |t| with_change_table do |t|
@connection.expect :remove_reference, nil, [:delete_me, :customer, {}] if RUBY_VERSION < "2.7"
@connection.expect :remove_reference, nil, [:delete_me, :customer, {}]
else
@connection.expect :remove_reference, nil, [:delete_me, :customer]
end
t.remove_references :customer t.remove_references :customer
end end
end end
def test_add_belongs_to_works_like_add_references def test_add_belongs_to_works_like_add_references
with_change_table do |t| with_change_table do |t|
@connection.expect :add_reference, nil, [:delete_me, :customer, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_reference, nil, [:delete_me, :customer, {}]
else
@connection.expect :add_reference, nil, [:delete_me, :customer]
end
t.belongs_to :customer t.belongs_to :customer
end end
end end
def test_remove_belongs_to_works_like_remove_references def test_remove_belongs_to_works_like_remove_references
with_change_table do |t| with_change_table do |t|
@connection.expect :remove_reference, nil, [:delete_me, :customer, {}] if RUBY_VERSION < "2.7"
@connection.expect :remove_reference, nil, [:delete_me, :customer, {}]
else
@connection.expect :remove_reference, nil, [:delete_me, :customer]
end
t.remove_belongs_to :customer t.remove_belongs_to :customer
end end
end end
...@@ -110,24 +126,39 @@ def test_primary_key_creates_primary_key_column ...@@ -110,24 +126,39 @@ def test_primary_key_creates_primary_key_column
def test_integer_creates_integer_column def test_integer_creates_integer_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :integer, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}] @connection.expect :add_column, nil, [:delete_me, :foo, :integer, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}]
else
@connection.expect :add_column, nil, [:delete_me, :foo, :integer]
@connection.expect :add_column, nil, [:delete_me, :bar, :integer]
end
t.integer :foo, :bar t.integer :foo, :bar
end end
end end
def test_bigint_creates_bigint_column def test_bigint_creates_bigint_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :bigint, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :bigint, {}] @connection.expect :add_column, nil, [:delete_me, :foo, :bigint, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :bigint, {}]
else
@connection.expect :add_column, nil, [:delete_me, :foo, :bigint]
@connection.expect :add_column, nil, [:delete_me, :bar, :bigint]
end
t.bigint :foo, :bar t.bigint :foo, :bar
end end
end end
def test_string_creates_string_column def test_string_creates_string_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :string, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :string, {}] @connection.expect :add_column, nil, [:delete_me, :foo, :string, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :string, {}]
else
@connection.expect :add_column, nil, [:delete_me, :foo, :string]
@connection.expect :add_column, nil, [:delete_me, :bar, :string]
end
t.string :foo, :bar t.string :foo, :bar
end end
end end
...@@ -135,16 +166,26 @@ def test_string_creates_string_column ...@@ -135,16 +166,26 @@ def test_string_creates_string_column
if current_adapter?(:PostgreSQLAdapter) if current_adapter?(:PostgreSQLAdapter)
def test_json_creates_json_column def test_json_creates_json_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :json, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :json, {}] @connection.expect :add_column, nil, [:delete_me, :foo, :json, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :json, {}]
else
@connection.expect :add_column, nil, [:delete_me, :foo, :json]
@connection.expect :add_column, nil, [:delete_me, :bar, :json]
end
t.json :foo, :bar t.json :foo, :bar
end end
end end
def test_xml_creates_xml_column def test_xml_creates_xml_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :xml, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :xml, {}] @connection.expect :add_column, nil, [:delete_me, :foo, :xml, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :xml, {}]
else
@connection.expect :add_column, nil, [:delete_me, :foo, :xml]
@connection.expect :add_column, nil, [:delete_me, :bar, :xml]
end
t.xml :foo, :bar t.xml :foo, :bar
end end
end end
...@@ -152,21 +193,33 @@ def test_xml_creates_xml_column ...@@ -152,21 +193,33 @@ def test_xml_creates_xml_column
def test_column_creates_column def test_column_creates_column
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}]
else
@connection.expect :add_column, nil, [:delete_me, :bar, :integer]
end
t.column :bar, :integer t.column :bar, :integer
end end
end end
def test_column_creates_column_with_options def test_column_creates_column_with_options
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, { null: false }] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, { null: false }]
else
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, { null: false }]
end
t.column :bar, :integer, null: false t.column :bar, :integer, null: false
end end
end end
def test_column_creates_column_with_index def test_column_creates_column_with_index
with_change_table do |t| with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}] if RUBY_VERSION < "2.7"
@connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}]
else
@connection.expect :add_column, nil, [:delete_me, :bar, :integer]
end
@connection.expect :add_index, nil, [:delete_me, :bar, {}] @connection.expect :add_index, nil, [:delete_me, :bar, {}]
t.column :bar, :integer, index: true t.column :bar, :integer, index: true
end end
......
...@@ -94,10 +94,17 @@ def test_invert_change_table ...@@ -94,10 +94,17 @@ def test_invert_change_table
t.rename :kind, :cultivar t.rename :kind, :cultivar
end end
end end
assert_equal [ if RUBY_VERSION < "2.7"
[:rename_column, [:fruits, :cultivar, :kind]], assert_equal [
[:remove_column, [:fruits, :name, :string, {}], nil], [:rename_column, [:fruits, :cultivar, :kind]],
], @recorder.commands [:remove_column, [:fruits, :name, :string, {}], nil],
], @recorder.commands
else
assert_equal [
[:rename_column, [:fruits, :cultivar, :kind]],
[:remove_column, [:fruits, :name, :string], nil],
], @recorder.commands
end
assert_raises(ActiveRecord::IrreversibleMigration) do assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.revert do @recorder.revert do
......
...@@ -193,7 +193,13 @@ def delegate(*methods, to: nil, prefix: nil, allow_nil: nil, private: nil) ...@@ -193,7 +193,13 @@ def delegate(*methods, to: nil, prefix: nil, allow_nil: nil, private: nil)
method_names = methods.map do |method| method_names = methods.map do |method|
# Attribute writer methods only accept one argument. Makes sure []= # Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments. # methods still accept two arguments.
definition = /[^\]]=$/.match?(method) ? "arg" : "*args, &block" definition = if /[^\]]=$/.match?(method)
"arg"
elsif RUBY_VERSION >= "2.7"
"..."
else
"*args, &block"
end
# The following generated method calls the target exactly once, storing # The following generated method calls the target exactly once, storing
# the returned value in a dummy variable. # the returned value in a dummy variable.
......
...@@ -14,14 +14,21 @@ def initialize(context, options) ...@@ -14,14 +14,21 @@ def initialize(context, options)
private private
def method_missing(method, *arguments, &block) def method_missing(method, *arguments, &block)
options = nil
if arguments.first.is_a?(Proc) if arguments.first.is_a?(Proc)
proc = arguments.pop proc = arguments.pop
arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) } arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
elsif arguments.last.respond_to?(:to_hash)
options = @options.deep_merge(arguments.pop)
else else
arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup) options = @options
end end
@context.__send__(method, *arguments, &block) if options
@context.__send__(method, *arguments, **options, &block)
else
@context.__send__(method, *arguments, &block)
end
end end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册