mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Store customer email as lower case
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user