Permalink
Browse files

Do not depend on railties, only load railtie if Rails is defined

  • Loading branch information...
1 parent 0fa0863 commit ad733ef5c73acd6fc6eea70a389c84248590c714 Rafael Mendonça França committed Dec 8, 2012
Showing with 37 additions and 38 deletions.
  1. +1 −33 lib/rails-observers.rb
  2. +33 −0 lib/rails/observers/railtie.rb
  3. +2 −3 rails-observers.gemspec
  4. +1 −2 rails-observers.gemspec.erb
View
@@ -1,34 +1,2 @@
-require 'rails'
require 'rails/observers/version'
-
-module Rails
- module Observers
- class Railtie < ::Rails::Railtie
- initializer "active_record.observer", :before => "active_record.set_configs" do |app|
- ActiveSupport.on_load(:active_record) do
- require "rails/observers/activerecord/active_record"
-
- if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
- send :observers=, observers
- end
- end
- end
-
- initializer "action_controller.caching.sweepers" do
- ActiveSupport.on_load(:action_controller) do
- require "rails/observers/action_controller/caching"
- end
- end
-
- config.after_initialize do |app|
- ActiveSupport.on_load(:active_record) do
- ActiveRecord::Base.instantiate_observers
-
- ActionDispatch::Reloader.to_prepare do
- ActiveRecord::Base.instantiate_observers
- end
- end
- end
- end
- end
-end
+require 'rails/observers/railtie' if defined? Rails
@spohlenz

spohlenz Dec 9, 2012

The previous require (rails/observers/version) defines the Rails module so this will always try to load the Railtie.

@@ -0,0 +1,33 @@
+require 'rails/railtie'
+
+module Rails
+ module Observers
+ class Railtie < ::Rails::Railtie
+ initializer "active_record.observer", :before => "active_record.set_configs" do |app|
+ ActiveSupport.on_load(:active_record) do
+ require "rails/observers/activerecord/active_record"
+
+ if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
+ send :observers=, observers
+ end
+ end
+ end
+
+ initializer "action_controller.caching.sweepers" do
+ ActiveSupport.on_load(:action_controller) do
+ require "rails/observers/action_controller/caching"
+ end
+ end
+
+ config.after_initialize do |app|
+ ActiveSupport.on_load(:active_record) do
+ ActiveRecord::Base.instantiate_observers
+
+ ActionDispatch::Reloader.to_prepare do
+ ActiveRecord::Base.instantiate_observers
+ end
+ end
+ end
+ end
+ end
+end
View
@@ -10,14 +10,13 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/rails/rails-observers"
s.version = Rails::Observers::VERSION
- s.files = [".gitignore",".travis.yml","Gemfile","LICENSE","README.md","Rakefile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
+ s.files = [".gitignore",".travis.yml","Gemfile","LICENSE","README.md","Rakefile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/railtie.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
s.test_files = ["test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
s.executables = []
s.require_paths = ["lib"]
- s.add_dependency 'railties', '~> 4.0.0.beta'
-
s.add_development_dependency 'minitest', '>= 3'
+ s.add_development_dependency 'railties', '~> 4.0.0.beta'
s.add_development_dependency 'activerecord', '~> 4.0.0.beta'
s.add_development_dependency 'activemodel', '~> 4.0.0.beta'
s.add_development_dependency 'actionmailer', '~> 4.0.0.beta'
@@ -15,9 +15,8 @@ Gem::Specification.new do |s|
s.executables = [<%= executables.map(&:inspect).join ',' %>]
s.require_paths = ["lib"]
- s.add_dependency 'railties', '~> 4.0.0.beta'
-
s.add_development_dependency 'minitest', '>= 3'
+ s.add_development_dependency 'railties', '~> 4.0.0.beta'
s.add_development_dependency 'activerecord', '~> 4.0.0.beta'
s.add_development_dependency 'activemodel', '~> 4.0.0.beta'
s.add_development_dependency 'actionmailer', '~> 4.0.0.beta'

0 comments on commit ad733ef

Please sign in to comment.