-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
Separate camelize and classify lookup #118
Conversation
55b680e
to
33d78ba
Compare
I have updated the PR to incorporate your ideas. I'd really like to add some tests around the Rails vs non-Rails behaviour but didn't quite manage to do it yet. I have explored two options:
The case where you'd have a symbol as a plural and lookup the policy as the singular was not tested previously but I'd really like to add it. I'd be super grateful for ideas on this! |
We run tests in isolation on CI, so Rails-specific tests (such as You can add one test under # test/rails/lookup_chain_test.rb
require_relative "./dummy/config/environment"
# we can skip this test file altogether if Action Policy has been loaded
return if defined?(ActionPolicy)
require "test_helper"
# or we can reload the lookup chain
# (that would require moving current `self.chain = ...` into a new `reload!` method)
ActionPolicy::LookupChain.reload! Similarly, the non-Rails test would include |
33d78ba
to
c1be553
Compare
Brilliant idea - Thank you very much for that! I was now able to update the branch with tests asserting the new behaviour. |
Previously, if you were using ActiveSupport, the lookup would use Rails' `String#classify` method. Without ActiveSupport it'd use a patched version which implemented classifying as camelizing a String. As an example, if the lookup was given a symbol `:users`, it'd behave as follows: a) Without ActiveSupport, look for `UsersPolicy`; b) With ActiveSupport, look for `UserPolicy`; This change does three things. First, we add a new lookup that uses `camelize`. This enables the lookup to find `UsersPolicy` when given `:users` in Rails. This is the new behaviour. Second, by changing the Symbol extension, we keep the old behaviour in the non-Rails working. Finally, we add a new lookup that is only used when `classify` is available on String and uses that to infer the polict. This ensures that finding `UserPolicy` still works in the case where we're in a Rails application.
c1be553
to
4d7c08f
Compare
Thanks! |
What is the purpose of this pull request?
I've noticed a difference between how the lookup for symbols works in a Rails vs a non-Rails context. When in a non-Rails context, the symbol gets camelized, whereas in Rails, we classify it. For example, if given
:users
, in a non-Rails environment, we're looking forUsersPolicy
. In Rails, we'd be looking forUserPolicy
.This PR updates the lookup to allow exact matches in Rails as well, e.g. when you don't want to or can't use Rails inflection. As a side-benefit, this PR makes the difference in behaviour between Rails and non-Rails a bit clearer.
What changes did you make? (overview)
First, we add a new lookup that uses
camelize
. By renaming the Symbol extension fromclassify
tocamelize
this maintains the old behaviour in the non-Rails case and allows the exact matching symbols in the Rails case (:users
->UsersPolicy
).Second, we change the existing lookup to check whether
classify
is available, so it's skipped in the non-Rails case. This second lookup keeps it working in the Rails context (:users
->UserPolicy
).Finally, we've updated the documentation to describe the added behaviour.
PR checklist: