Merge pull request #13286 from chitty/apr29-rubocop-style-corrections

Rubocop Style corrections
This commit is contained in:
David Cook
2025-05-05 12:53:57 +10:00
committed by GitHub
24 changed files with 39 additions and 83 deletions

View File

@@ -469,18 +469,6 @@ Security/Open:
Exclude:
- 'app/services/image_importer.rb'
# Offense count: 7
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/ArrayIntersect:
Exclude:
- 'app/models/spree/ability.rb'
- 'app/models/tag_rule/filter_order_cycles.rb'
- 'app/models/tag_rule/filter_payment_methods.rb'
- 'app/models/tag_rule/filter_products.rb'
- 'app/models/tag_rule/filter_shipping_methods.rb'
- 'lib/open_food_network/tag_rule_applicator.rb'
- 'spec/support/matchers/select2_matchers.rb'
# Offense count: 23
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
@@ -510,36 +498,6 @@ Style/ClassAndModuleChildren:
- 'lib/open_food_network/locking.rb'
- 'spec/models/spree/payment_method_spec.rb'
# Offense count: 10
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AllowSplatArgument.
Style/HashConversion:
Exclude:
- 'app/controllers/admin/column_preferences_controller.rb'
- 'app/controllers/admin/variant_overrides_controller.rb'
- 'app/controllers/spree/admin/products_controller.rb'
- 'app/models/product_import/product_importer.rb'
- 'app/serializers/api/admin/exchange_serializer.rb'
- 'app/services/variants_stock_levels.rb'
- 'spec/controllers/admin/inventory_items_controller_spec.rb'
- 'spec/controllers/admin/variant_overrides_controller_spec.rb'
# Offense count: 13
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AllowedReceivers.
# AllowedReceivers: Thread.current
Style/HashEachMethods:
Exclude:
- 'app/controllers/admin/enterprises_controller.rb'
- 'app/controllers/spree/admin/shipping_methods_controller.rb'
- 'app/forms/enterprise_fees_bulk_update.rb'
- 'app/models/product_import/entry_processor.rb'
- 'app/models/spree/preferences/configuration.rb'
- 'app/services/sets/model_set.rb'
- 'lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb'
- 'spec/models/product_importer_spec.rb'
- 'spec/support/cancan_helper.rb'
# Offense count: 4
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/MapToHash:

View File

@@ -40,8 +40,8 @@ module Admin
respond_to do |format|
format.json do
collection_attributes = Hash[permitted_params[:column_preferences].
each_with_index.map { |cp, i| [i, cp] }]
collection_attributes = permitted_params[:column_preferences].
each_with_index.to_h { |cp, i| [i, cp] }
collection_attributes.select!{ |_i, cp|
cp[:action_name] == permitted_params[:action_name]
}

View File

@@ -254,7 +254,7 @@ module Admin
# methods that are specific to each class do not become available until after the
# record is persisted. This problem is compounded by the use of calculators.
@object.transaction do
tag_rules_attributes.select{ |_i, attrs| attrs[:type].present? }.each do |_i, attrs|
tag_rules_attributes.select{ |_i, attrs| attrs[:type].present? }.each_value do |attrs|
rule = @object.tag_rules.find_by(id: attrs.delete(:id)) ||
attrs[:type].constantize.new(enterprise: @object)
@@ -293,7 +293,7 @@ module Admin
def check_can_change_bulk_sells
return if spree_current_user.admin?
params[:sets_enterprise_set][:collection_attributes].each do |_i, enterprise_params|
params[:sets_enterprise_set][:collection_attributes].each_value do |enterprise_params|
unless spree_current_user == Enterprise.find_by(id: enterprise_params[:id]).owner
enterprise_params.delete :sells
end
@@ -327,7 +327,7 @@ module Admin
def check_can_change_bulk_owner
return if spree_current_user.admin?
bulk_params[:collection_attributes].each do |_i, enterprise_params|
bulk_params[:collection_attributes].each_value do |enterprise_params|
enterprise_params.delete :owner_id
end
end

View File

@@ -70,7 +70,7 @@ module Admin
end
def load_collection
collection_hash = Hash[variant_overrides_params.each_with_index.map { |vo, i| [i, vo] }]
collection_hash = variant_overrides_params.each_with_index.to_h { |vo, i| [i, vo] }
# Reset count_on_hand when switching to producer settings:
collection_hash.each_value do |vo|

View File

