Skip to content

Conversation

@kaspernj
Copy link
Contributor

@kaspernj kaspernj commented May 3, 2022

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
@kaspernj
Copy link
Contributor Author

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

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
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
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
Copy link

oboxodo commented Jan 23, 2023

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
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? :-)

Copy link

@garrettblehm garrettblehm left a comment

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

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