Skip to content

Allow can with query on single resource - #785

Open
kaspernj wants to merge 5 commits into
CanCanCommunity:developfrom
kaspernj:allow-can-on-single-resource
Open

Allow can with query on single resource#785
kaspernj wants to merge 5 commits into
CanCanCommunity:developfrom
kaspernj:allow-can-on-single-resource

Conversation

@kaspernj

@kaspernj kaspernj commented May 3, 2022

Copy link
Copy Markdown
Contributor

Passing a query to a rule is already supported by accessible_by but not when checking on a single resource. This PR fixes that and adds specs for it.

Example usage:

can :read, ProjectPart, state: "won", offer_won: {account_id: current_accounts.select(:id)}

@kaspernj
kaspernj force-pushed the allow-can-on-single-resource branch from ebf633e to 02f63ac Compare May 3, 2022 13:25
Comment thread lib/cancan/conditions_matcher.rb
@kaspernj

Copy link
Copy Markdown
Contributor Author

Did anyone have time to take a look at this? :-)

Comment thread lib/cancan/conditions_matcher.rb
Comment thread lib/cancan/conditions_matcher.rb Outdated
selects = query.values[:select]

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a test for this error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coorasse I have added a test and fixed the issue with the wrong variable name :-)

@oboxodo

oboxodo commented Jan 23, 2023

Copy link
Copy Markdown

I just posted an issue related to this (but a bit more general maybe?): #812

And a PR with a proposed change: #813

Open for feedback.

@kaspernj

Copy link
Copy Markdown
Contributor Author

@coorasse I feel like fixing the offence would require me to do an unrelated big refactor to CanCan::ConditionsMatcher.

How would you prefer I continue? :-)

@garrettblehm garrettblehm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to have this pushed through

Comment on lines +91 to +94
if defined?(ActiveRecord) &&
(value.is_a?(ActiveRecord::AssociationRelation) || value.is_a?(ActiveRecord::Relation))
return condition_match_query?(attribute, value)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be moved within the case statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants