-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate camelize and classify lookup
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.
- Loading branch information
Showing
8 changed files
with
80 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
require "action_policy/ext/symbol_camelize" | ||
using ActionPolicy::Ext::SymbolCamelize | ||
|
||
class TestSymbolClassify < Minitest::Test | ||
def test_simple_name | ||
assert_equal "Test", :test.camelize | ||
end | ||
|
||
def test_underscored_name | ||
assert_equal "TeStO", :te_st_o.camelize | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
# Skip this test if the lookup chain is already defined. This is the case when | ||
# *not* running in isolated mode (`bundle exec rake test:isolated`). | ||
return if defined?(ActionPolicy::LookupChain) | ||
|
||
require_relative "./dummy/config/environment" | ||
require "test_helper" | ||
|
||
class EntityPolicy; end | ||
|
||
class TestLookupChain < Minitest::Test | ||
def test_symbol_singular | ||
assert_equal( | ||
EntityPolicy, | ||
ActionPolicy.lookup(:entity) | ||
) | ||
end | ||
|
||
def test_symbol_plural | ||
assert_equal( | ||
EntityPolicy, | ||
ActionPolicy.lookup(:entities) | ||
) | ||
end | ||
end |