@@ -134,9 +134,9 @@ module Spree
end
def product_set_from_params
collection_hash = Hash[products_bulk_params[:products].each_with_index.map { |p, i|
[i, p]
} ]
collection_hash = products_bulk_params[:products].each_with_index.to_h { |p, i|
[i, p]
}
Sets::ProductSet.new(collection_attributes: collection_hash)
end

View File

@@ -104,7 +104,7 @@ module Spree
return unless shipping_fees
shipping_fees.each do |_, shipping_amount|
shipping_fees.each_value do |shipping_amount|
unless shipping_amount.nil? || Float(shipping_amount, exception: false)
flash[:error] = I18n.t(:calculator_preferred_value_error)
return redirect_to location_after_save

View File

@@ -30,7 +30,7 @@ class EnterpriseFeesBulkUpdate
private
def check_enterprise_fee_input
enterprise_fee_bulk_params['collection_attributes'].each do |_, fee_row|
enterprise_fee_bulk_params['collection_attributes'].each_value do |fee_row|
enterprise_fees = fee_row['calculator_attributes']&.slice(
:preferred_flat_percent, :preferred_amount,
:preferred_first_item, :preferred_additional_item,
@@ -40,7 +40,7 @@ class EnterpriseFeesBulkUpdate
next unless enterprise_fees
enterprise_fees.each do |_, enterprise_amount|
enterprise_fees.each_value do |enterprise_amount|
unless enterprise_amount.nil? || Float(enterprise_amount, exception: false)
@errors.add(:base, I18n.t(:calculator_preferred_value_error))
end
@@ -49,7 +49,7 @@ class EnterpriseFeesBulkUpdate
end
def check_calculators_compatibility_with_taxes
enterprise_fee_bulk_params['collection_attributes'].each do |_, enterprise_fee|
enterprise_fee_bulk_params['collection_attributes'].each_value do |enterprise_fee|
next unless enterprise_fee['inherits_tax_category'] == "true"
next unless EnterpriseFee::PER_ORDER_CALCULATORS.include?(enterprise_fee['calculator_type'])

View File

@@ -46,7 +46,7 @@ module ProductImport
end
def count_existing_items
@spreadsheet_data.enterprises_index.each do |_enterprise_name, attrs|
@spreadsheet_data.enterprises_index.each_value do |attrs|
enterprise_id = attrs[:id]
next unless enterprise_id && permission_by_id?(enterprise_id)

View File

@@ -292,7 +292,7 @@ module ProductImport
def build_entries_from_rows(rows, offset = 0)
rows.each_with_index.inject([]) do |entries, (row, i)|
row_data = Hash[[headers, row].transpose]
row_data = [headers, row].transpose.to_h
entry = SpreadsheetEntry.new(row_data)
entry.line_number = offset + i + 2
entries.push entry

View File

@@ -36,7 +36,7 @@ module Spree
end
def reset
preferences.each do |name, _value|
preferences.each_key do |name|
set_preference name, preference_default(name)
end
end

View File

@@ -7,7 +7,7 @@ class TagRule::FilterOrderCycles < TagRule
def tags_match?(order_cycle)
exchange_tags = exchange_for(order_cycle)&.tag_list || []
preferred_tags = preferred_exchange_tags.split(",")
( exchange_tags & preferred_tags ).any?
exchange_tags.intersect?(preferred_tags)
end
def reject_matched?

View File

@@ -7,7 +7,7 @@ class TagRule::FilterPaymentMethods < TagRule
def tags_match?(payment_method)
payment_method_tags = payment_method&.tag_list || []
preferred_tags = preferred_payment_method_tags.split(",")
( payment_method_tags & preferred_tags ).any?
payment_method_tags.intersect?(preferred_tags)
end
def reject_matched?

View File

@@ -12,7 +12,7 @@ class TagRule
def tags_match?(variant)
variant_tags = variant&.[]("tag_list") || []
preferred_tags = preferred_variant_tags.split(",")
(variant_tags & preferred_tags).any?
variant_tags.intersect?(preferred_tags)
end
def reject_matched?

View File

@@ -11,6 +11,6 @@ class TagRule::FilterShippingMethods < TagRule
def tags_match?(shipping_method)
shipping_method_tags = shipping_method&.tag_list || []
preferred_tags = preferred_shipping_method_tags.split(",")
( shipping_method_tags & preferred_tags ).any?
shipping_method_tags.intersect?(preferred_tags)
end
end

View File

@@ -11,7 +11,7 @@ module Api
def variants
variants = object.incoming? ? visible_incoming_variants : visible_outgoing_variants
Hash[object.variants.merge(variants).map { |v| [v.id, true] }]
object.variants.merge(variants).to_h { |v| [v.id, true] }
end
private

View File

@@ -21,7 +21,7 @@ module Sets
end
def collection_attributes=(collection_attributes)
collection_attributes.each do |_k, attributes|
collection_attributes.each_value do |attributes|
# attributes == {:id => 123, :next_collection_at => '...'}
found_element = @collection.detect do |element|
element.id.to_s == attributes[:id].to_s && !element.id.nil?

View File

@@ -25,17 +25,15 @@ class VariantsStockLevels
private
def variant_stock_levels(line_items)
Hash[
line_items.map do |line_item|
variant = scoped_variant(line_item.order.distributor, line_item.variant)
line_items.to_h do |line_item|
variant = scoped_variant(line_item.order.distributor, line_item.variant)
[variant.id,
{ quantity: line_item.quantity,
max_quantity: line_item.max_quantity,
on_hand: variant.on_hand,
on_demand: variant.on_demand }]
end
]
[variant.id,
{ quantity: line_item.quantity,
max_quantity: line_item.max_quantity,
on_hand: variant.on_hand,
on_demand: variant.on_demand }]
end
end
def scoped_variant(distributor, variant)

View File

@@ -59,7 +59,7 @@ module OpenFoodNetwork
def customer_tags_match?(rule)
preferred_tags = rule.preferred_customer_tags.split(",")
(customer_tags & preferred_tags).any?
customer_tags.intersect?(preferred_tags)
end
end
end

View File

@@ -37,7 +37,7 @@ module Reporting
hash[:line_item].order.distributor_id,
hash[:line_item].order.order_cycle_id
]
end.each do |_, v|
end.each_value do |v|
v.map!{ |item| item[:line_item] }
end
end

