Skip to content

Commit

Permalink
fix redirect-after-create for shallow nested resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Bullock committed Feb 14, 2011
1 parent 7e915a6 commit c7b26d6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/resourceful/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(kontroller)
@responses = {}
@publish = {}
@parents = []
@shallow_route = false
@shallow_parent = nil
@custom_member_actions = []
@custom_collection_actions = []
end
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/resourceful/default/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/resourceful/default/urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/resourceful/maker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c7b26d6

Please sign in to comment.