(doc/guides) Unclear how to monkey patch e.g. Address to not require_phone? #4556
Replies: 14 comments
-
Hey @fwolfst, sorry about the confusion! The best place to put that code is in a decorator such as the following: # app/decorators/your_app/spree/address/make_phone_optional.rb
module YourApp::Spree::Address::MakePhoneOptional
def require_phone?
false
end
Spree::Address.prepend self
end (Just to be clear, the exact name of the decorator doesn't really matter as long as the namespace and module name match the file's path, in order to play nice with Zeitwerk. The name I'm using here is just the convention we adopt at Nebulab and in extensions.) The current guides are a bit outdated. In fact, we're working on a new version of the guides that explains these concepts much better, so there's no need to update the existing ones imo. |
Beta Was this translation helpful? Give feedback.
-
Thanks @aldesantis , unfortunately that one doesnt work either. |
Beta Was this translation helpful? Give feedback.
-
@fwolfst can you clarify? What do you see when you open a console and type |
Beta Was this translation helpful? Give feedback.
-
To be clear what else I tried: # app/models/spree/address/optional_phone.rb
module Spree::Address::OptionalPhone
def require_phone?
false
end
Spree::Address.prepend self
end $ pkill -f spring # just be sure
$ rails c
# ...
2.7.1 :001 > Spree::User.first.addresses.new.require_phone?
(0.4ms) SELECT sqlite_version(*)
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL ORDER BY "spree_users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> true |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Probably I have a typo in a file name or something, but then I probably have to go to an optician. If I execute |
Beta Was this translation helpful? Give feedback.
-
What I understand:
Now, I did find a way to modify # app/models/spree/address/optional_phone.rb
module Spree::Address::OptionalPhone
def require_phone?
false
end
# (does not work)
# Spree::Address.prepend self
end and # app/overrides/optional_phone_for_address.rb
module OptionalPhoneForAddress
Spree::Address.prepend Spree::Address::OptionalPhone
end Afterwards, I see desired change in the frontend and |
Beta Was this translation helpful? Give feedback.
-
Can you verify you're loading your decorators? Solidus injects something like this to application.rb:
You need to tell rails to load decorators folder as well:
|
Beta Was this translation helpful? Give feedback.
-
I added the snippet in the beginning of the Then I picked up that it probably has to be '../../app/**#....' - that worked! I only then saw that you referenced
Now what this tells us is ... the original advice by @aldesantis was great, and would have worked without modifications if the file name (and the module name) would have been *_decorator.rb / ...Decorator . :) I am sorry - this is most likely mentioned in the guides somewhere (pretty sure I've seen this), but it should be made explicit in all code examples throughout the guides, imho. As I had no time to read through the edgeguides, I leave it up to you if this issue can be closed. Thanks a lot @aldesantis and @softr8 , that was a timely and professional response. |
Beta Was this translation helpful? Give feedback.
-
Hi, I need help with this. I added a folder under the app called decorators then the folder with app_name then spree and finally my class with an overridden method. So path is app/decorators/app_name/spree/my_class.rb Inside that class now I have 2 methods, logo ( should override logo method from Spree::BaseHelper) and category_tree. In _header.html.erb I add <%= logo img_options: { class: 'img-responsive', width: '180' } %>and that gave me this error: ActionView::Template::Error (Nil location provided. Can't build URI.): In application file I added: config.to_prepare do Can someone point me to the next step or what did I do wrong? |
Beta Was this translation helpful? Give feedback.
-
@seki93 can you please direct your support request to the Solidus Slack? GitHub is only for reporting actual bugs. |
Beta Was this translation helpful? Give feedback.
-
Sounds like the desired outcome of the original discussion here might be that this guides page might need to be updated to be clearer about naming convention requirements. Is that fair? |
Beta Was this translation helpful? Give feedback.
-
This seems to be solved, and the overrides section in the new guides has updated instructions https://edgeguides.solidus.io/customization/customizing-the-core#using-overrides. @jarednorman @kennyadsl you think we can close this? |
Beta Was this translation helpful? Give feedback.
-
Yes, going to move it to Troubleshooting to keep track of it. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
I want Adresses not to require the phone number (besides other things) and was happy to find kind of official documentation about it immediately:
https://guides.solidus.io/developers/users/addresses.html
However, it is unclear to me where to put this code such that it gets loaded at the right time (probably related to Zeitwerk? just guessing). I spew it around at
lib
,app/lib
,config/initializers/solidus_overrides.rb
,config/initializers/spree.rb
, to no avail. I still see the star in the billing adress form indicating that the phone number input is a required field and inrails c
aSpree::Address
responds with true torequire_phone?
.Describe the solution you'd like
The guides could tell me a preferred (and working ;) ) location for the code.
Once somebody provided me with the insights, I can draft a PR for the guides.
Beta Was this translation helpful? Give feedback.
All reactions