Permalink
Browse files

Add Rails.groups to encapsulate available groups for require.

  • Loading branch information...
1 parent 7ff4aed commit 77b00cb500302ac9e1ba5ade2010b414c3e6741f @josevalim josevalim committed Jun 21, 2011
Showing with 38 additions and 0 deletions.
  1. +23 −0 railties/lib/rails.rb
  2. +15 −0 railties/test/application/configuration_test.rb
View
23 railties/lib/rails.rb
@@ -87,6 +87,29 @@ def cache
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
View
15 railties/test/application/configuration_test.rb
@@ -39,6 +39,21 @@ def teardown
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

0 comments on commit 77b00cb

Please sign in to comment.