diff --git a/lib/administrate/generator_helpers.rb b/lib/administrate/generator_helpers.rb index e0f07a2c53..bc3a4511e0 100644 --- a/lib/administrate/generator_helpers.rb +++ b/lib/administrate/generator_helpers.rb @@ -4,10 +4,29 @@ 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.file?(routes_file) + routes_file = find_file("routes.rb") + raise "Unable to locate your routes.rb file" unless !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 diff --git a/lib/generators/administrate/install/install_generator.rb b/lib/generators/administrate/install/install_generator.rb index 2ef5bca672..afdf8197c2 100644 --- a/lib/generators/administrate/install/install_generator.rb +++ b/lib/generators/administrate/install/install_generator.rb @@ -13,7 +13,7 @@ class InstallGenerator < Rails::Generators::Base def run_routes_generator if dashboard_resources.none? call_generator("administrate:routes", "--namespace", namespace) - load Rails.root.join("config/routes.rb") + load find_routes_file end end diff --git a/lib/generators/administrate/routes/routes_generator.rb b/lib/generators/administrate/routes/routes_generator.rb index c88b444baa..7dcca423d1 100644 --- a/lib/generators/administrate/routes/routes_generator.rb +++ b/lib/generators/administrate/routes/routes_generator.rb @@ -5,11 +5,13 @@ end require "rails/generators/base" +require "administrate/generator_helpers" require "administrate/namespace" module Administrate module Generators class RoutesGenerator < Rails::Generators::Base + include Administrate::GeneratorHelpers source_root File.expand_path("../templates", __FILE__) class_option :namespace, type: :string, default: "admin" @@ -81,7 +83,7 @@ def routes_includes_resources? end def rails_routes_file_path - Rails.root.join("config/routes.rb") + find_routes_file end def routes_file_path diff --git a/spec/lib/administrate/generator_helpers_spec.rb b/spec/lib/administrate/generator_helpers_spec.rb new file mode 100644 index 0000000000..a1cbcce088 --- /dev/null +++ b/spec/lib/administrate/generator_helpers_spec.rb @@ -0,0 +1,16 @@ +require "rails_helper" +include Administrate::GeneratorHelpers + +RSpec.describe Administrate::GeneratorHelpers do + describe "#find_routes_file" do + before (:all) do + @typical_routes_path = "config/routes.rb" + end + + it "returns the typical path for a Rails routes file if found" do + returned_path = find_routes_file + + expect(returned_path.to_s).to include(@typical_routes_path) + end + end +end