Skip to content
Merged
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
9 changes: 5 additions & 4 deletions lib/generators/rails/content/content_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/generators/perron/content_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Loading