Permalink
Browse files

fix redirect-after-create for shallow nested resources

  • Loading branch information...
1 parent 7e915a6 commit c7b26d6956e27e5f3088bb4c7d8824e7f793bae4 Kevin Bullock committed Feb 14, 2011
View
@@ -27,7 +27,7 @@ def initialize(kontroller)
@responses = {}
@publish = {}
@parents = []
- @shallow_route = false
+ @shallow_parent = nil
@custom_member_actions = []
@custom_collection_actions = []
end
@@ -59,7 +59,7 @@ def apply
kontroller.made_resourceful = true
kontroller.parents = @parents
- kontroller.shallow_route = @shallow_route
+ kontroller.shallow_parent = @shallow_parent
kontroller.model_namespace = @model_namespace
kontroller.before_filter :load_object, :only => (@ok_actions & SINGULAR_PRELOADED_ACTIONS) + @custom_member_actions
kontroller.before_filter :load_objects, :only => (@ok_actions & PLURAL_ACTIONS) + @custom_collection_actions
@@ -361,7 +361,11 @@ def publish(*formats)
def belongs_to(*parents)
options = parents.extract_options!
@parents = parents.map(&:to_s)
- @shallow_route = !!options[:shallow_route]
+ if options[:shallow]
+ options[:shallow] = options[:shallow].to_s
+ raise ArgumentError, ":shallow needs the name of a parent resource" unless @parents.include? options[:shallow]
+ @shallow_parent = options[:shallow]
+ end
end
# Specifies a namespace for the resource model. It can be given as a
@@ -212,10 +212,12 @@ def parent?
!!parent_name
end
- # Returns true if the belongs_to relationship on this controller was
- # declared to use shallow routing.
- def shallow_route?
- !!self.class.shallow_route
+ # Returns true if no parent id parameter can be found _and_ a belongs_to
+ # relationship on this controller was declared with a parent for shallow
+ # routing.
+ def shallow?
+ self.class.shallow_parent &&
+ (parent_name.nil? || parent_name == self.class.shallow_parent)
end
# Returns whether the parent (if it exists) is polymorphic
@@ -124,7 +124,7 @@ def new_object_route(type)
end
def instance_route(name, object, type, action = nil)
- send("#{action ? action + '_' : ''}#{url_helper_prefix}#{collection_url_prefix unless shallow_route?}#{name}_#{type}", *(parent? && !shallow_route? ? [parent_object, object] : [object]))
+ send("#{action ? action + '_' : ''}#{url_helper_prefix}#{collection_url_prefix unless shallow?}#{name}_#{type}", *(parent? && !shallow? ? [parent_object, object] : [object]))
end
def collection_route(name, type, action = nil)
View
@@ -12,7 +12,7 @@ def self.extended(base)
base.class_attribute :resourceful_callbacks
base.class_attribute :resourceful_responses
base.class_attribute :parents
- base.class_attribute :shallow_route
+ base.class_attribute :shallow_parent
base.class_attribute :model_namespace
base.class_attribute :made_resourceful

0 comments on commit c7b26d6

Please sign in to comment.