diff --git a/app/models/spree/ability.rb b/app/models/spree/ability.rb index 14a895bd7c..ce87891d13 100644 --- a/app/models/spree/ability.rb +++ b/app/models/spree/ability.rb @@ -353,23 +353,22 @@ module Spree end end + def can_edit_order(order, user) + return unless order.distributor&.enable_producers_to_edit_orders + + order.variants.any? { |variant| user.enterprises.ids.include?(variant.supplier_id) } + end + def add_manage_line_items_abilities(user) - can_edit_order_lambda = lambda do |order| - return unless order.distributor&.enable_producers_to_edit_orders - - order.variants.any? { |variant| user.enterprises.ids.include?(variant.supplier_id) } - end - can [:admin, :read, :index, :edit, :update, :bulk_management], Spree::Order do |order| - can_edit_order_lambda.call(order) + can_edit_order(order, user) end can [:admin, :index, :create, :destroy, :update], Spree::LineItem do |item| - can_edit_order_lambda.call(item.order) + can_edit_order(item.order, user) end can [:index, :create, :add, :read, :edit, :update], Spree::Shipment do |shipment| - can_edit_order_lambda.call(shipment.order) + can_edit_order(shipment.order, user) end - can [:visible], Enterprise end diff --git a/app/services/search_orders.rb b/app/services/search_orders.rb index fe839fafe0..e8992d1b90 100644 --- a/app/services/search_orders.rb +++ b/app/services/search_orders.rb @@ -28,7 +28,8 @@ class SearchOrders end def search_query - base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty.or(::Permissions::Order.new(current_user).editable_orders.finalized) + base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty + .or(::Permissions::Order.new(current_user).editable_orders.finalized) return base_query if params[:shipping_method_id].blank? diff --git a/spec/lib/open_food_network/scope_variants_for_search_spec.rb b/spec/lib/open_food_network/scope_variants_for_search_spec.rb index 5c72fc4012..6e55c99db7 100644 --- a/spec/lib/open_food_network/scope_variants_for_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_for_search_spec.rb @@ -194,9 +194,12 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do context "when search is done by the producer allowing to edit orders" do let(:params) { { q: "product" } } let(:producer) { create(:supplier_enterprise) } - let(:spree_current_user) { instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id), can_manage_line_items_in_orders_only?: true) } + let(:spree_current_user) { + instance_double('Spree::User', enterprises: Enterprise.where(id: producer.id), + can_manage_line_items_in_orders_only?: true) + } - it "returns all products distributed through distributors allowing producers to edit orders" do + it "returns products distributed by distributors allowing producers to edit orders" do v1.supplier_id = producer.id v2.supplier_id = producer.id v1.save! diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index dc69933211..6d123b6990 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -301,7 +301,7 @@ RSpec.describe Spree::User do describe "#can_manage_line_items_in_orders_only?" do let(:producer) { create(:supplier_enterprise) } - let(:order) { create(:order, distributor: distributor) } + let(:order) { create(:order, distributor:) } subject { user.can_manage_line_items_in_orders_only? } @@ -310,7 +310,8 @@ RSpec.describe Spree::User do context "order containing their product" do before do - order.line_items << create(:line_item, product: create(:product, supplier_id: producer.id)) + order.line_items << create(:line_item, + product: create(:product, supplier_id: producer.id)) end context "order distributor allow producer to edit orders" do let(:distributor) do diff --git a/spec/services/permissions/order_spec.rb b/spec/services/permissions/order_spec.rb index fbf727b792..5f7246d75b 100644 --- a/spec/services/permissions/order_spec.rb +++ b/spec/services/permissions/order_spec.rb @@ -67,7 +67,9 @@ module Permissions end context "with search params" do - let(:search_params) { { completed_at_gt: Time.zone.now.yesterday.strftime('%Y-%m-%d') } } + let(:search_params) { + { completed_at_gt: Time.zone.now.yesterday.strftime('%Y-%m-%d') } + } let(:permissions) { Permissions::Order.new(user, search_params) } it "only returns completed, non-cancelled orders within search filter range" do diff --git a/spec/system/admin/orders/producer_actions_spec.rb b/spec/system/admin/orders/producer_actions_spec.rb index 026bae260a..bfebbcfee3 100644 --- a/spec/system/admin/orders/producer_actions_spec.rb +++ b/spec/system/admin/orders/producer_actions_spec.rb @@ -121,7 +121,6 @@ RSpec.describe 'As a producer who have the ability to update orders' do end end - def expect_product_change(product, action, expected_qty = 0) within('table.index') do # JS for this page sometimes take more than 2 seconds (default timeout for cappybara) @@ -157,8 +156,6 @@ RSpec.describe 'As a producer who have the ability to update orders' do find("a[data-variant-id='#{product.variants.last.id}'][data-action='remove']").click click_button 'OK' end - end end - end