Use the association name for nested resource? #44

Closed
louismrose opened this Issue Mar 5, 2010 · 5 comments

Comments

Projects
None yet
3 participants
@louismrose

Suppose I have the follow model:

class Post < ActiveRecord::Base
  belongs_to :owner, :class_name => 'User'
end

And the following controller:

class PostsController < < ApplicationController
  load_and_authorize_resource :nested => :user

  .....
end

While this works, it'd be neat if CanCan could initialise an @owner variable (rather than @user).

I imagine that this could be achieved by changing CanCan such that load_and_authorize_resource :nested => :owner achieves this, or perhaps adding an extra option to the hash, such as :renamings => { :user => :owner }

Is this reasonable / feasible? (I can try to write the patch).

@ryanb

This comment has been minimized.

Show comment Hide comment
@ryanb

ryanb Apr 16, 2010

Owner

The problem is I'm not certain how to do this in a clean way without being very dependent upon Active Record. I would like CanCan's controller behavior to be fairly ORM agnostic and just rely on a few key methods (such as find). I'll think about this more and see if there's a clean solution. Feel free to offer some specific suggestions on implementation.

Owner

ryanb commented Apr 16, 2010

The problem is I'm not certain how to do this in a clean way without being very dependent upon Active Record. I would like CanCan's controller behavior to be fairly ORM agnostic and just rely on a few key methods (such as find). I'll think about this more and see if there's a clean solution. Feel free to offer some specific suggestions on implementation.

@louismrose

This comment has been minimized.

Show comment Hide comment
@louismrose

louismrose Apr 16, 2010

Ok - that makes sense. I'll have a look at some specifics, and get back to you. Cheers.

Ok - that makes sense. I'll have a look at some specifics, and get back to you. Cheers.

@mryan43

This comment has been minimized.

Show comment Hide comment
@mryan43

mryan43 May 3, 2010

Letting the user override the controller's instance variables names would be pretty ORM agnostic.

Maybe something like this :

load_and_authorize_resource :nested => {:owner => :owner_instance_variable_name}, :as => :post_instance_variable_name

i've checked the code a bit, my first feeling is you would have to add a "model_instance_name" parameter to the ControllerResource's initializer (and maybe rename the "name" parameter to "model_name")

also the parent_resource method in ResourceAuthorization would have to be adapted.

Cheers !

mryan43 commented May 3, 2010

Letting the user override the controller's instance variables names would be pretty ORM agnostic.

Maybe something like this :

load_and_authorize_resource :nested => {:owner => :owner_instance_variable_name}, :as => :post_instance_variable_name

i've checked the code a bit, my first feeling is you would have to add a "model_instance_name" parameter to the ControllerResource's initializer (and maybe rename the "name" parameter to "model_name")

also the parent_resource method in ResourceAuthorization would have to be adapted.

Cheers !

@ryanb

This comment has been minimized.

Show comment Hide comment
@ryanb

ryanb Aug 6, 2010

Owner

While it's not a perfect fix, the new :instance_name option will have to do as a solution for this. Once 1.3 is released you will use it like this.

load_and_authorize_resource :user, :instance_name => :owner
load_and_authorize_resource :post, :through => :owner
Owner

ryanb commented Aug 6, 2010

While it's not a perfect fix, the new :instance_name option will have to do as a solution for this. Once 1.3 is released you will use it like this.

load_and_authorize_resource :user, :instance_name => :owner
load_and_authorize_resource :post, :through => :owner
@louismrose

This comment has been minimized.

Show comment Hide comment
@louismrose

louismrose Aug 7, 2010

That looks great, thanks Ryan!

That looks great, thanks Ryan!

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment