Adding sorting

This commit is contained in:
Will Marshall
2014-02-04 14:44:32 +11:00
parent 74f8fe46c6
commit e5c90e80f5
2 changed files with 13 additions and 2 deletions

View File

@@ -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

View File

@@ -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