Skip to content

Commit

Permalink
Added generator helper to find project's routes.rb config
Browse files Browse the repository at this point in the history
thoughtbot#720

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.
  • Loading branch information
rnice01 authored and dyba committed Feb 11, 2018
1 parent 642d737 commit 827a871
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
21 changes: 21 additions & 0 deletions lib/administrate/generator_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
module Administrate
module GeneratorHelpers
EXCLUDE_DIRS = ['.git', 'vendor', 'test', 'spec', 'public', 'db']

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 EXCLUDE_DIRS.include? path
next unless path.include? file_name
file = path
end
file
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/administrate/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Rails.application.eager_load!
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"

Expand Down Expand Up @@ -76,7 +78,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
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/administrate/generator_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 827a871

Please sign in to comment.