From 3b5c3f41a2c04d82ee6d8c0ccc5cd3a4ac41a4bd Mon Sep 17 00:00:00 2001 From: Em-AK Date: Tue, 21 Mar 2017 18:20:14 +0100 Subject: [PATCH] Ensure created users are associated with customers When a user is created with the same email as existing customers Then the user is associated with these customers So that the user can access the private shops where he has been invited to before signup --- app/models/spree/user_decorator.rb | 5 +++++ spec/models/spree/user_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 092afa0f6b..3877428c86 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -19,6 +19,7 @@ Spree.user_class.class_eval do accepts_nested_attributes_for :ship_address attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit, :locale, :bill_address_attributes, :ship_address_attributes + after_create :associate_customers validate :limit_owned_enterprises @@ -64,6 +65,10 @@ Spree.user_class.class_eval do Delayed::Job.enqueue ConfirmSignupJob.new(id) end + def associate_customers + self.customers = Customer.where(email: email) + end + def can_own_more_enterprises? owned_enterprises(:reload).size < enterprise_limit end diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index 441965cfa8..8d1c0c0e18 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -77,6 +77,17 @@ describe Spree.user_class do end.to enqueue_job Delayed::PerformableMethod expect(Delayed::Job.last.payload_object.method_name).to eq(:send_on_create_confirmation_instructions_without_delay) end + + context "with the the same email as existing customers" do + let(:email) { Faker::Internet.email } + let!(:customer1) { create(:customer, user: nil, email: email) } + let!(:customer2) { create(:customer, user: nil, email: email) } + let!(:user) { create(:user, email: email) } + + it "should associate these customers with the created user" do + expect(user.customers.reload).to include customer1, customer2 + end + end end context "confirming email" do