mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
move product filter helper to a service
This commit is contained in:
@@ -9,7 +9,6 @@ module Spree
|
||||
include OpenFoodNetwork::SpreeApiKeyLoader
|
||||
include OrderCyclesHelper
|
||||
include EnterprisesHelper
|
||||
include ProductFilterHelper
|
||||
|
||||
before_action :load_data
|
||||
before_action :load_form_data, only: [:index, :new, :create, :edit, :update]
|
||||
@@ -18,13 +17,13 @@ module Spree
|
||||
|
||||
def new
|
||||
@object.shipping_category = DefaultShippingCategory.find_or_create
|
||||
super
|
||||
end
|
||||
|
||||
def create
|
||||
delete_stock_params_and_set_after do
|
||||
@prototype = Spree::Prototype.find(params[:product][:prototype_id]) if \
|
||||
params[:product][:prototype_id].present?
|
||||
if params[:product][:prototype_id].present?
|
||||
@prototype = Spree::Prototype.find(params[:product][:prototype_id])
|
||||
end
|
||||
|
||||
@object.attributes = permitted_resource_params
|
||||
if @object.save
|
||||
@@ -55,12 +54,11 @@ module Spree
|
||||
end
|
||||
|
||||
def edit
|
||||
@url_filters = product_filters(params)
|
||||
super
|
||||
@url_filters = ::ProductFilters.new.extract(params)
|
||||
end
|
||||
|
||||
def update
|
||||
@url_filter = product_filters(request.query_parameters)
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
|
||||
original_supplier_id = @product.supplier_id
|
||||
delete_stock_params_and_set_after do
|
||||
@@ -72,7 +70,7 @@ module Spree
|
||||
|
||||
flash[:success] = flash_message_for(@object, :successfully_updated)
|
||||
end
|
||||
redirect_to edit_admin_product_url(@object, @url_filter)
|
||||
redirect_to edit_admin_product_url(@object, @url_filters)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,40 +4,35 @@ module Spree
|
||||
module Admin
|
||||
class VariantsController < ResourceController
|
||||
helper 'spree/products'
|
||||
include ProductFilterHelper
|
||||
|
||||
belongs_to 'spree/product', find_by: :permalink
|
||||
new_action.before :new_before
|
||||
|
||||
def index
|
||||
@url_filters = product_filters(request.query_parameters)
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
end
|
||||
|
||||
def edit
|
||||
@url_filters = product_filters(request.query_parameters)
|
||||
|
||||
super
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
end
|
||||
|
||||
def update
|
||||
@url_filter = product_filters(request.query_parameters)
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
|
||||
if @object.update(permitted_resource_params)
|
||||
flash[:success] = flash_message_for(@object, :successfully_updated)
|
||||
redirect_to admin_product_variants_url(params[:product_id], @url_filter)
|
||||
redirect_to admin_product_variants_url(params[:product_id], @url_filters)
|
||||
else
|
||||
redirect_to edit_admin_product_variant_url(params[:product_id], @object, @url_filter)
|
||||
redirect_to edit_admin_product_variant_url(params[:product_id], @object, @url_filters)
|
||||
end
|
||||
end
|
||||
|
||||
def new
|
||||
@url_filters = product_filters(request.query_parameters)
|
||||
|
||||
super
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
end
|
||||
|
||||
def create
|
||||
@url_filter = product_filters(request.query_parameters)
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
|
||||
on_demand = params[:variant].delete(:on_demand)
|
||||
on_hand = params[:variant].delete(:on_hand)
|
||||
@@ -45,9 +40,9 @@ module Spree
|
||||
@object.attributes = permitted_resource_params
|
||||
if @object.save
|
||||
flash[:success] = flash_message_for(@object, :successfully_created)
|
||||
redirect_to admin_product_variants_url(params[:product_id], @url_filter)
|
||||
redirect_to admin_product_variants_url(params[:product_id], @url_filters)
|
||||
else
|
||||
redirect_to new_admin_product_variant_url(params[:product_id], @url_filter)
|
||||
redirect_to new_admin_product_variant_url(params[:product_id], @url_filters)
|
||||
end
|
||||
|
||||
return unless @object.present? && @object.valid?
|
||||
@@ -63,7 +58,7 @@ module Spree
|
||||
end
|
||||
|
||||
def destroy
|
||||
@url_filter = product_filters(request.query_parameters)
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
|
||||
@variant = Spree::Variant.find(params[:id])
|
||||
flash[:success] = if VariantDeleter.new.delete(@variant)
|
||||
@@ -72,9 +67,7 @@ module Spree
|
||||
Spree.t('notice_messages.variant_not_deleted')
|
||||
end
|
||||
|
||||
respond_with(@variant) do |format|
|
||||
format.html { redirect_to admin_product_variants_url(params[:product_id], @url_filter) }
|
||||
end
|
||||
redirect_to admin_product_variants_url(params[:product_id], @url_filters)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module Admin
|
||||
module ProductFilterHelper
|
||||
PRODUCT_FILTER = [
|
||||
'query', 'producerFilter', 'categoryFilter', 'sorting', 'importDateFilter'
|
||||
].freeze
|
||||
|
||||
def product_filters(params)
|
||||
params.select { |k, _v| PRODUCT_FILTER.include?(k) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
app/services/product_filters.rb
Normal file
11
app/services/product_filters.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ProductFilters
|
||||
PRODUCT_FILTERS = [
|
||||
'query', 'producerFilter', 'categoryFilter', 'sorting', 'importDateFilter'
|
||||
].freeze
|
||||
|
||||
def extract(params)
|
||||
params.select { |key, _value| PRODUCT_FILTERS.include?(key) }
|
||||
end
|
||||
end
|
||||
17
spec/services/product_filters_spec.rb
Normal file
17
spec/services/product_filters_spec.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe ProductFilters do
|
||||
describe "extract" do
|
||||
it "should return a hash including only key from ProductFilters::PRODUCT_FILTERS" do
|
||||
params = { 'id' => 20, 'producerFilter' => 2, 'categoryFilter' => 5 }
|
||||
|
||||
filters = ProductFilters.new.extract(params)
|
||||
|
||||
expect(filters).not_to include 'id'
|
||||
expect(filters).to include 'producerFilter'
|
||||
expect(filters).to include 'categoryFilter'
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user