Skip to content

Commit

Permalink
Merge pull request #22083 from thejamespinto/idempotent-route-generator
Browse files Browse the repository at this point in the history
Route generator should be idempotent
  • Loading branch information
pixeltrix committed Nov 1, 2015
2 parents b217354 + a9f9e1d commit d53de10
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,7 @@
* Route generator should be idempotent
running generators several times no longer require you to cleanup routes.rb

*Thiago Pinto*
* Allow passing an environment to `config_for`.

*Simon Eskildsen*
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/actions.rb
Expand Up @@ -235,7 +235,7 @@ def route(routing_code)
sentinel = /\.routes\.draw do\s*\n/m

in_root do
inject_into_file 'config/routes.rb', " #{routing_code}\n", { after: sentinel, verbose: false, force: true }
inject_into_file 'config/routes.rb', " #{routing_code}\n", { after: sentinel, verbose: false, force: false }
end
end

Expand Down
15 changes: 15 additions & 0 deletions railties/test/generators/actions_test.rb
Expand Up @@ -235,6 +235,21 @@ def test_route_should_add_data_to_the_routes_block_in_config_routes
assert_file 'config/routes.rb', /#{Regexp.escape(route_command)}/
end

def test_route_should_be_idempotent
run_generator
route_path = File.expand_path('config/routes.rb', destination_root)

# runs first time, not asserting
action :route, "root 'welcome#index'"
content_1 = File.read(route_path)

# runs second time
action :route, "root 'welcome#index'"
content_2 = File.read(route_path)

assert_equal content_1, content_2
end

def test_route_should_add_data_with_an_new_line
run_generator
action :route, "root 'welcome#index'"
Expand Down

0 comments on commit d53de10

Please sign in to comment.