Added ability to specify the name of the child element as referenced within the parent #171

Closed
wants to merge 11 commits into
from

Projects

None yet

2 participants

@hunterae

Previously, if you did something like:

class AssignmentsController < ApplicationController
  load_and_authorize_resource :through => :current_user
  ...
end

it would require that either the method "current_user" or the instance variable @current_user had an array called "assignments". However, this isn't always the case. Suppose my User model looks something like this:

class User < ActiveRecord::Base
  has_many :editor_of_assignments, :foreign_key => "editor_id", :class_name => "Assignment"
  ...
end

There was no way to specify to use the has_many "editor_of_assignments" relationship. I have added an option called "name_in_parent" to specify this relationship name. It may be used as follows:

load_and_authorize_resource :name_in_parent => :editor_of_assignments, :through => :current_user

To do this, I have modified line 135 in controller_resource.rb to read:
@options[:singleton] ? parent_resource : parent_resource.send(@options[:name_in_parent] || name.to_s.pluralize)

I have chosen not to modify the entire "name" function on line 163 since that function is used in many different places.

Hope this helps make Cancan even better.

@ryanb
Owner
ryanb commented Oct 13, 2010

Thanks, this is something I've been wanting to add but never got around to it, I'll pull this in soon.

I may change the name of the option though. Maybe :through_association? Anyone have a suggestion?

@hunterae

Yeah, that sounds much better than :name_in_parent.

hunterae added some commits Oct 18, 2010
@hunterae hunterae Modifying to see if I can get the collection to load even when a bloc…
…k is given
3cee524
@hunterae hunterae Modifying to see if I can get the collection to load even when a bloc…
…k is given
54ed52d
@hunterae hunterae Modifying to see if I can get the collection to load even when a bloc…
…k is given
a2f20c4
@hunterae hunterae Modifying to see if I can get the collection to load even when a bloc…
…k is given
43b219b
@hunterae hunterae Undoing changes that attempted to see if I can get the collection to …
…load even when a block is given
c991c5e
@hunterae hunterae Added feature for loading a resource through a parent element, such t…
…hat the parent and child are in a has and belongs to many relationship. The problem before was that the child wasn't having its parent array instantiated properly (For some reason, this is a rails feature; example: parent.children.new will add a new Child element to the parent's children array, however, the new child's parents array will be empty)
ec4b3dd
@hunterae hunterae Added feature for loading a resource through a parent element, such t…
…hat the parent and child are in a has and belongs to many relationship. The problem before was that the child wasn't having its parent array instantiated properly (For some reason, this is a rails feature; example: parent.children.new will add a new Child element to the parent's children array, however, the new child's parents array will be empty)
0ae7261
@ryanb
Owner
ryanb commented Nov 12, 2010

adding :through_association option to load_resource (thanks hunterae) - closed by 92995d7

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