diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index ca002fe08a..a4fe70c71b 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -12,7 +12,8 @@ class ShopController < BaseController def products unless @products = current_order_cycle.andand .products_distributed_by(@distributor).andand - .select(&:has_stock?) + .select(&:has_stock?).andand + .sort_by {|p| p.name } render json: "", status: 404 end end diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 03e544ca19..8c83de891d 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -87,9 +87,9 @@ describe ShopController do describe "returning products" do let(:product) { create(:product) } let(:order_cycle) { create(:order_cycle, distributors: [d], coordinator: create(:distributor_enterprise)) } + let(:exchange) { Exchange.find(order_cycle.exchanges.to_enterprises(d).outgoing.first.id) } before do - exchange = Exchange.find(order_cycle.exchanges.to_enterprises(d).outgoing.first.id) exchange.variants << product.master end @@ -99,6 +99,16 @@ describe ShopController do response.should be_success end + it "alphabetizes products" do + p1 = create(:product, name: "abc") + p2 = create(:product, name: "def") + exchange.variants << p1.master + exchange.variants << p2.master + controller.stub(:current_order_cycle).and_return order_cycle + xhr :get, :products + assigns[:products].should == [p1, p2, product].sort_by{|p| p.name } + end + it "does not return products if no order_cycle is selected" do controller.stub(:current_order_cycle).and_return nil xhr :get, :products