Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize email required checks #3879

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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 @@ -750,10 +750,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