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

Adding support for "can?" check multiple subjects #3

Conversation

cefigueiredo
Copy link
Contributor

The behaviour when an array of subject is given, is check if there is a permission at least on one of the given subjects.

The behaviour when an array of subject is given, is check if there is a permission at least on one of the given subjects.
@ability.can :update, [String, Range]
expect(@ability.can?(:update, ["foo", 1..3])).to be_true
expect(@ability.can?(:update, [123, "foo"])).to be_true
expect(@ability.can?(:update, 123)).to be_false
Copy link
Member

Choose a reason for hiding this comment

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

Could this be updated to check be_false against an array, for example, if you sent in an array of [123, 1.0] it should be false?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure... but I will not change that line... I will add another assuring that with an array where all subjects have no permission it should be false...

@bryanrite
Copy link
Member

Oh, and add a line to the changelog as well please!

@bryanrite
Copy link
Member

On further inspection, can we also test this will handle nested can? declarations?

You can declare a can? like:

can? :create, @category => Project

I want to ensure we could also send that into the array:

can? :create, [{@category => Project}, Category]

@@ -1,6 +1,11 @@
Master


1.7.1 (February 22th, 2014)
Copy link
Member

Choose a reason for hiding this comment

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

Please drop this line, just add your changelog entry under master. When a new release is cut, we'll assign it to a version.

Thanks!

@codyolsen
Copy link

+1 on this feature. Among other things, it's going to make testing much cleaner. @cefigueiredo are you planning on completing this?

@cefigueiredo
Copy link
Contributor Author

Sure... I'm working on some problems. Maybe I change a bit how it will deal with multiple subjects... but the functionality will be implemented!

During the test to permit pass an Object as a hash I needed to change how call can? to mantain the functionality to pass any object inclusive an instance of Array as a single subject.
To assure that funciontality, now we need to pass a hash containing a key :any and the array of subjects as his value.
The array deals with the same objects that an usual can? does... and obviously, that hash is only necessary when checking multiple subjects.
@cefigueiredo
Copy link
Contributor Author

To ensure that can? can receive the same objects and in the same way a can? with single subject does; I changed how to pass the multple subjects.
To pass an array of subjects, now you need to pass them in a hash as the value of the key :any

can? :read, { :any => [{@category => Project}, Category] }

@bryanrite
Copy link
Member

I like that! We can later add :all => [...] to match all rules.

@cefigueiredo
Copy link
Contributor Author

Exactly what I thought! I would do that on that commit, but I saw that it was more complex than :any to test and implement.

@codyolsen
Copy link

@cefigueiredo This is great! Thanks for your contribution! I can't wait to throw this into my tests! @bryanrite is the plan to merge this or should I use @cefigueiredo's fork for the time being?

@bryanrite
Copy link
Member

Sorry, didn't realize this was ready.

@cefigueiredo, @codyolsen I'm going to suggest a couple things to this PR to clean-up the code, afterwards I'll merge it in.

end
matches.compact.first
else
relevant_rules_for_match(action, subject).detect do |rule|
Copy link
Member

Choose a reason for hiding this comment

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

Can we extract the relevant_rules_for_match block into a single method so its not duplicated.

@codyolsen
Copy link

@bryanrite 10-4. Sounds like a plan.

The method can? was changed and does not pass an array of subject anymore to matches_subject? since last commit.
So, I'm removing the method matches_subject_any? that only existed to deal with that array of subjects.
The method can? became a bit messy and repetitive dealing chosing paths almost identical to single and multiple subjects, when only the difference was iterate on the array of subjects.
So, I created the method extract_subject that independently if given a single or multiple subjects, it will returns an array of subjects. So, for can? does  not matter anymore if subject is single or multiple, it will deal on the same way.
@bryanrite
Copy link
Member

Sorry for the delay on this, just been busy, I'll get this merged in, and try to add any functionality as well.

@ekampp
Copy link

ekampp commented Apr 10, 2014

This has been closed, not merged. State?

@bryanrite
Copy link
Member

@ekampp The referenced PR was closed. This PR is still open. It is valid, I have just not had a ton of time to integrate it lately, as it will require a new minor version. I'll try and get at it in the next day or two.

