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

Fix accessible_by fires query for rules using association as condition. #569

Conversation

albb0920
Copy link

see #552

In turns out this could be fixed by swapping the left hand side and right hand side of ==.

@albb0920
Copy link
Author

Rubocop doesn't like it, do you want (@conditions.is_a?(Hash) && @conditions == {}) instead?

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.

As discussed also in the issue, I need a real use case where the example is appropriate.

In this test, the ability is not defined correctly and there would be a better way of defining it.

I am not willing to fix an issue caused by a misuse of the library: I'd rather prefer the user to feel the pain for not using it correctly 😄.

Please, try to build an effective example where this happens, with the usage of a scope, for example, as you showed me in the issue. Thank you @albb0920 , I appreciate the effort you are putting into this issue! 🙇

@@ -115,6 +115,17 @@ class User < ActiveRecord::Base
expect(Article.accessible_by(@ability)).to eq([article])
end

it 'does not fires query with accessible_by() for abilities defined with association' do
user = User.create!
@ability.can(:read, Article, user.articles) { true }
Copy link
Member

Choose a reason for hiding this comment

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

this is not how you write rules in cancancan. you should replace it with can :read, Article, user_id: user.id, as discussed also #552.

@albb0920 albb0920 force-pushed the avoid_load_target_on_ar_collection_proxy branch from 8107ded to 75f85f6 Compare February 25, 2019 11:10
@albb0920
Copy link
Author

I've updated the test case, does this look more reasonable?

if condition is an ActiveRecord::Associations::CollectionProxy,
calling it's `==` method would load all records.
@albb0920 albb0920 force-pushed the avoid_load_target_on_ar_collection_proxy branch from 75f85f6 to 1916566 Compare February 25, 2019 11:15
@coorasse
Copy link
Member

coorasse commented Mar 6, 2020

closed in #620

@coorasse coorasse closed this Mar 6, 2020
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.

2 participants