support loading resource :through method along with instance variable - closes #146

This commit is contained in:
Ryan Bates
2010-09-21 11:42:47 -07:00
parent 264e2d2b68
commit c11ffb6fd1
3 changed files with 21 additions and 3 deletions

View File

@@ -69,7 +69,7 @@ module CanCan
# Does not apply before filter to given actions.
#
# [:+through+]
# Load this resource through another one. This should match the name of the parent instance variable.
# Load this resource through another one. This should match the name of the parent instance variable or method.
#
# [:+shallow+]
# Pass +true+ to allow this resource to be loaded directly when parent is +nil+. Defaults to +false+.

View File

@@ -145,7 +145,15 @@ module CanCan
# The object to load this resource through.
def parent_resource
@options[:through] && [@options[:through]].flatten.map { |i| @controller.instance_variable_get("@#{i}") }.compact.first
@options[:through] && [@options[:through]].flatten.map { |i| fetch_parent(i) }.compact.first
end
def fetch_parent(name)
if @controller.instance_variable_defined? "@#{name}"
@controller.instance_variable_get("@#{name}")
elsif @controller.respond_to? name
@controller.send(name)
end
end
def name