-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Add option for accessible_by
querying strategy
#655
Merged
coorasse
merged 18 commits into
CanCanCommunity:develop
from
ghiculescu:querying-strategy-2
Dec 12, 2020
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5bcba2c
Add option for accessible_by querying strategy
ghiculescu 930c92c
add to readme
ghiculescu 07a4a99
lint
ghiculescu ce50d04
ooops, https://github.com/CanCanCommunity/cancancan/pull/605#discussi…
ghiculescu 82413d0
update tests for where behaviors differ between DBs
ghiculescu d9473ad
lint
ghiculescu 1a82c36
tweak some tests that are bad in old AR versions
ghiculescu ffd4976
more has many thorugh tests
ghiculescu e5411d7
regression
ghiculescu 7841df0
ugh, lint
ghiculescu 738dcf0
fix the last test
ghiculescu c66cba0
need to run the query
ghiculescu 5870049
get it working in 4.2
ghiculescu 9320646
lint AGAIN
ghiculescu 52dca53
Merge branch 'develop' into querying-strategy-2
ghiculescu 3c17def
accessible_by_strategy only works for Rails 5+
ghiculescu 0061402
Merge branch 'develop' into querying-strategy-2
coorasse 744ab77
Merge branch 'develop' into querying-strategy-2
coorasse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module CanCan | ||
def self.valid_accessible_by_strategies | ||
strategies = [:left_join] | ||
strategies << :subquery unless CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0') | ||
strategies | ||
end | ||
|
||
# Determines how CanCan should build queries when calling accessible_by, | ||
# if the query will contain a join. The default strategy is `:subquery`. | ||
# | ||
# # config/initializers/cancan.rb | ||
# CanCan.accessible_by_strategy = :subquery | ||
# | ||
# Valid strategies are: | ||
# - :subquery - Creates a nested query with all joins, wrapped by a | ||
# WHERE IN query. | ||
# - :left_join - Calls the joins directly using `left_joins`, and | ||
# ensures records are unique using `distinct`. Note that | ||
# `distinct` is not reliable in some cases. See | ||
# https://github.com/CanCanCommunity/cancancan/pull/605 | ||
def self.accessible_by_strategy | ||
@accessible_by_strategy || default_accessible_by_strategy | ||
end | ||
|
||
def self.default_accessible_by_strategy | ||
if CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0') | ||
# see https://github.com/CanCanCommunity/cancancan/pull/655 for where this was added | ||
# the `subquery` strategy (from https://github.com/CanCanCommunity/cancancan/pull/619 | ||
# only works in Rails 5 and higher | ||
:left_join | ||
else | ||
:subquery | ||
end | ||
end | ||
|
||
def self.accessible_by_strategy=(value) | ||
unless valid_accessible_by_strategies.include?(value) | ||
raise ArgumentError, "accessible_by_strategy must be one of #{valid_accessible_by_strategies.join(', ')}" | ||
end | ||
|
||
if value == :subquery && CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0') | ||
raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer' | ||
end | ||
|
||
@accessible_by_strategy = value | ||
end | ||
end | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this. very descriptive