mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Customers controller serializes data for json requests, just renders view without data for html
This commit is contained in:
@@ -1,4 +1,28 @@
|
||||
module Admin
|
||||
class CustomersController < ResourceController
|
||||
before_filter :load_managed_shops, only: :index, if: :html_request?
|
||||
|
||||
def index
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json do
|
||||
render json: ActiveModel::ArraySerializer.new( @collection,
|
||||
each_serializer: Api::Admin::CustomerSerializer, spree_current_user: spree_current_user
|
||||
).to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def collection
|
||||
return Customer.where("1=0") if html_request? || params[:enterprise_id].nil?
|
||||
enterprise = Enterprise.managed_by(spree_current_user).find_by_id(params[:enterprise_id])
|
||||
Customer.of(enterprise)
|
||||
end
|
||||
|
||||
def load_managed_shops
|
||||
@shops = Enterprise.managed_by(spree_current_user).is_distributor
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -58,4 +58,8 @@ Spree::Admin::BaseController.class_eval do
|
||||
"Until you set these up, customers will not be able to shop at this hub."
|
||||
end
|
||||
end
|
||||
|
||||
def html_request?
|
||||
request.format.html?
|
||||
end
|
||||
end
|
||||
|
||||
3
app/serializers/api/admin/customer_serializer.rb
Normal file
3
app/serializers/api/admin/customer_serializer.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Api::Admin::CustomerSerializer < ActiveModel::Serializer
|
||||
attributes :id, :email, :enterprise_id, :user_id, :code
|
||||
end
|
||||
62
spec/controllers/admin/customers_controller_spec.rb
Normal file
62
spec/controllers/admin/customers_controller_spec.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
describe Admin::CustomersController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
|
||||
describe "index" do
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
let(:another_enterprise) { create(:distributor_enterprise) }
|
||||
|
||||
context "html" do
|
||||
before do
|
||||
controller.stub spree_current_user: enterprise.owner
|
||||
end
|
||||
|
||||
it "returns an empty @collection" do
|
||||
spree_get :index, format: :html
|
||||
expect(assigns(:collection)).to eq []
|
||||
end
|
||||
end
|
||||
|
||||
context "json" do
|
||||
let!(:customer) { create(:customer, enterprise: enterprise) }
|
||||
|
||||
context "where I manage the enterprise" do
|
||||
before do
|
||||
controller.stub spree_current_user: enterprise.owner
|
||||
end
|
||||
|
||||
context "and enterprise_id is given in params" do
|
||||
let(:params) { { format: :json, enterprise_id: enterprise.id } }
|
||||
|
||||
it "scopes @collection to customers of that enterprise" do
|
||||
spree_get :index, params
|
||||
expect(assigns(:collection)).to eq [customer]
|
||||
end
|
||||
|
||||
it "serializes the data" do
|
||||
expect(ActiveModel::ArraySerializer).to receive(:new)
|
||||
spree_get :index, params
|
||||
end
|
||||
end
|
||||
|
||||
context "and enterprise_id is not given in params" do
|
||||
it "returns an empty collection" do
|
||||
spree_get :index, format: :json
|
||||
expect(assigns(:collection)).to eq []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "and I do not manage the enterprise" do
|
||||
before do
|
||||
controller.stub spree_current_user: another_enterprise.owner
|
||||
end
|
||||
|
||||
it "returns an empty collection" do
|
||||
spree_get :index, format: :json
|
||||
expect(assigns(:collection)).to eq []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user