From 2688eb08f3b36abe87a5210b4b502387bb3edba4 Mon Sep 17 00:00:00 2001 From: Jeff Kreeftmeijer Date: Tue, 15 Jun 2010 06:18:20 +0200 Subject: [PATCH] Removed the index links from the generator's view templates when using the --singleton option. [#4863 state:resolved] --- .../erb/scaffold/templates/edit.html.erb | 6 +- .../generators/erb/scaffold/templates/new.html.erb | 3 +- .../erb/scaffold/templates/show.html.erb | 6 +- railties/lib/rails/generators/test_case.rb | 36 +++++++++ .../test/generators/scaffold_generator_test.rb | 80 ++++++++++++++++++++ 5 files changed, 126 insertions(+), 5 deletions(-) diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb index 5bc507f..a839eee 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -2,5 +2,7 @@ <%%= render 'form' %> -<%%= link_to 'Show', @<%= singular_name %> %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to 'Show', @<%= singular_name %> %> +<% unless options[:singleton] -%> +| <%%= link_to 'Back', <%= plural_name %>_path %> +<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb index 9a1c489..751bcb8 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -1,5 +1,6 @@

New <%= singular_name %>

<%%= render 'form' %> - +<% unless options[:singleton] -%> <%%= link_to 'Back', <%= plural_name %>_path %> +<% end -%> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb index 6b35187..7a3cb51 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -8,5 +8,7 @@ <% end -%> -<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | -<%%= link_to 'Back', <%= plural_name %>_path %> +<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> +<% unless options[:singleton] -%> +| <%%= link_to 'Back', <%= plural_name %>_path %> +<% end -%> diff --git a/railties/lib/rails/generators/test_case.rb b/railties/lib/rails/generators/test_case.rb index 0dfb5cd..f139e62 100644 --- a/railties/lib/rails/generators/test_case.rb +++ b/railties/lib/rails/generators/test_case.rb @@ -132,6 +132,32 @@ module Rails end alias :assert_no_directory :assert_no_file + # Asserts a given file does not contain any the argument in it's contents. + #You need to supply an absolute path or a path relative to the configured + # destination and another argument you want to check the contents for. + # + # If the argument is a regexp, it will check if the regular expression + # matches the given file content: + # + # assert_not_in_file "config/environment.rb", "/nitialize/ + # + # If it's a string, it compares the file with the given string: + # + # assert_not_in_file "config/environment.rb", "initialize" + # + def assert_not_in_file(relative, *contents) + read = File.read(File.expand_path(relative, destination_root)) + + contents.each do |content| + case content + when String + assert_not_equal content, read + when Regexp + assert_no_match content, read + end + end + end + # Asserts a given file does not exist. You need to supply an absolute path or a # path relative to the configured destination: # @@ -189,6 +215,16 @@ module Rails end alias :assert_method :assert_instance_method + # Asserts the given method doesn't exist in the given content. + # + # assert_file "app/controller/products_controller.rb" do |controller| + # assert_no_instance_method :index, content + # end + # + def assert_no_instance_method(method, content) + assert content !~ /def #{method}(\(.+\))?(.*?)\n end/m, "Expected not to have method #{method}" + end + # Asserts the given attribute type gets translated to a field type # properly: # diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index e8e622f..d36df54 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -80,6 +80,86 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "public/stylesheets/scaffold.css" end + def test_scaffold_index_links + run_generator + + %w( + edit + new + show + ).each do |view| + assert_file "app/views/product_lines/#{view}.html.erb", /<%= link_to 'Back', product_lines_path %>/ + end + end + + def test_singleton_scaffold_on_invoke + run_generator %w(product_line title:string price:integer --singleton) + + # Route + assert_file "config/routes.rb" do |route| + assert_match(/resource :product_line$/, route) + end + + # Controller + assert_file "app/controllers/product_lines_controller.rb" do |content| + assert_match(/class ProductLinesController < ApplicationController/, content) + + assert_no_instance_method :index, content + + assert_instance_method :show, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + end + + assert_instance_method :new, content do |m| + assert_match(/@product_line = ProductLine\.new/, m) + end + + assert_instance_method :edit, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + end + + assert_instance_method :create, content do |m| + assert_match(/@product_line = ProductLine\.new\(params\[:product_line\]\)/, m) + assert_match(/@product_line\.save/, m) + assert_match(/@product_line\.errors/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + assert_match(/@product_line\.update_attributes\(params\[:product_line\]\)/, m) + assert_match(/@product_line\.errors/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + assert_match(/@product_line\.destroy/, m) + end + end + + # Views + %w( + edit + new + show + ).each { |view| assert_file "app/views/product_lines/#{view}.html.erb" } + assert_no_file "app/views/product_lines/index.html.erb" + assert_no_file "app/views/layouts/product_lines.html.erb" + + end + + def test_singleton_scaffold_index_links + run_generator %w(product_line title:string price:integer --singleton) + + %w( + edit + new + show + _form + ).each do |view| + assert_not_in_file "app/views/product_lines/#{view}.html.erb", /<%= link_to 'Back', product_lines_path %>/ + end + end + def test_scaffold_on_revoke run_generator run_generator ["product_line"], :behavior => :revoke -- 1.6.4.1