Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make "authorize!" return the object it authorized #314

Closed
BMorearty opened this Issue · 6 comments

3 participants

@BMorearty

Responding to your call for comments on CanCan 2.0, what I miss from CanCan is the ability to compact my code into one line like this:
@project = authorize!(:edit, Project.find(params[:id]))
instead of this:
@project = Project.find(params[:id])
authorize! :edit, @project

So basically "authorize!" would return its second parameter if it succeeds. Easy peasy lemon squeezy.

This would be a nice convenience for those who want it and would not impact those who prefer to write two lines.

Thanks!

@ryanb
Owner

This is an easy enough change and doesn't hurt anything, although I lean on the side of preferring two lines. Still I can add this in, thanks for the suggestion.

@hauleth

If you prefer one line you can write:

authorize! :edit, @project = Project.find(params[:id])

It should work :)

@BMorearty

Good point Hauleth, but it doesn't solve my use case (which I neglected to mention, so I will now). Sometimes there are good reasons to put a query in a helper method. In that case it's natural to memoize it to avoid redoing the work if the helper is called several times:
def project
@project ||= authorize!(:edit, Project.find(params[:id]))
end
In this case it makes sense to avoid calling authorize! over and over again, which this would do:
def project
authorize! :edit, @project ||= Project.find(params[:id])
end

@hauleth

Then write (maybe more ugly but still easy):

authorize! :edit, @project = Project.find(params[:id]) unless @project
@BMorearty

But that wouldn't return @project.

And sure, now you could parenthesize the whole thing and add "and @project" to the end, but it gets ridiculous. As Ryan said, the change is easy enough and doesn't hurt anything.

@ryanb
Owner

return subject passed to authorize! - closed by 1ac8099

@ryanb ryanb closed this
@jhuckabee jhuckabee referenced this issue from a commit in jhuckabee/cancan
@ryanb return subject passed to authorize! - closes #314 1ac8099
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.