-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Custom ransack attributes appear to be being reset after configuration #3706
Comments
Hey @tmtrademarked, thanks for reporting. Curious if this also happens in production where eager_load is enabled, just to be sure it's a code reloading issue, probably coming from Rails 6. Ah, are you using Zeitwerk or the classic autoloader? |
Good point - let me try to check that out. We're using zeitwerk in this application, which may be one of the relevant differences here. |
Ah - in production, it seems like this works as expected. I was able to modify the initializer and have the change take effect as expected. I wonder if this has to do with the implementation of this feature as a class method and the interaction between zeitwerk and the class itself. (I'm hazy on the details of the new zeitwerk framework, but maybe it doesn't reload the initializers as previously expected?) |
Yeah, I think that's the issue. I think it can be solved by adding a product decorator to the project, following the Zeitwerk expectations in terms of filename and content and it will be reloaded at each code reload (each request). |
I'm not totally sure what you mean - you mean in a product decorator (say, |
I mean to configure it only with the decorator. What I mean with "Zeitwerk expectations" is to have a file and a module/class inside that follow the same naming pattern, like: # app/decorators/your_app/spree/product/whitelist_raskackable_attributes.rb
module YourApp
module Spree
module Product
module WhitelistRansackableAttributes
# your code here
Spree::Product.prepend self
end
end
end
end Otherwise it won't get considered by Zeitwerk. |
I gave that approach a try - and can confirm that it works as expected, which makes sense. I guess the only question here is whether we should update the documentation to make this more obvious? |
Yes, that would be awesome. And I think it will work even prior Rails 6 and Zeitwerk so 👍. If you have time to submit a PR, it would be welcome. Thanks! |
This PR updates the documentation around customizing Ransack attributes to recommend the use of a decorator instead of an initializer. This allows the code to reload correctly when using Zeitwerk under Rails 6+. Addresses solidusio#3706
Sounds great - opened #3709 to address this. Thanks for the help here! |
This PR updates the documentation around customizing Ransack attributes to recommend the use of a decorator instead of an initializer. This allows the code to reload correctly when using Zeitwerk under Rails 6+. Addresses solidusio#3706
This PR updates the documentation around customizing Ransack attributes to recommend the use of a decorator instead of an initializer. This allows the code to reload correctly when using Zeitwerk under Rails 6+. Addresses solidusio#3706
Following the guide for customizing search fields, I have been trying to add a search field for a custom field on a model. When I follow the steps, though, it seems like the
whitelisted_ransackable_attributes
are being reset somewhere along the line after the configuration is applied.Solidus Version:
Solidus: 2.10.0
Rails: 6.0.3.2
Ruby: 2.6.6
To Reproduce
I added a new field to my product model called
product_type
, and wanted to add an admin field to allow searching by type. After adding an entry toconfig/initializers/spree.rb
, I can see the following behavior on application launch:... which looks like the configuration has been applied.
When adding my custom field
product_type
to an admin field, I added the following deface override:If I put a breakpoint in the controller before the view is rendered, I can see that the ransackable attributes of the Product object have reverted back to the default. In fact, adding a dirty hack to the controller when the product collection is loaded appears to be a workaround, but seems like it shouldn't be necessary:
Current behavior
When loading the products admin page, I get the following ActionView error:
Expected behavior
I would have expected the config initializer to persist across the lifetime of the application rather than being reloaded.
The text was updated successfully, but these errors were encountered: