From 90e7d3cda884460b43af77701d9964d1b94e7342 Mon Sep 17 00:00:00 2001 From: Matheus Sales Date: Fri, 6 Sep 2024 11:52:39 -0300 Subject: [PATCH] refactor: Use `merge!` instead of `merge` when merging hashes by --- lib/shoulda/matchers/action_controller/route_params.rb | 2 +- lib/shoulda/matchers/active_model/allow_value_matcher.rb | 2 +- .../matchers/active_model/validate_inclusion_of_matcher.rb | 2 +- .../association_matchers/counter_cache_matcher.rb | 2 +- .../association_matchers/join_table_matcher.rb | 2 +- .../matchers/active_record/define_enum_for_matcher.rb | 2 +- lib/shoulda/matchers/doublespeak/double_collection.rb | 2 +- lib/shoulda/matchers/rails_shim.rb | 6 +++--- spec/support/unit/active_record/create_table.rb | 2 +- .../unit/create_model_arguments/uniqueness_matcher.rb | 6 +++--- spec/support/unit/model_creators/active_model.rb | 2 +- spec/support/unit/model_creators/active_record.rb | 2 +- .../unit/record_builder_with_i18n_validation_message.rb | 2 +- .../matchers/action_controller/permit_matcher_spec.rb | 2 +- .../action_controller/set_session_or_flash_matcher_spec.rb | 2 +- .../active_model/validate_absence_of_matcher_spec.rb | 3 +-- .../active_model/validate_exclusion_of_matcher_spec.rb | 2 +- .../matchers/active_record/have_db_index_matcher_spec.rb | 2 +- .../active_record/validate_uniqueness_of_matcher_spec.rb | 4 ++-- 19 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/shoulda/matchers/action_controller/route_params.rb b/lib/shoulda/matchers/action_controller/route_params.rb index 9761e1604..936bafcad 100644 --- a/lib/shoulda/matchers/action_controller/route_params.rb +++ b/lib/shoulda/matchers/action_controller/route_params.rb @@ -27,7 +27,7 @@ def controller_and_action_given_as_string? def extract_params_from_string controller, action = args[0].split('#') - params = (args[1] || {}).merge(controller: controller, action: action) + params = (args[1] || {}).merge!(controller: controller, action: action) normalize_values(params) end diff --git a/lib/shoulda/matchers/active_model/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb index 29d2aa3a0..c2a1c51b4 100644 --- a/lib/shoulda/matchers/active_model/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -677,7 +677,7 @@ def default_attribute_message_values attribute: attribute_to_check_message_against, } - defaults.merge(options[:expected_message_values]) + defaults.merge!(options[:expected_message_values]) end def model_name diff --git a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb index 9395cb5bd..b900903b3 100644 --- a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb @@ -358,7 +358,7 @@ def simple_description description = "validate that :#{@attribute}" description << - if @array.count > 1 + if @array.length > 1 " is either #{inspected_array}" else " is #{inspected_array}" diff --git a/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb b/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb index 414002454..9c0b52262 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb @@ -59,7 +59,7 @@ def normalize_value when String, Symbol { active: true, column: counter_cache.to_s } when Hash - { active: true, column: nil }.merge(counter_cache) + { active: true, column: nil }.merge!(counter_cache) else raise ArgumentError, 'Invalid counter_cache option' end diff --git a/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb b/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb index 6faf27bf0..cef027a9d 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb @@ -92,7 +92,7 @@ def missing_columns_message end def column_label - if missing_columns.count > 1 + if missing_columns.length > 1 'columns' else 'column' diff --git a/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb b/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb index 4288422fd..571147411 100644 --- a/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb +++ b/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb @@ -758,7 +758,7 @@ def exclude_scopes? def to_hash(value) if value.is_a?(Array) value.each_with_index.inject({}) do |hash, (item, index)| - hash.merge(item.to_s => index) + hash.merge!(item.to_s => index) end else value.stringify_keys diff --git a/lib/shoulda/matchers/doublespeak/double_collection.rb b/lib/shoulda/matchers/doublespeak/double_collection.rb index 0ac9698bc..2ba9416ca 100644 --- a/lib/shoulda/matchers/doublespeak/double_collection.rb +++ b/lib/shoulda/matchers/doublespeak/double_collection.rb @@ -27,7 +27,7 @@ def deactivate def calls_by_method_name doubles_by_method_name.inject({}) do |hash, (method_name, double)| - hash.merge method_name => double.calls.map(&:args) + hash.merge! method_name => double.calls.map(&:args) end end diff --git a/lib/shoulda/matchers/rails_shim.rb b/lib/shoulda/matchers/rails_shim.rb index 4c2833a83..5f17e3636 100644 --- a/lib/shoulda/matchers/rails_shim.rb +++ b/lib/shoulda/matchers/rails_shim.rb @@ -60,7 +60,7 @@ def serialized_attributes_for(model) attribute_types_for(model). inject({}) do |hash, (attribute_name, attribute_type)| if type_serialized_defined && attribute_type.is_a?(::ActiveRecord::Type::Serialized) - hash.merge(attribute_name => attribute_type.coder) + hash.merge!(attribute_name => attribute_type.coder) else hash end @@ -121,7 +121,7 @@ def attribute_types_for(model) model.columns.inject({}) do |hash, column| key = column.name.to_s value = model.type_for_attribute(column.name) - hash.merge(key => value) + hash.merge!(key => value) end else raise NotImplementedError @@ -169,7 +169,7 @@ def simply_generate_validation_message( ] primary_translation_key = default_translation_keys.shift translate_options = - { default: default_translation_keys }.merge(options) + { default: default_translation_keys }.merge!(options) I18n.translate(primary_translation_key, translate_options) end diff --git a/spec/support/unit/active_record/create_table.rb b/spec/support/unit/active_record/create_table.rb index ed93268f0..0d051c929 100644 --- a/spec/support/unit/active_record/create_table.rb +++ b/spec/support/unit/active_record/create_table.rb @@ -75,7 +75,7 @@ def normalize_columns(columns) end else columns.inject({}) do |hash, column_name| - hash.merge(column_name => { type: :string }) + hash.merge!(column_name => { type: :string }) end end end diff --git a/spec/support/unit/create_model_arguments/uniqueness_matcher.rb b/spec/support/unit/create_model_arguments/uniqueness_matcher.rb index c3fe9b5c7..3ef20f15e 100644 --- a/spec/support/unit/create_model_arguments/uniqueness_matcher.rb +++ b/spec/support/unit/create_model_arguments/uniqueness_matcher.rb @@ -17,7 +17,7 @@ def self.normalize_attributes(attributes) def columns attributes.inject({}) do |options, attribute| - options.merge( + options.merge!( attribute.name => { type: attribute.column_type, options: attribute.column_options, @@ -27,12 +27,12 @@ def columns end def validation_options - super.merge(scope: scope_attribute_names) + super.merge!(scope: scope_attribute_names) end def attribute_default_values_by_name attributes.inject({}) do |values, attribute| - values.merge(attribute.name => attribute.default_value) + values.merge!(attribute.name => attribute.default_value) end end diff --git a/spec/support/unit/model_creators/active_model.rb b/spec/support/unit/model_creators/active_model.rb index eae21a059..be31ee0d5 100644 --- a/spec/support/unit/model_creators/active_model.rb +++ b/spec/support/unit/model_creators/active_model.rb @@ -18,7 +18,7 @@ def self.call(args) def initialize(args) @arguments = CreateModelArguments::Basic.wrap( - args.merge( + args.merge!( model_creation_strategy: UnitTests::ModelCreationStrategies::ActiveModel, ), ) diff --git a/spec/support/unit/model_creators/active_record.rb b/spec/support/unit/model_creators/active_record.rb index 77708134f..6ee848c2f 100644 --- a/spec/support/unit/model_creators/active_record.rb +++ b/spec/support/unit/model_creators/active_record.rb @@ -21,7 +21,7 @@ def self.call(args) def initialize(args) @arguments = CreateModelArguments::Basic.wrap( - args.merge( + args.merge!( model_creation_strategy: UnitTests::ModelCreationStrategies::ActiveRecord, ), ) diff --git a/spec/support/unit/record_builder_with_i18n_validation_message.rb b/spec/support/unit/record_builder_with_i18n_validation_message.rb index 392612809..b5cab5114 100644 --- a/spec/support/unit/record_builder_with_i18n_validation_message.rb +++ b/spec/support/unit/record_builder_with_i18n_validation_message.rb @@ -4,7 +4,7 @@ module UnitTests class RecordBuilderWithI18nValidationMessage < SimpleDelegator def initialize(builder, options = {}) super(builder) - @options = default_options.merge(options) + @options = default_options.merge!(options) builder.message = validation_message_key end diff --git a/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb index b62add043..4d92ab482 100644 --- a/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb @@ -106,7 +106,7 @@ def permit_with_conditional_slice_of_params( selected_param: :user ) params = all_params.inject({}) do |hash, param| - hash.merge(param => { any: 'value' }) + hash.merge!(param => { any: 'value' }) end permit.add_params(params).on(selected_param) diff --git a/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb index a7fa58c63..f811b8bab 100644 --- a/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb @@ -556,7 +556,7 @@ def build_store(overrides = {}) :has_value? => nil, :empty? => nil, } - methods = defaults.merge(overrides) + methods = defaults.merge!(overrides) double('store', methods) end end diff --git a/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb index b92aac012..41af8cec9 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb @@ -278,8 +278,7 @@ def validation_matcher_scenario_args end def define_model_validating_absence_of(attr, validation_options = {}, given_column_options = {}) - default_column_options = { type: :string, options: {} } - column_options = default_column_options.merge(given_column_options) + column_options = { type: :string, options: {} }.merge!(given_column_options) define_model :example, attr => column_options do |model| model.validates_absence_of(attr, validation_options) diff --git a/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb index de42eb847..77087595f 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb @@ -238,7 +238,7 @@ def configure_validation_matcher(matcher) def define_model_validating_exclusion(options) options = options.dup column_type = options.delete(:column_type) { :string } - super options.merge(column_type: column_type) + super options.merge!(column_type: column_type) end end diff --git a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb index bc5a9414c..9aa0785f6 100644 --- a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb @@ -511,7 +511,7 @@ def record_with_index_on( **index_options ) columns ||= Array.wrap(column_name_or_names).inject({}) do |hash, name| - hash.merge(name => :string) + hash.merge!(name => :string) end model = define_model( diff --git a/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb index 767111f1a..7aa63c77e 100644 --- a/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb @@ -1575,7 +1575,7 @@ def next_version_of(value, value_type) def build_record_from(model, extra_attributes = {}) attributes = attributes_with_values_for(model) - model.new(attributes.merge(extra_attributes)) + model.new(attributes.merge!(extra_attributes)) end def create_record_from(model, extra_attributes = {}) @@ -1608,7 +1608,7 @@ def define_model_validating_uniqueness(options = {}, &block) model = define_model(:example, column_options) do |m| m.validates_uniqueness_of attribute_name, - validation_options.merge(scope: scope_attribute_names) + validation_options.merge!(scope: scope_attribute_names) if m.respond_to?(:attr_accessible) attributes.each { |attr| m.attr_accessible(attr[:name]) }