Skip to content

Commit

Permalink
Be consistent with method names checking if a field is required
Browse files Browse the repository at this point in the history
Across the repo the `<field-name>_required?` format is the most
common.
  • Loading branch information
elia committed Apr 30, 2021
1 parent bcd714d commit 8bab13a
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 @@ -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

0 comments on commit 8bab13a

Please sign in to comment.