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
12 changes: 3 additions & 9 deletions lib/generators/rails/content/content_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,9 @@ def add_root_action
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
root_action = " def root\n @resource = Content::#{class_name}.root\n\n render :show\n end\n\n"

inject_into_file controller_file, root_action, after: "class Content::#{plural_class_name}Controller < ApplicationController\n"
end

def create_root_content_file
Expand Down
67 changes: 61 additions & 6 deletions test/generators/perron/content_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,53 @@ class ContentGeneratorTest < Rails::Generators::TestCase
test "pages generates root action and route by default" do
run_generator %w[page]

assert_file "config/routes.rb", /root to: "content\/pages#root"/
assert_file "app/models/content/page.rb", /class Content::Page/
assert_file "app/controllers/content/pages_controller.rb" do |content|
assert_match (/class Content::PagesController/), content
assert_match(/ def root/, content)
assert_match(/ @resource = Content::Page\.root/, content)
assert_match(/ render :show/, content)
expected = <<~CONTROLLER
class Content::PagesController < ApplicationController
def root
@resource = Content::Page.root

render :show
end

def index
@resources = Content::Page.all
end

def show
@resource = Content::Page.find!(params[:id])
end
end
CONTROLLER

assert_equal expected, content, "Controller should have properly formatted root action"
end

assert_file "app/content/pages/root.erb", /Find me in `app\/content\/pages\/root\.erb`/
assert_file "config/routes.rb", /root to: "content\/pages#root"/
end

test "pages with show action only generates root and show" do
run_generator %w[page show]

assert_file "app/controllers/content/pages_controller.rb" do |content|
expected = <<~CONTROLLER
class Content::PagesController < ApplicationController
def root
@resource = Content::Page.root

render :show
end

def show
@resource = Content::Page.find!(params[:id])
end
end
CONTROLLER

assert_equal expected, content, "Controller should have properly formatted root and show actions"
end
end

test "destroy removes files without crashing" do
Expand Down Expand Up @@ -159,7 +196,25 @@ class ContentGeneratorTest < Rails::Generators::TestCase
run_generator %w[post --include-root]

assert_file "app/controllers/content/posts_controller.rb" do |content|
assert_match (/def root/), content
expected = <<~CONTROLLER
class Content::PostsController < ApplicationController
def root
@resource = Content::Post.root

render :show
end

def index
@resources = Content::Post.all
end

def show
@resource = Content::Post.find!(params[:id])
end
end
CONTROLLER

assert_equal expected, content, "Controller should have properly formatted root action"
end

assert_file "app/content/posts/root.erb"
Expand Down
Loading