View File

@@ -68,7 +68,7 @@ RSpec.describe Admin::InventoryItemsController, type: :controller do
it "returns an error message" do
expect{ spree_post :create, bad_params }.to change{ InventoryItem.count }.by(0)
expect(response.body).to eq Hash[:errors, ["Visible must be true or false"]].to_json
expect(response.body).to eq({ errors: ["Visible must be true or false"] }.to_json)
end
end
end
@@ -134,7 +134,7 @@ RSpec.describe Admin::InventoryItemsController, type: :controller do
it "returns an error message" do
expect{ spree_put :update, bad_params }.to change{ InventoryItem.count }.by(0)
expect(response.body).to eq Hash[:errors, ["Visible must be true or false"]].to_json
expect(response.body).to eq({ errors: ["Visible must be true or false"] }.to_json)
end
end
end

View File

@@ -60,7 +60,7 @@ RSpec.describe Admin::VariantOverridesController, type: :controller do
put :bulk_update, as: format, params: { variant_overrides: variant_override_params }
expect(assigns[:hubs]).to eq [hub]
expect(assigns[:producers]).to eq [variant.supplier]
expect(assigns[:hub_permissions]).to eq Hash[hub.id, [variant.supplier.id]]
expect(assigns[:hub_permissions]).to eq({ hub.id => [variant.supplier.id] })
expect(assigns[:inventory_items]).to eq [inventory_item]
end
@@ -163,7 +163,7 @@ RSpec.describe Admin::VariantOverridesController, type: :controller do
put(:bulk_reset, params:)
expect(assigns[:hubs]).to eq [hub]
expect(assigns[:producers]).to eq [producer]
expect(assigns[:hub_permissions]).to eq Hash[hub.id, [producer.id]]
expect(assigns[:hub_permissions]).to eq({ hub.id => [producer.id] })
expect(assigns[:inventory_items]).to eq []
end

View File

@@ -1059,7 +1059,7 @@ end
def filter(type, entries)
valid_count = 0
entries.each do |_line_number, entry|
entries.each_value do |entry|
validates_as = entry['validates_as']
valid_count += 1 if type == 'valid' && (validates_as != '')

View File

@@ -17,7 +17,7 @@ module Spree
member.merge(i => true)
}
end
ability_hash.each do |action, _true_or_false|
ability_hash.each_key do |action|
@ability_result[action] = ability.can?(action, target)
end

View File

@@ -56,7 +56,7 @@ RSpec::Matchers.define :have_select2 do |id, options = {}|
# if options.key? :without_options
end
if (options.keys & %i(selected options without_options)).any?
if options.keys.intersect?(%i(selected options without_options))
raise "Not yet implemented"
end