From 660ce92c27f68370fba638b72da08633a7d418c3 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 18 Jul 2019 17:20:40 +0100 Subject: [PATCH] Merge spree api controllers and its decorators --- .../spree/api/products_controller.rb | 85 ++++++++++++++++++ .../api/products_controller_decorator.rb | 86 ------------------- .../spree/api/shipments_controller.rb | 28 ++++-- .../api/shipments_controller_decorator.rb | 47 ---------- .../spree/api/variants_controller.rb | 8 ++ .../api/variants_controller_decorator.rb | 9 -- 6 files changed, 114 insertions(+), 149 deletions(-) delete mode 100644 app/controllers/spree/api/products_controller_decorator.rb delete mode 100644 app/controllers/spree/api/shipments_controller_decorator.rb delete mode 100644 app/controllers/spree/api/variants_controller_decorator.rb diff --git a/app/controllers/spree/api/products_controller.rb b/app/controllers/spree/api/products_controller.rb index 2e2574091c..fb9b76d654 100644 --- a/app/controllers/spree/api/products_controller.rb +++ b/app/controllers/spree/api/products_controller.rb @@ -1,3 +1,5 @@ +require 'open_food_network/permissions' + module Spree module Api class ProductsController < Spree::Api::BaseController @@ -56,6 +58,89 @@ module Spree @product.variants_including_master.update_all(:deleted_at => Time.now) respond_with(@product, :status => 204) end + + def managed + authorize! :admin, Spree::Product + authorize! :read, Spree::Product + + @products = product_scope.ransack(params[:q]).result.managed_by(current_api_user).page(params[:page]).per(params[:per_page]) + respond_with(@products, default_template: :index) + end + + # TODO: This should be named 'managed'. Is the action above used? Maybe we should remove it. + def bulk_products + @products = OpenFoodNetwork::Permissions.new(current_api_user).editable_products. + merge(product_scope). + order('created_at DESC'). + ransack(params[:q]).result. + page(params[:page]).per(params[:per_page]) + + render_paged_products @products + end + + def overridable + producers = OpenFoodNetwork::Permissions.new(current_api_user). + variant_override_producers.by_name + + @products = paged_products_for_producers producers + + render_paged_products @products + end + + def soft_delete + authorize! :delete, Spree::Product + @product = find_product(params[:product_id]) + authorize! :delete, @product + @product.destroy + respond_with(@product, status: 204) + end + + # POST /api/products/:product_id/clone + # + def clone + authorize! :create, Spree::Product + original_product = find_product(params[:product_id]) + authorize! :update, original_product + + @product = original_product.duplicate + + respond_with(@product, status: 201, default_template: :show) + end + + private + + # Copied and modified from Spree::Api::BaseController to allow + # enterprise users to access inactive products + def product_scope + if current_api_user.has_spree_role?("admin") || current_api_user.enterprises.present? # This line modified + scope = Spree::Product + if params[:show_deleted] + scope = scope.with_deleted + end + else + scope = Spree::Product.active + end + + scope.includes(:master) + end + + def paged_products_for_producers(producers) + Spree::Product.scoped. + merge(product_scope). + where(supplier_id: producers). + by_producer.by_name. + ransack(params[:q]).result. + page(params[:page]).per(params[:per_page]) + end + + def render_paged_products(products) + serializer = ActiveModel::ArraySerializer.new( + products, + each_serializer: Api::Admin::ProductSerializer + ) + + render text: { products: serializer, pages: products.num_pages }.to_json + end end end end diff --git a/app/controllers/spree/api/products_controller_decorator.rb b/app/controllers/spree/api/products_controller_decorator.rb deleted file mode 100644 index 906cbb3d0a..0000000000 --- a/app/controllers/spree/api/products_controller_decorator.rb +++ /dev/null @@ -1,86 +0,0 @@ -require 'open_food_network/permissions' - -Spree::Api::ProductsController.class_eval do - def managed - authorize! :admin, Spree::Product - authorize! :read, Spree::Product - - @products = product_scope.ransack(params[:q]).result.managed_by(current_api_user).page(params[:page]).per(params[:per_page]) - respond_with(@products, default_template: :index) - end - - # TODO: This should be named 'managed'. Is the action above used? Maybe we should remove it. - def bulk_products - @products = OpenFoodNetwork::Permissions.new(current_api_user).editable_products. - merge(product_scope). - order('created_at DESC'). - ransack(params[:q]).result. - page(params[:page]).per(params[:per_page]) - - render_paged_products @products - end - - def overridable - producers = OpenFoodNetwork::Permissions.new(current_api_user). - variant_override_producers.by_name - - @products = paged_products_for_producers producers - - render_paged_products @products - end - - def soft_delete - authorize! :delete, Spree::Product - @product = find_product(params[:product_id]) - authorize! :delete, @product - @product.destroy - respond_with(@product, status: 204) - end - - # POST /api/products/:product_id/clone - # - def clone - authorize! :create, Spree::Product - original_product = find_product(params[:product_id]) - authorize! :update, original_product - - @product = original_product.duplicate - - respond_with(@product, status: 201, default_template: :show) - end - - private - - # Copied and modified from Spree::Api::BaseController to allow - # enterprise users to access inactive products - def product_scope - if current_api_user.has_spree_role?("admin") || current_api_user.enterprises.present? # This line modified - scope = Spree::Product - if params[:show_deleted] - scope = scope.with_deleted - end - else - scope = Spree::Product.active - end - - scope.includes(:master) - end - - def paged_products_for_producers(producers) - Spree::Product.scoped. - merge(product_scope). - where(supplier_id: producers). - by_producer.by_name. - ransack(params[:q]).result. - page(params[:page]).per(params[:per_page]) - end - - def render_paged_products(products) - serializer = ActiveModel::ArraySerializer.new( - products, - each_serializer: Api::Admin::ProductSerializer - ) - - render text: { products: serializer, pages: products.num_pages }.to_json - end -end diff --git a/app/controllers/spree/api/shipments_controller.rb b/app/controllers/spree/api/shipments_controller.rb index ad0b71755e..07f2cf3f7b 100644 --- a/app/controllers/spree/api/shipments_controller.rb +++ b/app/controllers/spree/api/shipments_controller.rb @@ -1,3 +1,5 @@ +require 'open_food_network/scope_variant_to_hub' + module Spree module Api class ShipmentsController < Spree::Api::BaseController @@ -7,15 +9,16 @@ module Spree before_filter :find_and_update_shipment, :only => [:ship, :ready, :add, :remove] def create - variant = Spree::Variant.find(params[:variant_id]) + variant = scoped_variant(params[:variant_id]) quantity = params[:quantity].to_i - @shipment = @order.shipments.create(:stock_location_id => params[:stock_location_id]) + @shipment = get_or_create_shipment(params[:stock_location_id]) + @order.contents.add(variant, quantity, nil, @shipment) @shipment.refresh_rates @shipment.save! - respond_with(@shipment.reload, :default_template => :show) + respond_with(@shipment.reload, default_template: :show) end def update @@ -59,21 +62,22 @@ module Spree end def add - variant = Spree::Variant.find(params[:variant_id]) + variant = scoped_variant(params[:variant_id]) quantity = params[:quantity].to_i @order.contents.add(variant, quantity, nil, @shipment) - respond_with(@shipment, :default_template => :show) + respond_with(@shipment, default_template: :show) end def remove - variant = Spree::Variant.find(params[:variant_id]) + variant = scoped_variant(params[:variant_id]) quantity = params[:quantity].to_i @order.contents.remove(variant, quantity, @shipment) @shipment.reload if @shipment.persisted? - respond_with(@shipment, :default_template => :show) + + respond_with(@shipment, default_template: :show) end private @@ -88,6 +92,16 @@ module Spree @shipment.update_attributes(params[:shipment]) @shipment.reload end + + def scoped_variant(variant_id) + variant = Spree::Variant.find(variant_id) + OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) + variant + end + + def get_or_create_shipment(stock_location_id) + @order.shipment || @order.shipments.create(stock_location_id: stock_location_id) + end end end end diff --git a/app/controllers/spree/api/shipments_controller_decorator.rb b/app/controllers/spree/api/shipments_controller_decorator.rb deleted file mode 100644 index 6fb5e242ec..0000000000 --- a/app/controllers/spree/api/shipments_controller_decorator.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'open_food_network/scope_variant_to_hub' - -Spree::Api::ShipmentsController.class_eval do - def create - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i - @shipment = get_or_create_shipment(params[:stock_location_id]) - - @order.contents.add(variant, quantity, nil, @shipment) - - @shipment.refresh_rates - @shipment.save! - - respond_with(@shipment.reload, default_template: :show) - end - - def add - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i - - @order.contents.add(variant, quantity, nil, @shipment) - - respond_with(@shipment, default_template: :show) - end - - def remove - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i - - @order.contents.remove(variant, quantity, @shipment) - @shipment.reload if @shipment.persisted? - - respond_with(@shipment, default_template: :show) - end - - private - - def scoped_variant(variant_id) - variant = Spree::Variant.find(variant_id) - OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) - variant - end - - def get_or_create_shipment(stock_location_id) - @order.shipment || @order.shipments.create(stock_location_id: stock_location_id) - end -end diff --git a/app/controllers/spree/api/variants_controller.rb b/app/controllers/spree/api/variants_controller.rb index 7086dd9740..2e7a55c37d 100644 --- a/app/controllers/spree/api/variants_controller.rb +++ b/app/controllers/spree/api/variants_controller.rb @@ -39,6 +39,14 @@ module Spree end end + def soft_delete + @variant = scope.find(params[:variant_id]) + authorize! :delete, @variant + + VariantDeleter.new.delete(@variant) + respond_with @variant, status: 204 + end + def destroy authorize! :delete, Variant @variant = scope.find(params[:id]) diff --git a/app/controllers/spree/api/variants_controller_decorator.rb b/app/controllers/spree/api/variants_controller_decorator.rb deleted file mode 100644 index 0c5a1dd2a6..0000000000 --- a/app/controllers/spree/api/variants_controller_decorator.rb +++ /dev/null @@ -1,9 +0,0 @@ -Spree::Api::VariantsController.class_eval do - def soft_delete - @variant = scope.find(params[:variant_id]) - authorize! :delete, @variant - - VariantDeleter.new.delete(@variant) - respond_with @variant, status: 204 - end -end