From 97c03b50cc901c08474dca1de3249bcf696e5bbb Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 10 Jul 2013 11:06:42 +0530 Subject: [PATCH] Create new api routes/views/controllers for bulk product edit --- .../spree/api/enterprises_controller.rb | 12 +++++ .../spree/api/enterprises/bulk_show.v1.rabl | 3 ++ .../spree/api/products/bulk_index.v1.rabl | 2 + .../spree/api/products/bulk_show.v1.rabl | 11 +++++ .../spree/api/variants/bulk_index.v1.rabl | 2 + .../spree/api/variants/bulk_show.v1.rabl | 3 ++ config/routes.rb | 2 + .../spree/api/products_controller_spec.rb | 44 +++++++++++++++++++ .../spree/api/variants_controller_spec.rb | 34 ++++++++++++++ 9 files changed, 113 insertions(+) create mode 100644 app/controllers/spree/api/enterprises_controller.rb create mode 100644 app/views/spree/api/enterprises/bulk_show.v1.rabl create mode 100644 app/views/spree/api/products/bulk_index.v1.rabl create mode 100644 app/views/spree/api/products/bulk_show.v1.rabl create mode 100644 app/views/spree/api/variants/bulk_index.v1.rabl create mode 100644 app/views/spree/api/variants/bulk_show.v1.rabl create mode 100644 spec/controllers/spree/api/products_controller_spec.rb create mode 100644 spec/controllers/spree/api/variants_controller_spec.rb diff --git a/app/controllers/spree/api/enterprises_controller.rb b/app/controllers/spree/api/enterprises_controller.rb new file mode 100644 index 0000000000..ace0c8639e --- /dev/null +++ b/app/controllers/spree/api/enterprises_controller.rb @@ -0,0 +1,12 @@ +module Spree + module Api + class EnterprisesController < Spree::Api::BaseController + respond_to :json + + def show + @enterprise = Enterprise.find(params[:id]) + respond_with(@enterprise) + end + end + end +end \ No newline at end of file diff --git a/app/views/spree/api/enterprises/bulk_show.v1.rabl b/app/views/spree/api/enterprises/bulk_show.v1.rabl new file mode 100644 index 0000000000..5a40b74aa1 --- /dev/null +++ b/app/views/spree/api/enterprises/bulk_show.v1.rabl @@ -0,0 +1,3 @@ +object @enterprise + +attributes :id, :name \ No newline at end of file diff --git a/app/views/spree/api/products/bulk_index.v1.rabl b/app/views/spree/api/products/bulk_index.v1.rabl new file mode 100644 index 0000000000..dfb9f20f2a --- /dev/null +++ b/app/views/spree/api/products/bulk_index.v1.rabl @@ -0,0 +1,2 @@ +collection @products.order('id ASC') +extends "spree/api/products/bulk_show" diff --git a/app/views/spree/api/products/bulk_show.v1.rabl b/app/views/spree/api/products/bulk_show.v1.rabl new file mode 100644 index 0000000000..9dbaaab402 --- /dev/null +++ b/app/views/spree/api/products/bulk_show.v1.rabl @@ -0,0 +1,11 @@ +object @product +attributes :id, :name, :price, :on_hand +node( :available_on ) { |p| p.available_on.strftime("%F %T") } +node( :permalink_live ) { |p| p.permalink } +node( :supplier ) do |p| + partial 'spree/api/enterprises/bulk_show', :object => p.supplier +end +node( :variants ) do |p| + partial 'spree/api/variants/bulk_index', :object => p.variants.order('id ASC') +end + diff --git a/app/views/spree/api/variants/bulk_index.v1.rabl b/app/views/spree/api/variants/bulk_index.v1.rabl new file mode 100644 index 0000000000..69f1b82f5f --- /dev/null +++ b/app/views/spree/api/variants/bulk_index.v1.rabl @@ -0,0 +1,2 @@ +collection @variants +extends "spree/api/variants/bulk_show" diff --git a/app/views/spree/api/variants/bulk_show.v1.rabl b/app/views/spree/api/variants/bulk_show.v1.rabl new file mode 100644 index 0000000000..3fed62bf99 --- /dev/null +++ b/app/views/spree/api/variants/bulk_show.v1.rabl @@ -0,0 +1,3 @@ +object @variant + +attributes :id, :options_text, :price, :on_hand \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9445e01489..ee0a1e0579 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,8 @@ Spree::Core::Engine.routes.prepend do match '/admin/reports/order_cycles' => 'admin/reports#order_cycles', :as => "order_cycles_admin_reports", :via => [:get, :post] match '/admin/products/bulk_edit' => 'admin/products#bulk_edit', :as => "bulk_edit_admin_products" + match '/api/enterprises/:id' => 'api/enterprises#show', :via => :get, :defaults => { :format => 'json' } + namespace :admin do resources :products do get :bulk_index, :on => :collection, :as => :bulk_index diff --git a/spec/controllers/spree/api/products_controller_spec.rb b/spec/controllers/spree/api/products_controller_spec.rb new file mode 100644 index 0000000000..f0895eca35 --- /dev/null +++ b/spec/controllers/spree/api/products_controller_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' +require 'spree/api/testing_support/helpers' + +module Spree + describe Spree::Api::ProductsController do + include Spree::Api::TestingSupport::Helpers + render_views + + let!(:product1) { FactoryGirl.create(:product) } + let!(:product2) { FactoryGirl.create(:product) } + let!(:product3) { FactoryGirl.create(:product) } + let(:attributes) { [:id, :name, :supplier, :price, :on_hand, :available_on, :permalink_live] } + + before do + stub_authentication! + Spree.user_class.stub :find_by_spree_api_key => current_api_user + end + + context "as a normal user" do + it "retrieves a list of products with appropriate attributes" do + spree_get :index, { :template => 'bulk_index', :format => :json } + keys = json_response.first.keys.map{ |key| key.to_sym } + attributes.all?{ |attr| keys.include? attr }.should == true + end + + it "sorts products in ascending id order" do + spree_get :index, { :template => 'bulk_index', :format => :json } + ids = json_response.map{ |product| product['id'] }mate + ids[0].should < ids[1] + ids[1].should < ids[2] + end + + it "formats available_on to 'yyyy-mm-dd hh:mm'" do + spree_get :index, { :template => 'bulk_index', :format => :json } + json_response.map{ |product| product['available_on'] }.all?{ |a| a.match("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$") }.should == true + end + + it "returns permalink as permalink_live" do + spree_get :index, { :template => 'bulk_index', :format => :json } + json_response.detect{ |product| product['id'] == product1.id }['permalink_live'].should == product1.permalink + end + end + end +end \ No newline at end of file diff --git a/spec/controllers/spree/api/variants_controller_spec.rb b/spec/controllers/spree/api/variants_controller_spec.rb new file mode 100644 index 0000000000..c9449dc4fe --- /dev/null +++ b/spec/controllers/spree/api/variants_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +require 'spree/api/testing_support/helpers' + +module Spree + describe Spree::Api::VariantsController do + include Spree::Api::TestingSupport::Helpers + render_views + + let!(:variant1) { FactoryGirl.create(:variant) } + let!(:variant2) { FactoryGirl.create(:variant) } + let!(:variant3) { FactoryGirl.create(:variant) } + let(:attributes) { [:id, :options_text, :price, :on_hand] } + + before do + stub_authentication! + Spree.user_class.stub :find_by_spree_api_key => current_api_user + end + + context "as a normal user" do + it "retrieves a list of variants with appropriate attributes" do + spree_get :index, { :template => 'bulk_index', :format => :json } + keys = json_response.first.keys.map{ |key| key.to_sym } + attributes.all?{ |attr| keys.include? attr }.should == true + end + + it "sorts variants in ascending id order" do + spree_get :index, { :template => 'bulk_index', :format => :json } + ids = json_response.map{ |variant| variant['id'] } + ids[0].should < ids[1] + ids[1].should < ids[2] + end + end + end +end \ No newline at end of file