From 260211cf155b4f5c740cac51ff628893f6354d7d Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 17 Jun 2024 10:39:51 +1000 Subject: [PATCH] Fix Ruboxop issue --- app/models/enterprise.rb | 2 +- app/models/spree/variant.rb | 4 +++- app/services/products_renderer.rb | 6 ++++-- spec/controllers/api/v0/variants_controller_spec.rb | 4 ++-- .../spree/admin/variants_controller_spec.rb | 2 +- spec/models/spree/product_spec.rb | 12 ++++++------ spec/models/spree/taxon_spec.rb | 1 - spec/models/spree/variant_spec.rb | 2 +- spec/services/products_renderer_spec.rb | 3 +-- spec/services/sets/product_set_spec.rb | 2 +- spec/system/admin/products_v3/products_spec.rb | 8 ++++---- spec/system/admin/reports_spec.rb | 4 +++- spec/system/admin/variant_overrides_spec.rb | 12 ++++++++---- spec/system/consumer/shopping/shopping_spec.rb | 7 ++++--- 14 files changed, 39 insertions(+), 30 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3d6b33fc17..3d95f20353 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -45,7 +45,7 @@ class Enterprise < ApplicationRecord has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id', dependent: :restrict_with_exception - + belongs_to :address, class_name: 'Spree::Address' belongs_to :business_address, optional: true, class_name: 'Spree::Address', dependent: :destroy has_many :enterprise_fees, dependent: :restrict_with_exception diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index f0198f0171..5bca9d31df 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -25,7 +25,9 @@ module Spree variants_display_name variants_supplier_name).join('_or_')}_cont".freeze - belongs_to :product, -> { with_deleted }, required: true, touch: true, class_name: 'Spree::Product' + belongs_to :product, -> { + with_deleted + }, touch: true, class_name: 'Spree::Product', optional: false belongs_to :tax_category, class_name: 'Spree::TaxCategory' belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', optional: false belongs_to :primary_taxon, class_name: 'Spree::Taxon', touch: true, optional: false diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index bebdadf812..9db07fa975 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -2,7 +2,8 @@ require 'open_food_network/scope_product_to_hub' -class ProductsRenderer + +class ProductsRenderer # rubocop:disable Metrics/ClassLength include Pagy::Backend class NoProducts < RuntimeError; end @@ -63,7 +64,8 @@ class ProductsRenderer distributed_products.products_supplier_relation end - def filter(query) + # TODO: refactor to address CyclomaticComplexity + def filter(query) # rubocop:disable Metrics/CyclomaticComplexity supplier_properties = args[:q]&.slice("with_variants_supplier_properties") ransack_results = query.ransack(args[:q]).result.to_a diff --git a/spec/controllers/api/v0/variants_controller_spec.rb b/spec/controllers/api/v0/variants_controller_spec.rb index 1f053d0325..ce9f486512 100644 --- a/spec/controllers/api/v0/variants_controller_spec.rb +++ b/spec/controllers/api/v0/variants_controller_spec.rb @@ -145,8 +145,8 @@ RSpec.describe Api::V0::VariantsController, type: :controller do it "can create a new variant" do original_number_of_variants = variant.product.variants.count api_post :create, variant: { sku: "12345", unit_value: "1", unit_description: "L", - price: "1",primary_taxon_id: taxon.id, - supplier_id: variant.supplier.id}, + price: "1", primary_taxon_id: taxon.id, + supplier_id: variant.supplier.id }, product_id: variant.product.id expect(attributes.all?{ |attr| json_response.include? attr.to_s }).to eq(true) diff --git a/spec/controllers/spree/admin/variants_controller_spec.rb b/spec/controllers/spree/admin/variants_controller_spec.rb index 3ee35450ff..ba6e1542e5 100644 --- a/spec/controllers/spree/admin/variants_controller_spec.rb +++ b/spec/controllers/spree/admin/variants_controller_spec.rb @@ -79,7 +79,7 @@ module Spree variant: { supplier_id: new_producer.id } ) - expect(order_cycle.reload.distributed_variants).to_not include variant + expect(order_cycle.reload.distributed_variants).not_to include variant end end end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 5b5aaef0b4..cf678c5491 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -510,14 +510,14 @@ module Spree producer_a = create(:enterprise, name: "a_cooperative") producer_g = create(:enterprise, name: "g_cooperative") - product_1 = create(:product, supplier_id: producer_z.id) - product_2 = create(:product, supplier_id: producer_a.id) - product_3 = create(:product, supplier_id: producer_g.id) + product1 = create(:product, supplier_id: producer_z.id) + product2 = create(:product, supplier_id: producer_a.id) + product3 = create(:product, supplier_id: producer_g.id) expect(Product.by_producer).to match_array([ - product_2, - product_3, - product_1, + product2, + product3, + product1, ]) end end diff --git a/spec/models/spree/taxon_spec.rb b/spec/models/spree/taxon_spec.rb index 282320c206..49797f69f4 100644 --- a/spec/models/spree/taxon_spec.rb +++ b/spec/models/spree/taxon_spec.rb @@ -45,7 +45,6 @@ module Spree let!(:product) { create(:simple_product, primary_taxon_id: taxon1.id) } let(:variant) { product.variants.first } - it "is touched when assignment of primary_taxon on a variant changes" do expect do variant.update(primary_taxon: taxon2) diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index abfea244dd..c633bf67da 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -27,7 +27,7 @@ RSpec.describe Spree::Variant do variant = build(:variant, shipping_category: nil) expect(variant).to be_valid - expect(variant.shipping_category).to_not be_nil + expect(variant.shipping_category).not_to be_nil end end diff --git a/spec/services/products_renderer_spec.rb b/spec/services/products_renderer_spec.rb index 8ef148c9f0..2716448ba2 100644 --- a/spec/services/products_renderer_spec.rb +++ b/spec/services/products_renderer_spec.rb @@ -23,8 +23,7 @@ RSpec.describe ProductsRenderer do variants: [ create(:variant, supplier: cakes_supplier, primary_taxon: cakes), create(:variant, supplier: fruits_supplier, primary_taxon: cakes) - ] - ) + ]) } let!(:product_cherries) { create(:product, name: "cherries", primary_taxon_id: fruits.id, diff --git a/spec/services/sets/product_set_spec.rb b/spec/services/sets/product_set_spec.rb index f24fefcd76..068b49a900 100644 --- a/spec/services/sets/product_set_spec.rb +++ b/spec/services/sets/product_set_spec.rb @@ -240,7 +240,7 @@ RSpec.describe Sets::ProductSet do }.to change { variant.supplier }.to(producer). and change { order_cycle.distributed_variants.count }.by(-1) - expect(order_cycle.distributed_variants).to_not include variant + expect(order_cycle.distributed_variants).not_to include variant end end end diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index e2293de943..35dad02d36 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -488,13 +488,13 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi let(:supplier_permitted) { create(:supplier_enterprise, name: 'Supplier Permitted') } let(:distributor_managed) { create(:distributor_enterprise, name: 'Distributor Managed') } let(:distributor_unmanaged) { create(:distributor_enterprise, name: 'Distributor Unmanaged') } - let!(:product_supplied) { create(:product, supplier: supplier_managed1, price: 10.0) } - let!(:product_not_supplied) { create(:product, supplier: supplier_unmanaged) } + let!(:product_supplied) { create(:product, supplier_id: supplier_managed1.id, price: 10.0) } + let!(:product_not_supplied) { create(:product, supplier_id: supplier_unmanaged.id) } let!(:product_supplied_permitted) { - create(:product, name: 'Product Permitted', supplier: supplier_permitted, price: 10.0) + create(:product, name: 'Product Permitted', supplier_id: supplier_permitted.id, price: 10.0) } let(:product_supplied_inactive) { - create(:product, supplier: supplier_managed1, price: 10.0) + create(:product, supplier_id: supplier_managed1.id, price: 10.0) } let!(:supplier_permitted_relationship) do diff --git a/spec/system/admin/reports_spec.rb b/spec/system/admin/reports_spec.rb index cf00d2c2e6..835c6c8a4c 100644 --- a/spec/system/admin/reports_spec.rb +++ b/spec/system/admin/reports_spec.rb @@ -389,7 +389,9 @@ RSpec.describe ' supplier_id: supplier.id) } let(:variant1) { product1.variants.first } - let(:variant2) { create(:variant, product: product1, price: 80.0, primary_taxon: taxon, supplier:) } + let(:variant2) { + create(:variant, product: product1, price: 80.0, primary_taxon: taxon, supplier:) + } let(:variant3) { product2.variants.first } before do diff --git a/spec/system/admin/variant_overrides_spec.rb b/spec/system/admin/variant_overrides_spec.rb index ce81548573..44937c3123 100644 --- a/spec/system/admin/variant_overrides_spec.rb +++ b/spec/system/admin/variant_overrides_spec.rb @@ -48,7 +48,8 @@ RSpec.describe " context "when inventory_items exist for variants" do let!(:product) { - create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', variant_unit_scale: 1) + create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', + variant_unit_scale: 1) } let!(:variant) { create(:variant, product:, unit_value: 1, price: 1.23, on_hand: 12) } let!(:inventory_item) { create(:inventory_item, enterprise: hub, variant: ) } @@ -58,7 +59,8 @@ RSpec.describe " variant_unit_scale: 1) } let!(:variant_managed) { - create(:variant, product: product_managed, supplier: producer_managed, unit_value: 3, price: 3.65, on_hand: 2) + create(:variant, product: product_managed, supplier: producer_managed, unit_value: 3, + price: 3.65, on_hand: 2) } let!(:inventory_item_managed) { create(:inventory_item, enterprise: hub, variant: variant_managed ) @@ -66,7 +68,8 @@ RSpec.describe " let!(:product_related) { create(:simple_product, supplier_id: producer_related.id) } let!(:variant_related) { - create(:variant, product: product_related, supplier: producer_related, unit_value: 2, price: 2.34, on_hand: 23) + create(:variant, product: product_related, supplier: producer_related, unit_value: 2, + price: 2.34, on_hand: 23) } let!(:inventory_item_related) { create(:inventory_item, enterprise: hub, variant: variant_related ) @@ -476,7 +479,8 @@ RSpec.describe " describe "when inventory_items do not exist for variants" do let!(:product) { - create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', variant_unit_scale: 1) + create(:simple_product, supplier_id: producer.id, variant_unit: 'weight', + variant_unit_scale: 1) } let!(:variant1) { create(:variant, product:, unit_value: 1, price: 1.23, on_hand: 12) diff --git a/spec/system/consumer/shopping/shopping_spec.rb b/spec/system/consumer/shopping/shopping_spec.rb index fcbdbfc890..a7984c6318 100644 --- a/spec/system/consumer/shopping/shopping_spec.rb +++ b/spec/system/consumer/shopping/shopping_spec.rb @@ -296,7 +296,8 @@ RSpec.describe "As a consumer I want to shop with a distributor" do display_as: 'displayedunderthename') end let(:product2) { - create(:simple_product, supplier_id: supplier.id, name: "Meercats", meta_keywords: "Wild Fresh") + create(:simple_product, supplier_id: supplier.id, name: "Meercats", + meta_keywords: "Wild Fresh") } let(:variant3) { create(:variant, product: product2, supplier:, price: 40, display_name: "Ferrets") @@ -338,8 +339,8 @@ RSpec.describe "As a consumer I want to shop with a distributor" do it "returns results when successful" do visit shop_path # When we see the Add button, it means product are loaded on the page - expect(page).to have_content("Add", count: 4) - + expect(page).to have_content("Add", count: 4) + fill_in "search", with: "74576345634XXXXXX" expect(page).to have_content "Sorry, no results found" expect(page).not_to have_content 'Meercats'