@ekampp
Copy link

ekampp commented Apr 11, 2014

Ohh.. Never mind, then. I'm tiered 😫

@cefigueiredo
Copy link
Contributor Author

Are there something I could do to make it easier?

On Fri, Apr 11, 2014 at 2:12 AM, Emil Kampp notifications@gh.neting.ccwrote:

Ohh.. Never mind, then. I'm tiered [image: 😫]


Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-40171350
.

@bryanrite bryanrite closed this in 8d4279d Apr 30, 2014
@bryanrite
Copy link
Member

Thanks! Sorry it took so long, but I had to spend some time refactoring all the tests before I wanted to add more features. Travis says failing but its due to their latest update and not code, so you can ignore.

coorasse pushed a commit that referenced this pull request Jul 6, 2022
author Remo Fritzsche <remo.fritzsche@sitrox.com> 1612868567 +0100
committer Alessandro Rodi <coorasse@gmail.com> 1657114188 +0200

# This is a combination of 4 commits.
# This is the 1st commit message:

Preserve nil values as extra arguments

# This is the commit message #2:

Fix link (#744)

# This is the commit message #3:

Documentation fixes and improvements

- fix typo, punctuations, and grammar
- fix broken and misformatted links
- fix code sample

# This is the commit message #4:

Format documentation/guides and fix linting issues

The motivation of the changes is to make these documents neater when
reading offline using a text or code editor.

Reading the documentation offline is faster, and the source code is
readily available for further learning.

The changes do not affect the meaning of each documentation or
instruction.

The following changes were made:
- remove extra and trailing spaces
- add new lines to separate the title, explanation, code samples, etc
- fix headings, links
- specify code block language (bash, ruby)

Updated Devise.md as exception is changed

CanCan::Unathorized exception not exists anymore, CanCan::AccessDenied is used. Also added responses for different content types as a recommendation.

Add support for non-hash conditions

Co-authored-by: Juleffel <juleffel@protonmail.com>

Improve ability checks with Hashes

Add Ruby 3.0 and Ruby 3.1 to CI

Drop ActiveRecord4 from CI (#778)
coorasse pushed a commit that referenced this pull request Jul 6, 2022
author Remo Fritzsche <remo.fritzsche@sitrox.com> 1612868567 +0100
committer Alessandro Rodi <coorasse@gmail.com> 1657114188 +0200

# This is a combination of 4 commits.
# This is the 1st commit message:

Preserve nil values as extra arguments

# This is the commit message #2:

Fix link (#744)

# This is the commit message #3:

Documentation fixes and improvements

- fix typo, punctuations, and grammar
- fix broken and misformatted links
- fix code sample

# This is the commit message #4:

Format documentation/guides and fix linting issues

The motivation of the changes is to make these documents neater when
reading offline using a text or code editor.

Reading the documentation offline is faster, and the source code is
readily available for further learning.

The changes do not affect the meaning of each documentation or
instruction.

The following changes were made:
- remove extra and trailing spaces
- add new lines to separate the title, explanation, code samples, etc
- fix headings, links
- specify code block language (bash, ruby)

Updated Devise.md as exception is changed

CanCan::Unathorized exception not exists anymore, CanCan::AccessDenied is used. Also added responses for different content types as a recommendation.

Add support for non-hash conditions

Co-authored-by: Juleffel <juleffel@protonmail.com>

Improve ability checks with Hashes

Add Ruby 3.0 and Ruby 3.1 to CI

Drop ActiveRecord4 from CI (#778)
coorasse pushed a commit that referenced this pull request Jul 6, 2022
author Remo Fritzsche <remo.fritzsche@sitrox.com> 1612868567 +0100
committer Alessandro Rodi <coorasse@gmail.com> 1657114188 +0200

# This is a combination of 4 commits.
# This is the 1st commit message:

Preserve nil values as extra arguments

# This is the commit message #2:

Fix link (#744)

# This is the commit message #3:

Documentation fixes and improvements

- fix typo, punctuations, and grammar
- fix broken and misformatted links
- fix code sample

# This is the commit message #4:

Format documentation/guides and fix linting issues

The motivation of the changes is to make these documents neater when
reading offline using a text or code editor.

Reading the documentation offline is faster, and the source code is
readily available for further learning.

The changes do not affect the meaning of each documentation or
instruction.

The following changes were made:
- remove extra and trailing spaces
- add new lines to separate the title, explanation, code samples, etc
- fix headings, links
- specify code block language (bash, ruby)

Updated Devise.md as exception is changed

CanCan::Unathorized exception not exists anymore, CanCan::AccessDenied is used. Also added responses for different content types as a recommendation.

Add support for non-hash conditions

Co-authored-by: Juleffel <juleffel@protonmail.com>

Improve ability checks with Hashes

Add Ruby 3.0 and Ruby 3.1 to CI

Drop ActiveRecord4 from CI (#778)
coorasse pushed a commit that referenced this pull request Jul 6, 2022
author Remo Fritzsche <remo.fritzsche@sitrox.com> 1612868567 +0100
committer Alessandro Rodi <coorasse@gmail.com> 1657114188 +0200

# This is a combination of 4 commits.
# This is the 1st commit message:

Preserve nil values as extra arguments

# This is the commit message #2:

Fix link (#744)

# This is the commit message #3:

Documentation fixes and improvements

- fix typo, punctuations, and grammar
- fix broken and misformatted links
- fix code sample

# This is the commit message #4:

Format documentation/guides and fix linting issues

The motivation of the changes is to make these documents neater when
reading offline using a text or code editor.

Reading the documentation offline is faster, and the source code is
readily available for further learning.

The changes do not affect the meaning of each documentation or
instruction.

The following changes were made:
- remove extra and trailing spaces
- add new lines to separate the title, explanation, code samples, etc
- fix headings, links
- specify code block language (bash, ruby)

Updated Devise.md as exception is changed

CanCan::Unathorized exception not exists anymore, CanCan::AccessDenied is used. Also added responses for different content types as a recommendation.

Add support for non-hash conditions

Co-authored-by: Juleffel <juleffel@protonmail.com>

Improve ability checks with Hashes

Add Ruby 3.0 and Ruby 3.1 to CI

Drop ActiveRecord4 from CI (#778)
coorasse pushed a commit that referenced this pull request Jul 6, 2022
author Remo Fritzsche <remo.fritzsche@sitrox.com> 1612868567 +0100
committer Alessandro Rodi <coorasse@gmail.com> 1657114188 +0200

# This is a combination of 4 commits.
# This is the 1st commit message:

Preserve nil values as extra arguments

# This is the commit message #2:

Fix link (#744)

# This is the commit message #3:

Documentation fixes and improvements

- fix typo, punctuations, and grammar
- fix broken and misformatted links
- fix code sample

# This is the commit message #4:

Format documentation/guides and fix linting issues

The motivation of the changes is to make these documents neater when
reading offline using a text or code editor.

Reading the documentation offline is faster, and the source code is
readily available for further learning.

The changes do not affect the meaning of each documentation or
instruction.

The following changes were made:
- remove extra and trailing spaces
- add new lines to separate the title, explanation, code samples, etc
- fix headings, links
- specify code block language (bash, ruby)

Updated Devise.md as exception is changed

CanCan::Unathorized exception not exists anymore, CanCan::AccessDenied is used. Also added responses for different content types as a recommendation.

Add support for non-hash conditions

Co-authored-by: Juleffel <juleffel@protonmail.com>

Improve ability checks with Hashes

Add Ruby 3.0 and Ruby 3.1 to CI

Drop ActiveRecord4 from CI (#778)
coorasse added a commit that referenced this pull request May 23, 2024
Do not run rails >= 7 on ruby < 3
coorasse added a commit that referenced this pull request May 23, 2024
Do not run rails >= 7 on ruby < 3
coorasse added a commit that referenced this pull request May 23, 2024
Do not run rails >= 7 on ruby < 3
coorasse added a commit that referenced this pull request May 23, 2024
* Update gitignore

* This is the commit message #3:

Do not run rails >= 7 on ruby < 3

* Remove jruby and truffleruby

* Fix rubocop

* Use rspec instead of rake
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