1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Support multiple config.after_initialize blocks so plugins and apps can more easily cooperate. Closes #9582.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7507 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-09-18 01:45:37 +00:00
parent 71e33d9650
commit 4f375d5b82
3 changed files with 79 additions and 13 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Support multiple config.after_initialize blocks so plugins and apps can more easily cooperate. #9582 [zdennis]
* Added db:drop:all to drop all databases declared in config/database.yml [DHH] * Added db:drop:all to drop all databases declared in config/database.yml [DHH]
* Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo] * Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo]

View file

@ -325,7 +325,9 @@ module Rails
# Fires the user-supplied after_initialize block (Configuration#after_initialize) # Fires the user-supplied after_initialize block (Configuration#after_initialize)
def after_initialize def after_initialize
configuration.after_initialize_block.call if configuration.after_initialize_block configuration.after_initialize_blocks.each do |block|
block.call
end
end end
def load_application_initializers def load_application_initializers
@ -515,16 +517,16 @@ module Rails
::RAILS_ENV ::RAILS_ENV
end end
# Sets a block which will be executed after rails has been fully initialized. # Adds a block which will be executed after rails has been fully initialized.
# Useful for per-environment configuration which depends on the framework being # Useful for per-environment configuration which depends on the framework being
# fully initialized. # fully initialized.
def after_initialize(&after_initialize_block) def after_initialize(&after_initialize_block)
@after_initialize_block = after_initialize_block after_initialize_blocks << after_initialize_block if after_initialize_block
end end
# Returns the block set in Configuration#after_initialize # Returns the blocks added with Configuration#after_initialize
def after_initialize_block def after_initialize_blocks
@after_initialize_block @after_initialize_blocks ||= []
end end
# Add a preparation callback that will run before every request in development # Add a preparation callback that will run before every request in development

View file

@ -1,15 +1,16 @@
require "#{File.dirname(__FILE__)}/abstract_unit" require "#{File.dirname(__FILE__)}/abstract_unit"
require 'initializer' require 'initializer'
class InitializerTest < Test::Unit::TestCase class ConfigurationMock < Rails::Configuration
class ConfigurationMock < Rails::Configuration attr_reader :environment_path
attr_reader :environment_path
def initialize(envpath) def initialize(envpath)
super() super()
@environment_path = envpath @environment_path = envpath
end
end end
end
class Initializer_load_environment_Test < Test::Unit::TestCase
def test_load_environment_with_constant def test_load_environment_with_constant
config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb") config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
@ -19,4 +20,65 @@ class InitializerTest < Test::Unit::TestCase
ensure ensure
$initialize_test_set_from_env = nil $initialize_test_set_from_env = nil
end end
end
class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
def setup
config = ConfigurationMock.new("")
config.after_initialize do
$test_after_initialize_block1 = "success"
end
config.after_initialize do
$test_after_initialize_block2 = "congratulations"
end
assert_nil $test_after_initialize_block1
assert_nil $test_after_initialize_block2
Rails::Initializer.run(:after_initialize, config)
end
def teardown
$test_after_initialize_block1 = nil
$test_after_initialize_block2 = nil
end
def test_should_have_called_the_first_after_initialize_block
assert_equal "success", $test_after_initialize_block1
end
def test_should_have_called_the_second_after_initialize_block
assert_equal "congratulations", $test_after_initialize_block2
end
end
class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
def setup
config = ConfigurationMock.new("")
config.after_initialize do
$test_after_initialize_block1 = "success"
end
config.after_initialize # don't pass a block, this is what we're testing!
config.after_initialize do
$test_after_initialize_block2 = "congratulations"
end
assert_nil $test_after_initialize_block1
Rails::Initializer.run(:after_initialize, config)
end
def teardown
$test_after_initialize_block1 = nil
$test_after_initialize_block2 = nil
end
def test_should_have_called_the_first_after_initialize_block
assert_equal "success", $test_after_initialize_block1, "should still get set"
end
def test_should_have_called_the_first_after_initialize_block
assert_equal "congratulations", $test_after_initialize_block2
end
end end