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

Add preference for phone validation #3685

Merged
merged 2 commits into from
Oct 3, 2020
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
6 changes: 3 additions & 3 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ def active_merchant_hash

# @todo Remove this from the public API if possible.
# @return [true] whether or not the address requires a phone number to be
# valid
# present
def require_phone?
true
Spree::Config[:address_requires_phone]
end

# @todo Remove this from the public API if possible.
# @return [true] whether or not the address requires a zipcode to be valid
# @return [true] whether or not the address requires a zipcode to be present
seand7565 marked this conversation as resolved.
Show resolved Hide resolved
def require_zipcode?
true
end
Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module Spree
class AppConfiguration < Preferences::Configuration
# Preferences (alphabetized to more easily lookup particular preferences)

# @!attribute [rw] address_requires_phone
# @return [Boolean] should phone number be required (default: +true+)
preference :address_requires_phone, :boolean, default: true

# @!attribute [rw] address_requires_state
# @return [Boolean] should state/state_name be required (default: +true+)
preference :address_requires_state, :boolean, default: true
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@
end

context "phone not required" do
before { allow(address).to receive_messages require_phone?: false }
before { stub_spree_preferences(address_requires_phone: false) }

it "shows no errors when phone is blank" do
it "is valid when phone is blank" do
address.phone = ""
address.valid?
expect(address.errors[:phone].size).to eq 0
Expand Down
16 changes: 6 additions & 10 deletions guides/source/developers/users/addresses.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,17 @@ You may want to alter Solidus's address requirements for your store. For
example, if you do not require customer phone numbers in order for them to check
out.

Right now, you need to [monkey-patch][monkey-patch] the `Spree::Address` model
in order to change its requirements. For example, you could prepend your custom
behavior that redefines `Spree::Address`'s `require_phone?` method:
In that case, you can set the `address_requires_phone` preference to `false` in
an initializer.

```ruby
module PhoneNotRequired
def require_phone?
false
end
Spree::Config.configure do |config|
config.address_requires_phone = false
end

Spree::Address.prepend(PhoneNotRequired)
```

Similarly, if you ship to countries that don't require postal codes, like Hong
With the exception of phone number, you need to [monkey-patch][monkey-patch] the `Spree::Address` model
in order to change its requirements. If you ship to countries that don't require postal codes, like Hong
Kong or Macau, you may want to make postal codes optional instead of required.

Right now, you can monkey-patch the `Spree::Address` model in order to remove or
Expand Down