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

Add option for accessible_by querying strategy #655

Merged
merged 18 commits into from Dec 12, 2020

Conversation

ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Oct 13, 2020

Inspired by this comment, let users choose the querying strategy cancan will use when calling accessible_by.

You can choose :subquery (the default since #619), or :left_joins (how it worked before #619).

Most tests run exactly the same on both strategies. Where they differ I have split into two separate test contexts.

I have deliberately not added this to the README, because out of the box I don't think you should change this. I would like to add a mention to https://github.com/CanCanCommunity/cancancan/wiki/Fetching-Records if this is merged (I don't have edit access to that page).

@ghiculescu ghiculescu changed the title Add option for accessible_by querying strategy Add option for accessible_by querying strategy Oct 13, 2020
@ghiculescu ghiculescu marked this pull request as ready for review October 14, 2020 22:07
@ghiculescu
Copy link
Contributor Author

@coorasse let me know your thoughts

@madmax
Copy link

madmax commented Oct 23, 2020

@ghiculescu I think it will be god if we can decide by ability when using subquery and when left_joins not only globaly. Some queries will be better with joins some with subquery.

@ghiculescu
Copy link
Contributor Author

I looked into doing it that way @madmax but it added a fair bit more complexity. I'm keen to start with this, because it allows everyone to match pre-#619 behavior. We can always add more options later.

@coorasse
Copy link
Member

coorasse commented Nov 9, 2020

I love the effort you put in this PR! There is a lot of good work! I have to agree with @madmax . I strongly believe that this option could/should be also on a "per call" level. Because most of the times you might want to change it only in one specific place. Nevertheless: I am also in favour of a global configuration, so I am keen in merging after a review.

@ghiculescu
Copy link
Contributor Author

For the record, we'll be using this configuration option globally in our app.

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 really like your work! There's only one question that I have

lib/cancan/model_adapters/active_record_4_adapter.rb Outdated Show resolved Hide resolved
@coorasse
Copy link
Member

coorasse commented Nov 9, 2020

I love the effort you put in this PR! There is a lot of good work! I have to agree with @madmax . I strongly believe that this option could/should be also on a "per call" level. Because most of the times you might want to change it only in one specific place. Nevertheless: I am also in favour of a global configuration, so I am keen in merging after a review.

@ghiculescu
Copy link
Contributor Author

ghiculescu commented Dec 4, 2020

I thought of a way of making this work per-query without cluttering up the cancan API a lot.

CanCan.accessible_by_strategy = :subquery
User.accessible_by(current_ability) # uses :subquery

CanCan.with_accessible_by_strategy(:left_joins) do
  # all CanCan queries generated inside the block use :left_joins
  User.accessible_by(current_ability)
end

User.accessible_by(current_ability) # uses :subquery

Thoughts? Should be relatively easy to implement.

@coorasse
Copy link
Member

coorasse commented Dec 9, 2020

What about opening a discussion regarding this point? I just enabled the tab in the project. We can start from your proposal @ghiculescu . So we can proceed with this PR and merge it in the meantime and proceed elsewhere to decide how we want to implement the per-query API.

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.

Maybe an update to the docs will be necessary, but it can be done in a separate PR. Thanks!


@accessible_by_strategy = value
end
end
Copy link
Member

Choose a reason for hiding this comment

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

I like this. very descriptive

@ghiculescu
Copy link
Contributor Author

ghiculescu commented Dec 9, 2020

@coorasse happy to add docs, any suggestions on where? I was thinking https://github.com/CanCanCommunity/cancancan/blob/develop/docs/Defining-Abilities:-Best-Practices.md but I'm not sure any of the current docs feels right.

ps. I'm not sure what https://github.com/CanCanCommunity/cancancan/blob/develop/docs/mvc--deficiencies.md is but feel like it shouldn't be there.

pps. I started a discussion here #668

@coorasse
Copy link
Member

Thanks for finding the irrelevant doc page, I removed it. The whole docs would need a bit of re-organisation. I'd definitely need some help there.
Probably here? https://github.com/CanCanCommunity/cancancan/blob/develop/docs/Fetching-Records.md

@coorasse coorasse merged commit 34885fc into CanCanCommunity:develop Dec 12, 2020
@ghiculescu ghiculescu deleted the querying-strategy-2 branch December 16, 2020 17:49
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.

None yet

3 participants