Last active
April 20, 2018 19:56
-
-
Save ahoward/284bbce45fc5efaf0ceed424eb63aa2e to your computer and use it in GitHub Desktop.
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
# sometimes you need to calculate the rails_root from a subdirectory of a rails' app, eg, lib/capistrano/tasks/db.rake | |
# this code does it properly, but is verbose. golf it! | |
# | |
rails_root = File.expand_path(Dir.pwd) | |
until rails_root == '/' | |
if %w[ Gemfile app public ].all?{|entry| test(?e, "#{ rails_root }/#{ entry }")} | |
break | |
else | |
rails_root = File.dirname(rails_root) | |
end | |
rails_root = File.dirname(rails_root) | |
end | |
raise 'no rails_root!' if rails_root == '/' |
@bradpauly - yes on both 'Gemfile' and 'app' !
based on your awesome suggestions:
Pathname.pwd.ascend{|d| f = d.join('config/application.rb') and f.exist? && f.read.match('Rails') && (break(d))}
# or
Pathname.pwd.ascend{|d| (d.join('config/application.rb').read.match('Rails') && (break(d))) rescue nil}
# or
Pathname.pwd.ascend{|d| (d.join('Gemfile').read.match('rails') && (break(d))) rescue 42}
@ahoward Nice, I like reading config/application.rb
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@joelparkerhenderson - oh! Pathname.ascend is teh sweet!