Skip to content

Commit

Permalink
Avoid writing to routes.rb when no models exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcharlton authored and fwolfst committed Mar 8, 2017
1 parent 067483e commit 87f1cb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RoutesGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)

def insert_dashboard_routes
unless File.read(rails_routes_file_path).include?(dashboard_routes)
if should_route_dashboard?
route(dashboard_routes)
end
end
Expand Down Expand Up @@ -66,13 +66,21 @@ def dashboard_routes
ERB.new(File.read(routes_file_path)).result(binding)
end

def routes_includes_resources?
File.read(rails_routes_file_path).include?(dashboard_routes)
end

def rails_routes_file_path
Rails.root.join("config/routes.rb")
end

def routes_file_path
File.expand_path(find_in_source_paths("routes.rb.erb"))
end

def should_route_dashboard?
routes_includes_resources? || valid_dashboard_models.any?
end
end
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 @@ -17,6 +17,16 @@
expect(routes).not_to contain("Delayed::Backend::ActiveRecord::Job")
end

it "does not populate routes when no models exist" do
stub_generator_dependencies
routes = file("config/routes.rb")
allow(ActiveRecord::Base).to receive(:descendants).and_return([])

run_generator

expect(routes).not_to contain("namespace :admin")
end

it "skips namespaced models with a warning" do
stub_generator_dependencies
routes = file("config/routes.rb")
Expand Down

0 comments on commit 87f1cb3

Please sign in to comment.