Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the configuration_tests pass

  • Loading branch information...
commit 2cd81d0800166d916e63f892e2dbd2fcb90d9470 1 parent c4f9e10
@rafaelfranca rafaelfranca authored
View
11 lib/rails-observers.rb
@@ -1,8 +1,9 @@
-require "rails/observers/version"
+require 'rails'
+require 'rails/observers/version'
module Rails
- module Observes
- module Railtie < Rails::Railtie
+ module Observers
+ class Railtie < ::Rails::Railtie
initializer "active_record.observer" do
ActiveSupport.on_load(:active_record) do
require "rails/observers/activerecord/active_record"
@@ -17,10 +18,10 @@ module Railtie < Rails::Railtie
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
- ActiveRecord::Model.instantiate_observers
+ ActiveRecord::Base.instantiate_observers
ActionDispatch::Reloader.to_prepare do
- ActiveRecord::Model.instantiate_observers
+ ActiveRecord::Base.instantiate_observers
end
end
end
View
2  rails-observers.gemspec
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Rails::Observers::VERSION
+ gem.add_dependency 'railties', '~> 4.0.0.beta'
+
gem.add_development_dependency 'minitest', '>= 3'
gem.add_development_dependency 'mocha'
gem.add_development_dependency 'activerecord', '~> 4.0.0.beta'
View
77 test/configuration_test.rb
@@ -1,58 +1,41 @@
-class ::MyMailObserver
- def self.delivered_email(email); email; end
-end
-
-class ::MyOtherMailObserver < ::MyMailObserver; end
-
-module ApplicationTests
- class ConfigurationTest < ActiveSupport::TestCase
- include ActiveSupport::Testing::Isolation
-
- test "registers observers with ActionMailer" do
- add_to_config <<-RUBY
- config.action_mailer.observers = MyMailObserver
- RUBY
-
- require "#{app_path}/config/environment"
- require "mail"
+require "isolation/abstract_unit"
+require "rails-observers"
- _ = ActionMailer::Base
+class ConfigurationTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
- assert_equal [::MyMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
- end
-
- test "registers multiple observers with ActionMailer" do
- add_to_config <<-RUBY
- config.action_mailer.observers = [MyMailObserver, "MyOtherMailObserver"]
- RUBY
-
- require "#{app_path}/config/environment"
- require "mail"
+ def app
+ @app ||= Rails.application
+ end
- _ = ActionMailer::Base
+ def setup
+ build_app
+ boot_rails
+ FileUtils.rm_rf("#{app_path}/config/environments")
+ end
- assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
- end
+ def teardown
+ teardown_app
+ end
- test "config.active_record.observers" do
- add_to_config <<-RUBY
- config.active_record.observers = :foo_observer
- RUBY
+ test "config.active_record.observers" do
+ add_to_config <<-RUBY
+ config.active_record.observers = :foo_observer
+ RUBY
- app_file 'app/models/foo.rb', <<-RUBY
- class Foo < ActiveRecord::Base
- end
- RUBY
+ app_file 'app/models/foo.rb', <<-RUBY
+ class Foo < ActiveRecord::Base
+ end
+ RUBY
- app_file 'app/models/foo_observer.rb', <<-RUBY
- class FooObserver < ActiveRecord::Observer
- end
- RUBY
+ app_file 'app/models/foo_observer.rb', <<-RUBY
+ class FooObserver < ActiveRecord::Observer
+ end
+ RUBY
- require "#{app_path}/config/environment"
+ require "#{app_path}/config/environment"
- ActiveRecord::Base
- assert defined?(FooObserver)
- end
+ ActiveRecord::Base
+ assert defined?(FooObserver)
end
end
View
104 test/isolation/abstract_unit.rb
@@ -0,0 +1,104 @@
+# Note:
+# It is important to keep this file as light as possible
+# the goal for tests that require this is to test booting up
+# rails from an empty state, so anything added here could
+# hide potential failures
+#
+# It is also good to know what is the bare minimum to get
+# Rails booted up.
+require 'fileutils'
+
+require 'bundler/setup'
+require 'minitest/autorun'
+require 'active_support/test_case'
+
+# These files do not require any others and are needed
+# to run the tests
+require "active_support/testing/isolation"
+require "active_support/core_ext/kernel/reporting"
+require 'tmpdir'
+
+module TestHelpers
+ module Paths
+ def app_template_path
+ File.join Dir.tmpdir, 'app_template'
+ end
+
+ def tmp_path(*args)
+ @tmp_path ||= File.realpath(Dir.mktmpdir)
+ File.join(@tmp_path, *args)
+ end
+
+ def app_path(*args)
+ tmp_path(*%w[app] + args)
+ end
+ end
+
+ module Generation
+ # Build an application by invoking the generator and going through the whole stack.
+ def build_app(options = {})
+ @prev_rails_env = ENV['RAILS_ENV']
+ ENV['RAILS_ENV'] = 'development'
+
+ FileUtils.rm_rf(app_path)
+ FileUtils.cp_r(app_template_path, app_path)
+
+ # Delete the initializers unless requested
+ unless options[:initializers]
+ Dir["#{app_path}/config/initializers/*.rb"].each do |initializer|
+ File.delete(initializer)
+ end
+ end
+
+ unless options[:gemfile]
+ File.delete "#{app_path}/Gemfile"
+ end
+
+ routes = File.read("#{app_path}/config/routes.rb")
+ if routes =~ /(\n\s*end\s*)\Z/
+ File.open("#{app_path}/config/routes.rb", 'w') do |f|
+ f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)', :via => :all\n" + $1
+ end
+ end
+
+ add_to_config <<-RUBY
+ config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
+ config.session_store :cookie_store, :key => "_myapp_session"
+ config.active_support.deprecation = :log
+ config.action_controller.allow_forgery_protection = false
+ config.eager_load = false
+ RUBY
+ end
+
+ def teardown_app
+ ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env
+ end
+
+ def add_to_config(str)
+ environment = File.read("#{app_path}/config/application.rb")
+ if environment =~ /(\n\s*end\s*end\s*)\Z/
+ File.open("#{app_path}/config/application.rb", 'w') do |f|
+ f.puts $` + "\n#{str}\n" + $1
+ end
+ end
+ end
+
+ def app_file(path, contents)
+ FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
+ File.open("#{app_path}/#{path}", 'w') do |f|
+ f.puts contents
+ end
+ end
+
+ def boot_rails
+ require 'rubygems' unless defined? Gem
+ require 'bundler'
+ Bundler.setup
+ end
+ end
+end
+
+class ActiveSupport::TestCase
+ include TestHelpers::Paths
+ include TestHelpers::Generation
+end
Please sign in to comment.
Something went wrong with that request. Please try again.