diff options
author | José Valim <jose.valim@gmail.com> | 2011-06-21 08:02:47 -0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-06-21 11:24:05 -0300 |
commit | a803aec00d3f7cbe09c3ac0233265754ddd0025f (patch) | |
tree | 85217b234844d680513d6de527fa3b822c41d047 | |
parent | 8a889dd45e088014c77f4cd799c9e5ddea0b54c6 (diff) | |
download | rails-a803aec00d3f7cbe09c3ac0233265754ddd0025f.tar.gz rails-a803aec00d3f7cbe09c3ac0233265754ddd0025f.tar.bz2 rails-a803aec00d3f7cbe09c3ac0233265754ddd0025f.zip |
Add Rails.groups to encapsulate available groups for require.
-rw-r--r-- | railties/lib/rails.rb | 23 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 15 |
2 files changed, 38 insertions, 0 deletions
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index cca0891835..df92934457 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -87,6 +87,29 @@ module Rails RAILS_CACHE end + # Returns all rails groups for loading based on: + # + # * The Rails environment; + # * The environment variable RAILS_GROUPS; + # * The optional hash given as argument with group dependencies; + # + # == Examples + # + # groups :assets => [:development, :test] + # + # # Returns + # # => [:default, :development, :assets] for Rails.env == "development" + # # => [:default, :production] for Rails.env == "production" + # + def groups(hash={}) + env = Rails.env + groups = [:default, env] + groups.concat ENV["RAILS_GROUPS"].to_s.split(",") + groups.concat hash.map { |k,v| k if v.map(&:to_s).include?(env) } + groups.compact! + groups + end + def version VERSION::STRING end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 477dada820..2547863d12 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -39,6 +39,21 @@ module ApplicationTests FileUtils.rm_rf(new_app) if File.directory?(new_app) end + test "Rails.groups returns available groups" do + require "rails" + + Rails.env = "development" + assert_equal [:default, "development"], Rails.groups + assert_equal [:default, "development", :assets], Rails.groups(:assets => [:development]) + assert_equal [:default, "development", :assets], Rails.groups(:assets => %w(development)) + + Rails.env = "test" + assert_equal [:default, "test"], Rails.groups(:assets => [:development]) + + ENV["RAILS_GROUPS"] = "javascripts,stylesheets" + assert_equal [:default, "test", "javascripts", "stylesheets"], Rails.groups + end + test "Rails.application is nil until app is initialized" do require 'rails' assert_nil Rails.application |