Skip to content

Commit

Permalink
elia/normalize-email-required-checks: [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Jan 4, 2021
1 parent c7025ee commit a4849c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def states
before_create :create_token
before_create :link_by_email

validates :email, presence: true, if: :require_email
validates :email, presence: true, if: :email_required?
validates :email, 'spree/email' => true, allow_blank: true
validates :guest_token, presence: { allow_nil: true }
validates :number, presence: true, uniqueness: { allow_blank: true, case_sensitive: true }
Expand Down Expand Up @@ -883,10 +883,15 @@ def link_by_email
end

# Determine if email is required (we don't want validation errors before we hit the checkout)
def require_email
def email_required?
true unless new_record? || ['cart', 'address'].include?(state)
end

def require_email
Spree::Deprecation.warn "Use email_required? instead", caller(1)
email_required?
end

def ensure_inventory_units
if has_checkout_step?("delivery")
inventory_validator = Spree::Stock::InventoryValidator.new
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Spree
# Regression test for https://github.com/spree/spree/issues/2214
it "does not return two error messages when email is blank" do
order = Spree::Order.new
allow(order).to receive_messages(require_email: true)
allow(order).to receive_messages(email_required?: true)
order.valid?
expect(order.errors[:email]).to eq(["can't be blank"])
end
Expand Down

0 comments on commit a4849c0

Please sign in to comment.