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

Support using a nil relation as a condition #653

Merged

Conversation

ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Oct 13, 2020

I want to define an ability check that only applies if a has_many relation returns an empty array. The use case is I want to only allow deleting a record if it has no associated records for a given association.

In ActiveRecord you can do this with syntax like this:

Article.joins(:comments).where(comments: { id: nil })

This generates the SQL:

SELECT "articles".* FROM "articles" INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" WHERE "comments"."id" IS NULL

Which returns an empty array.

This PR makes cancan support the same style of query. So you can do:

@ability.can :destroy, Article, comments: { id: nil }

This will allow you to :destroy an Article that has no comments, but not an article that has comments.

…elation returns an empty array. The use case is I want to only allow deleting a record if it has no associated records for a given association.

In ActiveRecord you can do this with syntax like this:

> Article.joins(:comments).where(comments: { id: nil })

This generates the SQL:

> SELECT "articles".* FROM "articles" INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" WHERE "comments"."id" IS NULL

Which returns an empty array.

This PR makes cancan support the same style of query. So you can do:

> @ability.can :destroy, Article, comments: { id: nil }

This will allow you to `:destroy` an `Article` that has no comments, but not an article that has comments.
Copy link
Member

@coorasse coorasse left a comment

Choose a reason for hiding this comment

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

I have one or two questions and maybe a request for an additional test. good job!

spec/cancan/model_adapters/active_record_adapter_spec.rb Outdated Show resolved Hide resolved
expect(Article.accessible_by(@ability)).to eq([])

if CanCan::ModelAdapters::ActiveRecordAdapter.version_greater_or_equal?('5.0.0')
expect(@ability.model_adapter(Article, :read)).to generate_sql(%(
Copy link
Member

Choose a reason for hiding this comment

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

why does it even try? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, good question. I tried to avoid adding too much logic specific to this scenario, and mostly rely on how the Rails querying interface already works. In this case, we are generating a query that can never be true, but Rails hasn't necessarily understood that or optimised it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose I could try and refactor it to return Article.none in this case, but it feels like it could add a fair bit more maintenance load.

a2 = Article.create!
a2.comments = [Comment.create!]

@ability.can :read, Article, comments: { id: nil }
Copy link
Member

Choose a reason for hiding this comment

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

would can :read, Article, comments: nil work as well? I'd expect so from 3.2.0.

And what about can :read, Article, comments: []? Maybe I'd expect this to work as well as can :read, Article, comments: { id: [] }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not currently. The cancan conditions need to match conditions you'd pass into AR#where. Both of your examples wouldn't work as regular queries.

(byebug) Article.left_outer_joins(:comments).where(comments: nil)
#<ActiveRecord::Relation []>
(byebug) Article.left_outer_joins(:comments).where(comments: nil).to_sql
"SELECT \"articles\".* FROM \"articles\" LEFT OUTER JOIN \"comments\" ON \"comments\".\"article_id\" = \"articles\".\"id\" WHERE \"articles\".\"id\" IS NULL" # note how it looks for article.ids IS NULL, not comments.id
(byebug) Article.left_outer_joins(:comments).where(comments: [])
#<ActiveRecord::Relation []>
(byebug) Article.left_outer_joins(:comments).where(comments: []).to_sql
"SELECT \"articles\".* FROM \"articles\" LEFT OUTER JOIN \"comments\" ON \"comments\".\"article_id\" = \"articles\".\"id\" WHERE 1=0"

Whereas with my example:

(byebug) Article.left_outer_joins(:comments).where(comments: { id: nil })
#<ActiveRecord::Relation [#<Article id: 1, name: nil, created_at: "2020-12-14 00:50:15", updated_at: "2020-12-14 00:50:15", published: nil, secret: nil, priority: nil, category_id: nil, project_id: nil, user_id: nil>]>
(byebug) Article.left_outer_joins(:comments).where(comments: { id: nil }).to_sql
"SELECT \"articles\".* FROM \"articles\" LEFT OUTER JOIN \"comments\" ON \"comments\".\"article_id\" = \"articles\".\"id\" WHERE \"comments\".\"id\" IS NULL"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm open to trying to support more use cases, but I think it'd add a bit more complexity as we'd need to translate specific values (nils, [], etc) into the corresponding AR query statements.

@coorasse coorasse assigned ghiculescu and unassigned coorasse Dec 13, 2020
@ghiculescu ghiculescu requested a review from coorasse December 21, 2020 17:25
@coorasse coorasse removed their request for review February 3, 2021 15:52
@ghiculescu
Copy link
Contributor Author

@coorasse anything more you need from me to get this merged?

@coorasse
Copy link
Member

coorasse commented Jul 6, 2022

I believe this is ready to be merged. Could you squash and rebase, please?

@ghiculescu
Copy link
Contributor Author

I'm just gonna run our CI suite against this now that a few changes upstream have been merged in.

@coorasse
Copy link
Member

coorasse commented Jul 6, 2022

Sure! Just let me know, but I believe is good to go 👍

@ghiculescu
Copy link
Contributor Author

@coorasse green build. Go for it.

@coorasse coorasse merged commit 8efb522 into CanCanCommunity:develop Jul 6, 2022
@ghiculescu ghiculescu deleted the nil-relation-as-condition branch July 6, 2022 15:12
@ghiculescu ghiculescu restored the nil-relation-as-condition branch July 6, 2022 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants