Skip to content

Commit

Permalink
Enhanced Rails.root to take parameters that'll be join with the root,…
Browse files Browse the repository at this point in the history
… like Rails.root('app', 'controllers') => File.join(Rails.root, 'app', 'controllers') [#1482 state:committed] (Damian Janowski)
  • Loading branch information
dhh committed Nov 29, 2008
1 parent 1e8f963 commit fdfcdf4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -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]
Expand Down
8 changes: 2 additions & 6 deletions railties/lib/initializer.rb
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions railties/test/initializer_test.rb
Expand Up @@ -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

@Roman2K
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Roman2K
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the question about *args.compact.

@djanowski
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.