From cfbf4d5bc4f1e92f4c4838a2c604fba6fc5ecf02 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 17 Jan 2014 10:35:40 +0800 Subject: [PATCH] Adding attributes to Order Managment API --- .../spree/api/line_items/bulk_show.v1.rabl | 3 ++ app/views/spree/api/orders/bulk_show.v1.rabl | 8 ++++- .../spree/api/orders_controller_spec.rb | 36 +++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 app/views/spree/api/line_items/bulk_show.v1.rabl diff --git a/app/views/spree/api/line_items/bulk_show.v1.rabl b/app/views/spree/api/line_items/bulk_show.v1.rabl new file mode 100644 index 0000000000..c699d4b89e --- /dev/null +++ b/app/views/spree/api/line_items/bulk_show.v1.rabl @@ -0,0 +1,3 @@ +object @line_item +attributes :id, :quantity, :max_quantity +node( :supplier ) { |li| partial 'spree/api/enterprises/bulk_show', :object => li.product.supplier } \ No newline at end of file diff --git a/app/views/spree/api/orders/bulk_show.v1.rabl b/app/views/spree/api/orders/bulk_show.v1.rabl index 5642032975..9454ebb733 100644 --- a/app/views/spree/api/orders/bulk_show.v1.rabl +++ b/app/views/spree/api/orders/bulk_show.v1.rabl @@ -1,2 +1,8 @@ object @order -attributes :id \ No newline at end of file +attributes :id, :email +node( :completed_at ) { |order| order.completed_at.blank? ? "" : order.completed_at.strftime("%F %T") } +node( :line_items ) do |order| + order.line_items.order('id ASC').map do |line_item| + partial 'spree/api/line_items/bulk_show', :object => line_item + end +end \ No newline at end of file diff --git a/spec/controllers/spree/api/orders_controller_spec.rb b/spec/controllers/spree/api/orders_controller_spec.rb index 2d70e37fed..685325b063 100644 --- a/spec/controllers/spree/api/orders_controller_spec.rb +++ b/spec/controllers/spree/api/orders_controller_spec.rb @@ -6,11 +6,15 @@ module Spree include Spree::Api::TestingSupport::Helpers render_views - - let!(:order1) { FactoryGirl.create(:order) } - let!(:line_item1) { FactoryGirl.create(:line_item) } - let!(:line_item2) { FactoryGirl.create(:line_item) } - let(:attributes) { [:id] } + let!(:order1) { FactoryGirl.create(:order, :state => 'complete', :completed_at => Time.now ) } + let!(:order2) { FactoryGirl.create(:order, :state => 'complete', :completed_at => Time.now ) } + let!(:order3) { FactoryGirl.create(:order, :state => 'complete', :completed_at => Time.now ) } + let!(:line_item1) { FactoryGirl.create(:line_item, :order => order1) } + let!(:line_item2) { FactoryGirl.create(:line_item, :order => order2) } + let!(:line_item3) { FactoryGirl.create(:line_item, :order => order2) } + let!(:line_item4) { FactoryGirl.create(:line_item, :order => order3) } + let(:order_attributes) { [:id, :email, :completed_at, :line_items] } + let(:line_item_attributes) { [:id, :quantity, :max_quantity, :supplier] } before do stub_authentication! @@ -18,10 +22,28 @@ module Spree end context "as a normal user" do - it "retrieves a list of products with appropriate attributes" do + before :each do spree_get :index, { :template => 'bulk_index', :format => :json } + end + + it "retrieves a list of orders with appropriate attributes, including line items with appropriate attributes" do keys = json_response.first.keys.map{ |key| key.to_sym } - attributes.all?{ |attr| keys.include? attr }.should == true + order_attributes.all?{ |attr| keys.include? attr }.should == true + end + + it "retrieves a list of line items with appropriate attributes" do + li_keys = json_response.first['line_items'].first.keys.map{ |key| key.to_sym } + line_item_attributes.all?{ |attr| li_keys.include? attr }.should == true + end + + it "sorts orders in ascending id order" do + ids = json_response.map{ |order| order['id'] } + ids[0].should < ids[1] + ids[1].should < ids[2] + end + + it "formats completed_at to 'yyyy-mm-dd hh:mm'" do + json_response.map{ |order| order['completed_at'] }.all?{ |a| a.match("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$") }.should == true end end end