From be40742ce4ecc72725d850f51da227891805ebdb Mon Sep 17 00:00:00 2001 From: Dennis Marchand Date: Mon, 22 Jun 2020 10:58:06 -0700 Subject: [PATCH] Add filter feature for stock movements We've had feedback from folks that being able to search for stock movements related to a variant would be helpful. --- .../spree/admin/stock_movements_controller.rb | 7 ++ .../admin/stock_movements/index.html.erb | 29 +++++++ .../admin/stock_movements_controller_spec.rb | 79 +++++++++++++++++++ core/app/models/spree/stock_movement.rb | 2 + core/spec/models/spree/stock_movement_spec.rb | 4 + 5 files changed, 121 insertions(+) create mode 100644 backend/spec/controllers/spree/admin/stock_movements_controller_spec.rb diff --git a/backend/app/controllers/spree/admin/stock_movements_controller.rb b/backend/app/controllers/spree/admin/stock_movements_controller.rb index 91b67a74412..a26568125f8 100644 --- a/backend/app/controllers/spree/admin/stock_movements_controller.rb +++ b/backend/app/controllers/spree/admin/stock_movements_controller.rb @@ -6,6 +6,13 @@ class StockMovementsController < ResourceController belongs_to 'spree/stock_location' before_action :parent + def index + params[:q] ||= {} + + @search = collection.ransack(params[:q]) + @stock_movements = @search.result.page(params[:page]) + end + private def permitted_resource_params diff --git a/backend/app/views/spree/admin/stock_movements/index.html.erb b/backend/app/views/spree/admin/stock_movements/index.html.erb index a4cd48e208a..9edba5a6faf 100644 --- a/backend/app/views/spree/admin/stock_movements/index.html.erb +++ b/backend/app/views/spree/admin/stock_movements/index.html.erb @@ -12,6 +12,35 @@ <% end %> <% admin_breadcrumb(plural_resource_name(Spree::StockMovement)) %> +<% content_for :table_filter_title do %> + <%= t('spree.filter') %> +<% end %> + +<% content_for :table_filter do %> + <%= + search_form_for( + [:admin, @search], + url: admin_stock_location_stock_movements_path( + params[:stock_location_id] + )) do |f| + %> +
+
+
+ <%= label_tag :q_variant_sku_eq, "#{t('spree.variant')} #{t('spree.sku')}" %> + <%= f.text_field :variant_sku_eq, value: params[:q][:variant_sku_eq] %> +
+
+
+ +
+ +
+ <%= button_tag t('spree.filter_results'), class: 'btn btn-primary' %> +
+ <% end %> +<% end %> + <% if @stock_movements.any? %> diff --git a/backend/spec/controllers/spree/admin/stock_movements_controller_spec.rb b/backend/spec/controllers/spree/admin/stock_movements_controller_spec.rb new file mode 100644 index 00000000000..fffd98e33a1 --- /dev/null +++ b/backend/spec/controllers/spree/admin/stock_movements_controller_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'spec_helper' + +module Spree + module Admin + describe StockMovementsController, type: :controller do + stub_authorization! + + let!(:stock_location) do + create( + :stock_location_with_items + ) + end + + let!(:stock_movement_1) do + create( + :stock_movement, + stock_item: stock_location.stock_items.first + ) + end + + let!(:stock_movement_2) do + create( + :stock_movement, + stock_item: stock_location.stock_items.last + ) + end + + describe '#index' do + subject { get :index, params: params } + + context 'with no params' do + let(:params) { { stock_location_id: stock_location.id } } + + it 'responds with a successful status code' do + subject + + expect(response).to be_successful + end + + it 'responds with all the stock locations stock movements' do + subject + + expect(assigns[:stock_movements]).to contain_exactly( + stock_movement_1, + stock_movement_2 + ) + end + end + + context 'with search parameters' do + let(:params) do + { + stock_location_id: stock_location.id, + q: { + variant_sku_eq: stock_movement_1.stock_item.variant.sku + } + } + end + + it 'responds with a successful status code' do + subject + + expect(response).to be_successful + end + + it 'responds with the stock movements that match the search criteria' do + subject + + expect(assigns[:stock_movements]).to contain_exactly( + stock_movement_1, + ) + end + end + end + end + end +end diff --git a/core/app/models/spree/stock_movement.rb b/core/app/models/spree/stock_movement.rb index c4c11cf929a..999c32447d8 100644 --- a/core/app/models/spree/stock_movement.rb +++ b/core/app/models/spree/stock_movement.rb @@ -4,6 +4,7 @@ module Spree class StockMovement < Spree::Base belongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movements, optional: true belongs_to :originator, polymorphic: true, optional: true + has_one :variant, through: :stock_item after_create :update_stock_item_quantity @@ -12,6 +13,7 @@ class StockMovement < Spree::Base scope :recent, -> { order(created_at: :desc) } + self.whitelisted_ransackable_associations = %w[variant] self.whitelisted_ransackable_attributes = ['quantity'] def readonly? diff --git a/core/spec/models/spree/stock_movement_spec.rb b/core/spec/models/spree/stock_movement_spec.rb index 5191f7ab3d5..19e3b8ad8fc 100644 --- a/core/spec/models/spree/stock_movement_spec.rb +++ b/core/spec/models/spree/stock_movement_spec.rb @@ -11,6 +11,10 @@ expect(subject).to respond_to(:stock_item) end + it 'should have a variant' do + expect(subject).to respond_to(:variant) + end + it 'is readonly unless new' do subject.save expect {