Create new api routes/views/controllers for bulk product edit

This commit is contained in:
Rob H
2013-07-10 11:06:42 +05:30
committed by Rohan Mitchell
parent 8f6ec9bd9b
commit 97c03b50cc
9 changed files with 113 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,3 @@
object @enterprise
attributes :id, :name

View File

@@ -0,0 +1,2 @@
collection @products.order('id ASC')
extends "spree/api/products/bulk_show"

View File

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

View File

@@ -0,0 +1,2 @@
collection @variants
extends "spree/api/variants/bulk_show"

View File

@@ -0,0 +1,3 @@
object @variant
attributes :id, :options_text, :price, :on_hand

View File

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

View File

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

View File

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