Skip to content

Commit

Permalink
Enable resources_controller to handle dynamic segments that do not ha…
Browse files Browse the repository at this point in the history
…ve a corresponding static segment
  • Loading branch information
Tom ten Thij authored and ianwhite committed Jun 27, 2009
1 parent b22c113 commit 54cea4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
15 changes: 13 additions & 2 deletions lib/ardes/resources_controller.rb
Expand Up @@ -725,11 +725,22 @@ def remove_namespaces_from_segments(segments)
#
# This is used to map resources and automatically load resources.
def route_enclosing_names
last_segment_type = nil
@route_enclosing_names ||= returning(Array.new) do |req|
enclosing_segments.each do |segment|
unless segment.is_optional or segment.is_a?(::ActionController::Routing::DividerSegment)
req << [segment.value, true] if segment.is_a?(::ActionController::Routing::StaticSegment)
req.last[1] = false if segment.is_a?(::ActionController::Routing::DynamicSegment)
if segment.is_a?(::ActionController::Routing::StaticSegment)
req << [segment.value, true]
last_segment_type = :static
end
if segment.is_a?(::ActionController::Routing::DynamicSegment)
if last_segment_type == :static
req.last[1] = false
else
req << [segment.key.to_s.sub('_id','').pluralize, false]
end
last_segment_type = :dynamic
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/app.rb
Expand Up @@ -51,6 +51,10 @@
end

map.resources :tags

map.with_options :path_prefix => ":tag_id", :name_prefix => "tag_" do |tag|
tag.resources :forums
end

# the following routes are for testing errors
map.resources :posts, :controller => 'forum_posts'
Expand Down Expand Up @@ -295,4 +299,4 @@ class InterestsController < ApplicationController

# the above two lines are the same as:
# resources_controller_for :interests, :in => '?interested_in'
end
end
14 changes: 13 additions & 1 deletion spec/lib/route_enclosing_names_spec.rb
Expand Up @@ -92,4 +92,16 @@
@controller.stub!(:recognized_route).and_return(@routes[:admin_superduper_forums])
@controller.send(:route_enclosing_names).should == []
end
end
end

describe "#route_enclosing_names for route with a dynamic segment with no corresponding static segment" do
before do
@routes = ActionController::Routing::Routes.named_routes
@controller = Admin::Superduper::ForumsController.new
end

it ':tag_forums should be [["tags", false]]' do
@controller.stub!(:recognized_route).and_return(@routes[:tag_forums])
@controller.send(:route_enclosing_names).should == [["tags", false]]
end
end

0 comments on commit 54cea4a

Please sign in to comment.