forked from rnice01/administrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added generator helper to find project's routes.rb config
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
Showing
4 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |