From 0ed1eeffd6c68d9345eb1d1a32fcd8ec9e4e55ce Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 27 Mar 2015 14:05:58 +1100 Subject: [PATCH] When registering a new enterprise, if user is signed in, owns a hub and is not making a producer, the new enterprise becomes a hub --- app/controllers/api/enterprises_controller.rb | 4 +++- spec/controllers/api/enterprises_controller_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/enterprises_controller.rb b/app/controllers/api/enterprises_controller.rb index d40d67409d..e51d21ef3c 100644 --- a/app/controllers/api/enterprises_controller.rb +++ b/app/controllers/api/enterprises_controller.rb @@ -63,7 +63,9 @@ module Api end def override_sells - params[:enterprise][:sells] = 'unspecified' + has_hub = current_api_user.enterprises.is_hub.any? + new_enterprise_is_producer = !!params[:enterprise][:is_primary_producer] + params[:enterprise][:sells] = (has_hub && !new_enterprise_is_producer) ? 'any' : 'unspecified' end def override_visible diff --git a/spec/controllers/api/enterprises_controller_spec.rb b/spec/controllers/api/enterprises_controller_spec.rb index e21a879d4f..ae6b02fac7 100644 --- a/spec/controllers/api/enterprises_controller_spec.rb +++ b/spec/controllers/api/enterprises_controller_spec.rb @@ -20,6 +20,19 @@ module Api Spree.user_class.stub :find_by_spree_api_key => enterprise_manager end + describe "creating an enterprise" do + let(:australia) { Spree::Country.find_by_name('Australia') } + let(:new_enterprise_params) { {enterprise: {name: 'name', email: 'email@example.com', address_attributes: {address1: '123 Abc Street', city: 'Northcote', zipcode: '3070', state_id: australia.states.first, country_id: australia.id } } } } + + it "creates as sells=any when it is not a producer" do + spree_post :create, new_enterprise_params + response.should be_success + + enterprise = Enterprise.last + enterprise.sells.should == 'any' + end + end + describe "submitting a valid image" do before do enterprise.stub(:update_attributes).and_return(true)