multiple nested resources.

12 views
Skip to first unread message

dgm

unread,
Nov 19, 2009, 5:25:08 PM11/19/09
to declarative_authorization
I just discovered that filter_resource_access provides some default
objects that I had been creating by hand, but I'm not sure it is doing
it all.

can the :nested option take an array of resources?

I have a resource that can be nested in several different ways... I'm
a fan of polymorphic_path :)

Steffen Bartsch

unread,
Nov 20, 2009, 3:51:55 AM11/20/09
to declarative_...@googlegroups.com
Am Donnerstag, 19. November 2009 schrieb dgm:
> can the :nested option take an array of resources?
>
> I have a resource that can be nested in several different ways... I'm
> a fan of polymorphic_path :)

No, filter_resource_access is not meant to handle polymorphic controllers.

For those cases, you need to setup your own before_filters to load the parent
object as appropriate. Then, you can implement your own
"new_#{controller_name.singularize}_for_collection" and
"new_#{controller_name.singularize}_from_params" methods.

Then, you can use the filter_resource_access mechanism with your custom
behavior concerning the parent resource.

If you have that one running, please share it on this list. Even better,
improve the filter_resource_access method to handle multiple nested_ins.

Steffen

dgm

unread,
Nov 20, 2009, 2:55:46 PM11/20/09
to declarative_authorization


On Nov 20, 2:51 am, Steffen Bartsch <sbart...@tzi.de> wrote:

> If you have that one running, please share it on this list.  Even better,
> improve the filter_resource_access method to handle multiple nested_ins.

What I would envison would be :nested_in
[:person_id, :household_id, :organization_id]

would create something like this:

@parent = case
when params[:person_id] then Person.find_by_id(params
[:person_id])
when params[:household_id] then Household.find_by_id(params
[:household_id])
when params[:organization_id] then Organization.find_by_id
([:organization_id])
end

I think it looks like changing in_controller.rb around ` unless options
[:nested_in].blank?`
to loop through the array and determine which one is active (since
only one should be) and then operate on that.
Furthermore , load_parent_controller_object should set @parent in
addition to whichever one of [@person,@organization, @household] it
currently sets.

Is there anything else that I'm missing?

Steffen Bartsch

unread,
Nov 20, 2009, 4:37:33 PM11/20/09
to declarative_...@googlegroups.com
Am Freitag, 20. November 2009 schrieb dgm:
> What I would envison would be :nested_in
> [:person_id, :household_id, :organization_id]
>
> would create something like this:
>
> @parent = case
> when params[:person_id] then Person.find_by_id(params
> [:person_id])
> when params[:household_id] then Household.find_by_id(params
> [:household_id])
> when params[:organization_id] then Organization.find_by_id
> ([:organization_id])
> end
>
> I think it looks like changing in_controller.rb around ` unless options
> [:nested_in].blank?`
> to loop through the array and determine which one is active (since
> only one should be) and then operate on that.

I think that the loop rather needs to be inside the
load_parent_controller_object because the filter_resource_access will only be
called on initialization. Also, up to now, the instance variable is named
after the respective parent resource name.

> Furthermore , load_parent_controller_object should set @parent in
> addition to whichever one of [@person,@organization, @household] it
> currently sets.
>
> Is there anything else that I'm missing?

new_controller_object_from_params and _for_collection needs to be aware of the
different options as those get the parent object from its instance variable.

Most likely that should be all.

Steffen

dgm

unread,
Nov 20, 2009, 4:58:52 PM11/20/09
to declarative_authorization


On Nov 20, 3:37 pm, Steffen Bartsch <sbart...@tzi.de> wrote:

> Most likely that should be all.


I'm mostly there, but it didn't create the record in the :through
table...

Also, it appears that the current implementation has a problem with
viewing the resource un-nested, is that right?

dgm

unread,
Nov 20, 2009, 5:54:13 PM11/20/09
to declarative_authorization


On Nov 20, 3:37 pm, Steffen Bartsch <sbart...@tzi.de> wrote:

> new_controller_object_from_params and _for_collection needs to be aware of the
> different options as those get the parent object from its instance variable.


I have a basic patch done, I sent a pull request.

I still have one problem

Permission denied: index not allowed for #<User id: 689986156, login:
"dgm", email: "dgm", person_id: 1180170431, crypted_password:
"00742970dc9e6319f8019fd54864d3ea740f04b1", salt:
"7e3041ebc2fc05a40c60028e2c4901a81035d3cd", created_at: "2009-11-19
19:06:11", updated_at: "2009-11-20 19:06:11", remember_token: nil,
remember_token_expires_at: nil, activation_code:
"8f24789ae988411ccf33ab0c30fe9106fab32e9a", activated_at: nil> on
#<PhoneNumber id: nil, phone_number: nil, phone_type: nil, private:
nil, note: nil, updated_by: nil, created_at: nil, updated_at: nil>.


role :staff do
has_permission_on [:people], :to =>
[:index, :show, :edit, :update, :create, :new] do
if_attribute :user => is { user }
end
has_permission_on [:phone_numbers], :to =>
[:index, :show, :edit, :update, :create, :new] do
if_attribute :people => {:user => is { user }}
end
end


I'm probably missing something obvious.

Steffen Bartsch

unread,
Nov 21, 2009, 5:59:22 AM11/21/09
to declarative_...@googlegroups.com
Am Freitag, 20. November 2009 schrieb dgm:
> Also, it appears that the current implementation has a problem with
> viewing the resource un-nested, is that right?

Not to my knowledge. Do you have a failing test for that issue?

Steffen

Steffen Bartsch

unread,
Nov 21, 2009, 6:01:20 AM11/21/09
to declarative_...@googlegroups.com
Am Freitag, 20. November 2009 schrieb dgm:
> I have a basic patch done, I sent a pull request.

Looks nice. Can you provide a matching test case?

Steffen

dgm

unread,
Nov 21, 2009, 10:18:10 AM11/21/09
to declarative_authorization
I'm still not very fluent with tests... and I see this broke a bunch
of them due to mocks.

dgm

unread,
Nov 21, 2009, 1:30:11 PM11/21/09
to declarative_authorization
Shouldn't new_controller_object_for_collection create a pluralized
instance variable?

like @companies?
Reply all
Reply to author
Forward
0 new messages