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