From 49febc63338207cca05b4ca02fa7afed2263ec40 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 15 Apr 2016 15:35:43 +1000 Subject: [PATCH] Store customer email as lower case --- app/models/customer.rb | 6 ++++++ app/models/spree/order_decorator.rb | 4 ++-- spec/models/customer_spec.rb | 22 ++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index bcafd3246b..34f62a6aa6 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -4,6 +4,8 @@ class Customer < ActiveRecord::Base belongs_to :enterprise belongs_to :user, class_name: Spree.user_class + before_validation :downcase_email + validates :code, uniqueness: { scope: :enterprise_id, allow_blank: true, allow_nil: true } validates :email, presence: true, uniqueness: { scope: :enterprise_id, message: I18n.t('validation_msg_is_associated_with_an_exising_customer') } validates :enterprise_id, presence: true @@ -14,6 +16,10 @@ class Customer < ActiveRecord::Base private + def downcase_email + email.andand.downcase! + end + def associate_user self.user = user || Spree::User.find_by_email(email) end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 8e3c6b7bb4..f0e9324185 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -284,11 +284,11 @@ Spree::Order.class_eval do def customer_is_valid? return true unless require_customer? - customer.present? && customer.enterprise_id == distributor_id && customer.email == (user.andand.email || email) + customer.present? && customer.enterprise_id == distributor_id && customer.email == email_for_customer end def email_for_customer - user.andand.email || email + (user.andand.email || email).andand.downcase end def associate_customer diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index 0e43ce9df5..f2ac14e9b2 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -6,15 +6,25 @@ describe Customer, type: :model do let!(:user2) { create(:user) } let!(:enterprise) { create(:distributor_enterprise) } + it "associates no user using non-existing email" do + c = Customer.create(enterprise: enterprise, email: 'some-email-not-associated-with-a-user@email.com') + expect(c.user).to be_nil + end + it "associates an existing user using email" do - c1 = Customer.create(enterprise: enterprise, email: 'some-email-not-associated-with-a-user@email.com') - expect(c1.user).to be_nil + non_existing_email = 'some-email-not-associated-with-a-user@email.com' + c1 = Customer.create(enterprise: enterprise, email: non_existing_email, user: user1) + expect(c1.user).to eq user1 + expect(c1.email).to eq non_existing_email + expect(c1.email).to_not eq user1.email - c2 = Customer.create(enterprise: enterprise, email: 'some-email-not-associated-with-a-user@email.com', user: user1) - expect(c2.user).to eq user1 + c2 = Customer.create(enterprise: enterprise, email: user2.email) + expect(c2.user).to eq user2 + end - c3 = Customer.create(enterprise: enterprise, email: user2.email) - expect(c3.user).to eq user2 + it "associates an existing user using email case-insensitive" do + c = Customer.create(enterprise: enterprise, email: user2.email.upcase) + expect(c.user).to eq user2 end end end