mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Create new api routes/views/controllers for bulk product edit
This commit is contained in:
12
app/controllers/spree/api/enterprises_controller.rb
Normal file
12
app/controllers/spree/api/enterprises_controller.rb
Normal 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
|
||||
3
app/views/spree/api/enterprises/bulk_show.v1.rabl
Normal file
3
app/views/spree/api/enterprises/bulk_show.v1.rabl
Normal file
@@ -0,0 +1,3 @@
|
||||
object @enterprise
|
||||
|
||||
attributes :id, :name
|
||||
2
app/views/spree/api/products/bulk_index.v1.rabl
Normal file
2
app/views/spree/api/products/bulk_index.v1.rabl
Normal file
@@ -0,0 +1,2 @@
|
||||
collection @products.order('id ASC')
|
||||
extends "spree/api/products/bulk_show"
|
||||
11
app/views/spree/api/products/bulk_show.v1.rabl
Normal file
11
app/views/spree/api/products/bulk_show.v1.rabl
Normal 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
|
||||
|
||||
2
app/views/spree/api/variants/bulk_index.v1.rabl
Normal file
2
app/views/spree/api/variants/bulk_index.v1.rabl
Normal file
@@ -0,0 +1,2 @@
|
||||
collection @variants
|
||||
extends "spree/api/variants/bulk_show"
|
||||
3
app/views/spree/api/variants/bulk_show.v1.rabl
Normal file
3
app/views/spree/api/variants/bulk_show.v1.rabl
Normal file
@@ -0,0 +1,3 @@
|
||||
object @variant
|
||||
|
||||
attributes :id, :options_text, :price, :on_hand
|
||||
@@ -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
|
||||
|
||||
44
spec/controllers/spree/api/products_controller_spec.rb
Normal file
44
spec/controllers/spree/api/products_controller_spec.rb
Normal 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
|
||||
34
spec/controllers/spree/api/variants_controller_spec.rb
Normal file
34
spec/controllers/spree/api/variants_controller_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user