Skip to content

Commit

Permalink
Merge pull request solidusio#17 from bonobos/fix-variant-search-filter
Browse files Browse the repository at this point in the history
Fix in stock check for api variant search
  • Loading branch information
richardnuno committed Feb 3, 2016
2 parents e43458e + ed51630 commit 25844cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/app/controllers/spree/api/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def scope
end

variants = variants.accessible_by(current_ability, :read)
variants = variants.in_stock if params[:in_stock_only] || cannot?(:view_out_of_stock, Spree::Variant)
variants = variants.in_stock if params[:in_stock_only] == "true" || cannot?(:view_out_of_stock, Spree::Variant)
variants
end

Expand Down
56 changes: 43 additions & 13 deletions api/spec/controllers/spree/api/variants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,57 @@ module Spree
end

context "stock filtering" do
subject { api_get :index, in_stock_only: true }
subject { api_get :index, in_stock_only: in_stock_only }

context "variant is out of stock" do
before do
variant.stock_items.update_all(count_on_hand: 0)
context "in stock requested" do
let(:in_stock_only) { "true" }

context "variant is out of stock" do
before do
variant.stock_items.update_all(count_on_hand: 0)
end

it "is not returned in the results" do
subject
expect(json_response["variants"].count).to eq 0
end
end

it "is not returned in the results" do
subject
expect(json_response["variants"].count).to eq 0
context "variant is in stock" do
before do
variant.stock_items.update_all(count_on_hand: 10)
end

it "is returned in the results" do
subject
expect(json_response["variants"].count).to eq 1
end
end
end

context "variant is in stock" do
before do
variant.stock_items.update_all(count_on_hand: 10)
context "out of stock requested" do
let(:in_stock_only) { "false" }

context "variant is out of stock" do
before do
variant.stock_items.update_all(count_on_hand: 0)
end

it "is returned in the results" do
subject
expect(json_response["variants"].count).to eq 1
end
end

it "is returned in the results" do
subject
expect(json_response["variants"].count).to eq 1
context "variant is in stock" do
before do
variant.stock_items.update_all(count_on_hand: 10)
end

it "is returned in the results" do
subject
expect(json_response["variants"].count).to eq 1
end
end
end
end
Expand Down

0 comments on commit 25844cc

Please sign in to comment.