From 63168086e72390fc64ef6f9f2ca93e45d58ffd3a Mon Sep 17 00:00:00 2001 From: Carlos Chitty Date: Tue, 29 Apr 2025 14:27:31 -0400 Subject: [PATCH] Autocorrect Style/HashConversion offenses --- .rubocop_todo.yml | 14 -------------- .../admin/column_preferences_controller.rb | 4 ++-- .../admin/variant_overrides_controller.rb | 2 +- .../spree/admin/products_controller.rb | 6 +++--- app/models/product_import/product_importer.rb | 2 +- .../api/admin/exchange_serializer.rb | 2 +- app/services/variants_stock_levels.rb | 18 ++++++++---------- .../admin/inventory_items_controller_spec.rb | 4 ++-- .../admin/variant_overrides_controller_spec.rb | 4 ++-- 9 files changed, 20 insertions(+), 36 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 414d0dd5d8..c21ecf39d6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -508,20 +508,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. diff --git a/app/controllers/admin/column_preferences_controller.rb b/app/controllers/admin/column_preferences_controller.rb index 5403a9a5ee..79f70d9ff1 100644 --- a/app/controllers/admin/column_preferences_controller.rb +++ b/app/controllers/admin/column_preferences_controller.rb @@ -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] } diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index d88c25da10..00ba18f101 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -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| diff --git a/app/controllers/spree/admin/products_controller.rb b/app/controllers/spree/admin/products_controller.rb index 1c693ea32d..f51ba5503e 100644 --- a/app/controllers/spree/admin/products_controller.rb +++ b/app/controllers/spree/admin/products_controller.rb @@ -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 diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 2cdfba52bd..e92006cea8 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -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 diff --git a/app/serializers/api/admin/exchange_serializer.rb b/app/serializers/api/admin/exchange_serializer.rb index 90828f0ba1..39505ca67b 100644 --- a/app/serializers/api/admin/exchange_serializer.rb +++ b/app/serializers/api/admin/exchange_serializer.rb @@ -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 diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index e76c1fef99..454658b220 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -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) diff --git a/spec/controllers/admin/inventory_items_controller_spec.rb b/spec/controllers/admin/inventory_items_controller_spec.rb index be33be4871..49d2adb03d 100644 --- a/spec/controllers/admin/inventory_items_controller_spec.rb +++ b/spec/controllers/admin/inventory_items_controller_spec.rb @@ -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 diff --git a/spec/controllers/admin/variant_overrides_controller_spec.rb b/spec/controllers/admin/variant_overrides_controller_spec.rb index 13848a22d1..807e4a26c7 100644 --- a/spec/controllers/admin/variant_overrides_controller_spec.rb +++ b/spec/controllers/admin/variant_overrides_controller_spec.rb @@ -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