From cf109b2d5f7800121188ea506eeee4079a11ba4e Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 7 Oct 2017 01:42:54 +0000 Subject: [PATCH] Added generator helper to find project's routes.rb config Because there may be Rails projects with the routes.rb file in a different location than Rails.root/config a helper was added to locate the file. The helper will look in the 'normal' routes.rb location and then search through the project if not located there. Fixes #720 --- lib/administrate/generator_helpers.rb | 21 +++++++++++++++++++ .../administrate/install/install_generator.rb | 2 +- .../administrate/routes/routes_generator.rb | 4 +++- .../administrate/generator_helpers_spec.rb | 15 +++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 spec/lib/administrate/generator_helpers_spec.rb diff --git a/lib/administrate/generator_helpers.rb b/lib/administrate/generator_helpers.rb index e0f07a2c53..ae220b0ba9 100644 --- a/lib/administrate/generator_helpers.rb +++ b/lib/administrate/generator_helpers.rb @@ -4,10 +4,31 @@ 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 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..9262562381 --- /dev/null +++ b/spec/lib/administrate/generator_helpers_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +RSpec.describe Administrate::GeneratorHelpers do + include Administrate::GeneratorHelpers + + describe "#find_routes_file" do + it "returns the typical path for a Rails routes file if found" do + typical_routes_path = "config/routes.rb" + + returned_path = find_routes_file + + expect(returned_path.to_s).to include(typical_routes_path) + end + end +end