Skip to content
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

Allow can with query on single resource #785

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/cancan/conditions_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ def matches_hash_conditions(adapter, subject, conditions)
end
end

def condition_match?(attribute, value)
def condition_match?(attribute, value) # rubocop:disable Metrics/MethodLength
kaspernj marked this conversation as resolved.
Show resolved Hide resolved
if defined?(ActiveRecord) &&
(value.is_a?(ActiveRecord::AssociationRelation) || value.is_a?(ActiveRecord::Relation))
return condition_match_query?(attribute, value)
end

case value
when Hash
hash_condition_match?(attribute, value)
Expand All @@ -99,6 +104,16 @@ def condition_match?(attribute, value)
end
end

def condition_match_query?(attribute, query)
selects = query.values[:select]

if selects&.length != 1
raise "Only one column should be selected and not #{selects&.length || 0} for: #{query.to_sql}"
end

query.where(selects[0] => attribute).any?
end

def hash_condition_match?(attribute, value)
if attribute.is_a?(Array) || (defined?(ActiveRecord) && attribute.is_a?(ActiveRecord::Relation))
array_like_matches_condition_hash?(attribute, value)
Expand Down
27 changes: 27 additions & 0 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,33 @@ class CustomPkTransaction < ActiveRecord::Base

expect(CustomPkTransaction.accessible_by(ability)).to match_array([transaction1])
end

it 'allows scope usage on can?' do
user1 = User.create!
user2 = User.create!

ability = Ability.new(user1)
ability.can :read, Article, user_id: User.where(id: user1.id).select(:id)

article1 = Article.create!(user: user1)
article2 = Article.create!(user: user2)

expect(ability).to be_able_to :read, article1
expect(ability).not_to be_able_to :read, article2
expect(Article.accessible_by(ability)).to eq [article1]
end

it 'raises an error if multiple columns are selected' do
user1 = User.create!

ability = Ability.new(user1)
ability.can :read, Article, user_id: User.where(id: user1.id).select(:id, :created_at)

article1 = Article.create!(user: user1)

expect { ability.can?(:read, article1) }
.to raise_error(/\AOnly one column should be selected and not 2 for: SELECT/)
end
end
end

Expand Down