Skip to content

Commit

Permalink
validate order's email against existing users
Browse files Browse the repository at this point in the history
see #3
  • Loading branch information
dedekm committed May 12, 2022
1 parent b77bb98 commit 3561eef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/models/boutique/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Boutique::Order < Boutique::ApplicationRecord
unless: :pending?

validate :validate_primary_address
validate :validate_email_not_already_registered, unless: :pending?

validates :primary_address,
presence: true,
if: :requires_address_and_not_pending?

aasm timestamps: true do
state :pending, initial: true
Expand Down Expand Up @@ -180,8 +185,16 @@ def validate_primary_address
errors.add(:primary_address, :blank)
end
end
end

def validate_email_not_already_registered
return if email.nil?
return if user.present?

if Folio::User.where(email:).exists?
errors.add(:email, :already_registered)
end
end
end
# == Schema Information
#
# Table name: boutique_orders
Expand Down
5 changes: 5 additions & 0 deletions config/locales/activerecord.cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ cs:
base:
missing_master_variant: Nemá vybranou hlavní variantu.
too_many_master_variants: Má vybrano více hlavních variant.

boutique/order:
attributes:
email:
already_registered: je již registrovaný

0 comments on commit 3561eef

Please sign in to comment.