mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
extract product filter functionality to a helper
This commit is contained in:
@@ -9,16 +9,13 @@ 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]
|
||||
before_action :load_spree_api_key, only: [:index, :variant_overrides]
|
||||
before_action :strip_new_properties, only: [:create, :update]
|
||||
|
||||
PRODUCT_FILTER = [
|
||||
'query', 'producerFilter', 'categoryFilter', 'sorting', 'importDateFilter'
|
||||
].freeze
|
||||
|
||||
def new
|
||||
@object.shipping_category = DefaultShippingCategory.find_or_create
|
||||
super
|
||||
@@ -58,9 +55,7 @@ module Spree
|
||||
end
|
||||
|
||||
def edit
|
||||
filters = product_filters(params)
|
||||
@url_filters = filters.empty? ? "" : "?#{filters.to_query}"
|
||||
|
||||
@url_filters = product_filters(params)
|
||||
super
|
||||
end
|
||||
|
||||
@@ -259,10 +254,6 @@ module Spree
|
||||
def set_product_master_variant_price_to_zero
|
||||
@product.price = 0 if @product.price.nil?
|
||||
end
|
||||
|
||||
def product_filters(params)
|
||||
params.select { |k, _v| PRODUCT_FILTER.include?(k) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
15
app/helpers/spree/admin/product_filter_helper.rb
Normal file
15
app/helpers/spree/admin/product_filter_helper.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
# 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
|
||||
@@ -1,5 +1,5 @@
|
||||
- content_for :page_actions do
|
||||
%li= button_link_to t('admin.products.back_to_products_list'), "#{admin_products_path}##{@url_filters}", :icon => 'icon-arrow-left'
|
||||
%li= button_link_to t('admin.products.back_to_products_list'), "#{admin_products_path}#{(@url_filters.empty? ? "" : "#?#{@url_filters.to_query}")}", :icon => 'icon-arrow-left'
|
||||
%li#new_product_link
|
||||
= button_link_to t(:new_product), new_object_url, { :icon => 'icon-plus', :id => 'admin_new_product' }
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
= render :partial => 'spree/admin/shared/product_tabs', :locals => { :current => 'Product Details' }
|
||||
= render :partial => 'spree/shared/error_messages', :locals => { :target => @product }
|
||||
|
||||
= form_for [:admin, @product], :url => "#{admin_product_path(@product)}#{@url_filters}", :method => :put, :html => { :multipart => true } do |f|
|
||||
= form_for [:admin, @product], :url => admin_product_path(@product, @url_filters), :method => :put, :html => { :multipart => true } do |f|
|
||||
%fieldset.no-border-top{'ng-app' => 'admin.products'}
|
||||
= render :partial => 'form', :locals => { :f => f }
|
||||
.form-buttons.filter-actions.actions
|
||||
= button t(:update), 'icon-refresh'
|
||||
%span.or= t(:or)
|
||||
= button_link_to t(:cancel), "#{collection_url}##{@url_filters}", icon: 'icon-remove'
|
||||
= button_link_to t(:cancel), "#{collection_url}#{(@url_filters.empty? ? "" : "#?#{@url_filters.to_query}")}", icon: 'icon-remove'
|
||||
|
||||
@@ -12,22 +12,22 @@
|
||||
- if can?(:admin, Spree::Product)
|
||||
- klass = current == 'Product Details' ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-edit', t('admin.products.tabs.product_details'), edit_admin_product_url(@product)
|
||||
= link_to_with_icon 'icon-edit', t('admin.products.tabs.product_details'), edit_admin_product_url(@product, @url_filters)
|
||||
- if can?(:admin, Spree::Image)
|
||||
- klass = current == 'Images' ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-picture', t('admin.products.tabs.images'), admin_product_images_url(@product)
|
||||
= link_to_with_icon 'icon-picture', t('admin.products.tabs.images'), admin_product_images_url(@product, @url_filters)
|
||||
- if can?(:admin, Spree::Variant)
|
||||
- klass = current == 'Variants' ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-th-large', t('admin.products.tabs.variants'), admin_product_variants_url(@product)
|
||||
= link_to_with_icon 'icon-th-large', t('admin.products.tabs.variants'), admin_product_variants_url(@product, @url_filters)
|
||||
- if can?(:admin, Spree::ProductProperty)
|
||||
- klass = current == 'Product Properties' ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-tasks', t('admin.products.tabs.product_properties'), admin_product_product_properties_url(@product)
|
||||
= link_to_with_icon 'icon-tasks', t('admin.products.tabs.product_properties'), admin_product_product_properties_url(@product, @url_filters)
|
||||
- klass = current == 'Group Buy Options' ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-tasks', t('admin.products.tabs.group_buy_options'), group_buy_options_admin_product_url(@product)
|
||||
= link_to_with_icon 'icon-tasks', t('admin.products.tabs.group_buy_options'), group_buy_options_admin_product_url(@product, @url_filters)
|
||||
- klass = current == t(:search) ? 'active' : ''
|
||||
%li{:class => klass}
|
||||
= link_to_with_icon 'icon-tasks', t(:search), seo_admin_product_url(@product)
|
||||
= link_to_with_icon 'icon-tasks', t(:search), seo_admin_product_url(@product, @url_filters)
|
||||
|
||||
@@ -167,6 +167,14 @@ feature '
|
||||
# Link back to the bulk product update page should include the filters
|
||||
expect(page).to have_link(I18n.t('admin.products.back_to_products_list'), href: %r{admin\/products#\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t(:cancel), href: %r{admin\/products#\?#{filter_query}})
|
||||
|
||||
# Sidebar link should include the filters
|
||||
expect(page).to have_link(I18n.t('admin.products.tabs.product_details'), href: %r{admin\/products\/#{product.permalink}\/edit\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t('admin.products.tabs.images'), href: %r{admin\/products\/#{product.permalink}\/images\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t('admin.products.tabs.variants'), href: %r{admin\/products\/#{product.permalink}\/variants\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t('admin.products.tabs.product_properties'), href: %r{admin\/products\/#{product.permalink}\/product_properties\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t('admin.products.tabs.group_buy_options'), href: %r{admin\/products\/#{product.permalink}\/group_buy_options\?#{filter_query}})
|
||||
expect(page).to have_link(I18n.t(:search), href: %r{admin\/products\/#{product.permalink}\/seo\?#{filter_query}})
|
||||
end
|
||||
|
||||
scenario "editing product group buy options" do
|
||||
|
||||
18
spec/helpers/spree/admin/product_filter_helper_spec.rb
Normal file
18
spec/helpers/spree/admin/product_filter_helper_spec.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Admin::ProductFilterHelper, type: :helper do
|
||||
describe 'product_filters' do
|
||||
it 'should return only parameters included in PRODUCT_FILTER' do
|
||||
parameters = { 'query' => 'test', 'categoryFilter' => 2, 'randomFilter' => 5 }
|
||||
|
||||
filters = helper.product_filters(parameters)
|
||||
|
||||
puts filters.inspect
|
||||
expect(filters.key?('query')).to be true
|
||||
expect(filters.key?('categoryFilter')).to be true
|
||||
expect(filters.key?('randomFilter')).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user