Skip to content

Commit

Permalink
Merge pull request #3680 from jacquesporveau/jacquesporveau/stock_mov…
Browse files Browse the repository at this point in the history
…ements_admin_1

[Admin] Add filter feature for stock movements
  • Loading branch information
spaghetticode authored Jul 14, 2020
2 parents 7073fd9 + be40742 commit 8841b69
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions backend/app/views/spree/admin/stock_movements/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
%>
<div class ="row">
<div class="field-block col-12 col-md-6 col-lg-4 col-xl-3">
<div class="field">
<%= label_tag :q_variant_sku_eq, "#{t('spree.variant')} #{t('spree.sku')}" %>
<%= f.text_field :variant_sku_eq, value: params[:q][:variant_sku_eq] %>
</div>
</div>
</div>

<div class="clearfix"></div>

<div class="actions filter-actions">
<%= button_tag t('spree.filter_results'), class: 'btn btn-primary' %>
</div>
<% end %>
<% end %>

<% if @stock_movements.any? %>
<table class="index" id='listing_stock_movements'>
<colgroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions core/app/models/spree/stock_movement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions core/spec/models/spree/stock_movement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8841b69

Please sign in to comment.