Skip to content

Commit

Permalink
Rely on Rails' route method to be correct
Browse files Browse the repository at this point in the history
Previously, we were searching for a `routes.rb` to verify that we'd
already added the routes or not. We don't need to do this: the `route`
method will only ever inject into the `config/routes.rb` file it knows
about — so us looking elsewhere would never make a difference to the
outcome of the generator.

On top of this, the `route` method in turn leans on `inject_into_file`
from Thor which is configured to not force the result.

So, much of this can go. We just continue to check that we don't
generate routes with no models.

https://github.com/rails/rails/blob/291a3d2ef29a3842d1156ada7526f4ee60dd2b59/railties/lib/rails/generators/actions.rb#L269
https://github.com/erikhuda/thor/blob/34df888d721ecaa8cf0cea97d51dc6c388002742/lib/thor/actions/inject_into_file.rb#L26
  • Loading branch information
nickcharlton committed Feb 16, 2021
1 parent 701f383 commit 376d4ff
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 49 deletions.
21 changes: 0 additions & 21 deletions lib/administrate/generator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,10 @@ def call_generator(generator, *args)
Rails::Generators.invoke(generator, args, generator_options)
end

def find_routes_file
routes_file = Rails.root.join("config/routes.rb")
if File.exist?(routes_file)
routes_file = find_file("routes.rb")
raise "Unable to locate your routes.rb file" if routes_file.nil?
end
routes_file
end

private

def generator_options
{ behavior: behavior }
end

def find_file(file_name)
file = nil
Find.find(Rails.root) do |path|
Find.prune if path.include? ".git"

next unless path.include? file_name

file = path
end
file
end
end
end
14 changes: 1 addition & 13 deletions lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RoutesGenerator < Rails::Generators::Base
class_option :namespace, type: :string, default: "admin"

def insert_dashboard_routes
if should_route_dashboard?
if valid_dashboard_models.any?
route(dashboard_routes)
end
end
Expand Down Expand Up @@ -78,21 +78,9 @@ 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
find_routes_file
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
15 changes: 0 additions & 15 deletions spec/lib/administrate/generator_helpers_spec.rb

This file was deleted.

0 comments on commit 376d4ff

Please sign in to comment.