Skip to content

Translate the has_many routes to nested plural resources. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/routes_upgrader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ def to_route_code
unless copied_options.empty?
copied_options_str = ", " + copied_options.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(",")
end

add_has_many_to_children

if [email protected]? || @options.has_key?(:collection) || @options.has_key?(:member)
prefix = ["#{route_method} :#{@name}#{copied_options_str} do"]
Expand All @@ -279,6 +281,16 @@ def to_route_code
indent_lines [base % [@name]]
end
end

def add_has_many_to_children
if @options[:has_many].is_a?(Symbol)
@children << FakeResourceRoute.new(@options[:has_many])
elsif @options[:has_many].is_a?(Array)
@options[:has_many].each do |resource|
@children << FakeResourceRoute.new(resource)
end
end
end

def custom_methods
collection_code = generate_custom_methods_for(:collection)
Expand Down
24 changes: 24 additions & 0 deletions test/routes_upgrader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,28 @@ def test_generates_code_for_delete_route
upgrader.routes_code = routes_code
assert_equal new_routes_code.strip, upgrader.generate_new_routes.strip
end

def test_generates_code_for_resources_with_has_many_option
routes_code = %Q{
ActionController::Routing::Routes.draw do |map|
map.resources :ducks, :has_many => [:geese]
end
}

new_routes_code = %Q{
MyApplication::Application.routes.draw do
resources :ducks do


resources :geese
end

end
}

upgrader = Rails::Upgrading::RoutesUpgrader.new
upgrader.routes_code = routes_code

assert_equal new_routes_code.strip, upgrader.generate_new_routes.strip
end
end