Permalink
Browse files

Enhanced Rails.root to take parameters that'll be join with the root,…

… like Rails.root('app', 'controllers') => File.join(Rails.root, 'app', 'controllers') [#1482 state:committed] (Damian Janowski)
  • Loading branch information...
1 parent 1e8f963 commit fdfcdf467387c4db3d79c1f46eadbb55a88ef814 @dhh dhh committed Nov 29, 2008
Showing with 14 additions and 6 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +2 −6 railties/lib/initializer.rb
  3. +10 −0 railties/test/initializer_test.rb
View
@@ -1,5 +1,7 @@
*2.3.0 [Edge]*
+* Enhanced Rails.root to take parameters that'll be join with the root, like Rails.root('app', 'controllers') => File.join(Rails.root, 'app', 'controllers') #1482 [Damian Janowski]
+
* Added view path support for engines [DHH]
* Added that config/routes.rb files in engine plugins are automatically loaded (and reloaded when they change in dev mode) [DHH]
@@ -48,12 +48,8 @@ def backtrace_cleaner
end
end
- def root
- if defined?(RAILS_ROOT)
- RAILS_ROOT
- else
- nil
- end
+ def root(*args)
+ File.join(RAILS_ROOT, *args.compact) if defined?(RAILS_ROOT)
end
def env
@@ -311,3 +311,13 @@ def test_setting_another_default_locale
end
end
end
+
+class RailsRootTest < Test::Unit::TestCase
+ def test_rails_dot_root_equals_rails_root
+ assert_equal RAILS_ROOT, Rails.root
+ end
+
+ def test_rails_dot_root_accepts_arguments_for_file_dot_join
+ assert_equal File.join(RAILS_ROOT, 'app', 'controllers'), Rails.root('app', 'controllers')
+ end
+end

4 comments on commit fdfcdf4

I would rather make Rails.root return Pathname(RAILS_ROOT) to allow for this instead: Rails.root.join('app', 'controllers'). Makes more sense and reads better.

Contributor

matthewrudy replied Nov 29, 2008

what’s the purpose of calling *args.compact?

if you have code like Rails.root(“app”, @subdirectory),
it feels like it should raise an exception if that ends up being nil,
rather than silently failing.

+1 for the question about *args.compact.

Contributor

djanowski replied Nov 29, 2008

My original patch didn’t include the call to `#compact` and I agree it should be avoided. My comments here http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1482-allow-railsroot-to-take-arguments-to-build-a-path-with-filejoin#ticket-1482-5

Please sign in to comment.