Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid generating invalid root route when there's no model #555

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def unnamed_constants
end

def dashboard_routes
ERB.new(File.read(routes_file_path)).result(binding)
erb = ERB.new(File.read(routes_file_path), nil, "-")
erb.result(binding)
end

def rails_routes_file_path
Expand Down
6 changes: 4 additions & 2 deletions lib/generators/administrate/routes/templates/routes.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace :admin do
<% dashboard_resources.each do |resource| %>resources :<%= resource %>
<% end %>
<% dashboard_resources.each do |resource| %> resources :<%= resource %>
<% end -%>
<% unless dashboard_resources.empty? -%>
root to: "<%= dashboard_resources.first %>#index"
<% end -%>
end
10 changes: 10 additions & 0 deletions spec/generators/routes_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ def self.table_name

expect(routes).to contain('root to: "customers#index')
end

it "does not create a invalid root route when there's no model" do
allow(ActiveRecord::Base).to receive(:descendants).and_return([])
stub_generator_dependencies
routes = file("config/routes.rb")

run_generator

expect(routes).not_to contain('root to: "#index"')
end
end