I am trying to override Solidus code and wanted to know what my options were?
In particular I am trying to change the Address to make the phone optional.
I am trying to override Solidus code and wanted to know what my options were?
In particular I am trying to change the Address to make the phone optional.
Solidus aims to provide a better customization interface - they want to avoid you opening up the Solidus/Spree classes - because it increases the chances that you break the system.
The original answer was broken for Rails 6.0 (Zeitwerk changes). For Rails 6 there is untested example code taken from a github issue.
# app/decorators/your_app/spree/address/make_phone_optional_decorator.rb
module YourApp::Spree::Address::MakePhoneOptionalDecorator
def require_phone?
false
end
Spree::Address.prepend self
end
Be sure to name the files *_decorator.rb
, like this they will automatically been picked up by solidus.
Original answer:
app/models/spree
and prepending additional spree namespaces if requiredconfig/initializers/spree.rb
In the actual problem - there is no extension for a Spree Address, a decorator won't work because we are not calling the code (say in a view we have written), so we need to change the Spree code with prepend.
app/models/spree/optional_phone_address.rb
module Spree
module OptionalPhoneAddress
# require_phone?
# - no longer require a phone
# - a * no longer appears next to phone in the view
#
def require_phone?
false
end
end
end
config/initializers/spree.rb
#
# Customization of Spree Code
#
Spree::Address.prepend Spree::OptionalPhoneAddress