diff --git a/lib/generators/rails/content/content_generator.rb b/lib/generators/rails/content/content_generator.rb index d78089f..9af1bdb 100644 --- a/lib/generators/rails/content/content_generator.rb +++ b/lib/generators/rails/content/content_generator.rb @@ -86,16 +86,17 @@ def create_data_sources def add_root_action return if @content_mode return unless should_include_root? - return unless File.exist?(File.join(destination_root, "app/controllers/content/#{plural_file_name}_controller.rb")) - inject_into_class "app/controllers/content/#{plural_file_name}_controller.rb", "Content::#{plural_class_name}Controller" do - <<~RUBY.indent(2) + controller_file = "app/controllers/content/#{plural_file_name}_controller.rb" + return unless File.exist?(File.join(destination_root, controller_file)) + + inject_into_file controller_file, after: "class Content::#{plural_class_name}Controller < ApplicationController\n" do + <<-RUBY.strip.indent(2) def root @resource = Content::#{class_name}.root render :show end - RUBY end end diff --git a/test/generators/perron/content_generator_test.rb b/test/generators/perron/content_generator_test.rb index 68832d3..c4003b9 100644 --- a/test/generators/perron/content_generator_test.rb +++ b/test/generators/perron/content_generator_test.rb @@ -132,6 +132,16 @@ class ContentGeneratorTest < Rails::Generators::TestCase assert_file "config/routes.rb", /root to: "content\/pages#root"/ end + test "destroy removes files without crashing" do + run_generator %w[page] + run_generator %w[page], behavior: :revoke + + assert_no_file "app/models/content/page.rb" + assert_no_file "app/controllers/content/pages_controller.rb" + assert_no_file "app/views/content/pages" + assert_no_file "app/content/pages/root.erb" + end + test "pages with --no-include-root skips root generation" do run_generator %w[page --no-include-root]