Skip to content

Commit

Permalink
refactor: Use merge! instead of merge when merging hashes by
Browse files Browse the repository at this point in the history
  • Loading branch information
matsales28 committed Sep 6, 2024
1 parent 8890f3d commit 90e7d3c
Show file tree
Hide file tree
Showing 19 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/action_controller/route_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/active_model/allow_value_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/doublespeak/double_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/shoulda/matchers/rails_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/support/unit/active_record/create_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/support/unit/model_creators/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/unit/model_creators/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -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]) }
Expand Down

0 comments on commit 90e7d3c

Please sign in to comment.