Module: Spork::AppFramework::Rails::NinjaPatcher

Defined in:
lib/spork/app_framework/rails.rb

Overview

TODO - subclass this out to handle different versions of rails

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/spork/app_framework/rails.rb', line 5

def self.included(klass)
  klass.class_eval do
    unless method_defined?(:load_environment_without_spork)
      alias :load_environment_without_spork :load_environment
      alias :load_environment :load_environment_with_spork
    end
  end
end

Instance Method Details

#auto_reestablish_db_connectionObject



63
64
65
66
67
68
69
70
71
# File 'lib/spork/app_framework/rails.rb', line 63

def auto_reestablish_db_connection
  if Object.const_defined?(:ActiveRecord)
    Spork.each_run do
      # spec/rails is very aggressive about overriding RAILS_ENV and will switch it back to test after the cucumberĀ env was loaded
      reset_rails_env
      ActiveRecord::Base.establish_connection
    end
  end
end

#delay_app_preloadObject



45
46
47
48
49
# File 'lib/spork/app_framework/rails.rb', line 45

def delay_app_preload
  if ::Rails::Initializer.instance_methods.include?('load_application_classes')
    Spork.trap_method(::Rails::Initializer, :load_application_classes)
  end
end

#delay_application_controller_loadingObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spork/app_framework/rails.rb', line 51

def delay_application_controller_loading
  if application_controller_source = ["#{Dir.pwd}/app/controllers/application.rb", "#{Dir.pwd}/app/controllers/application_controller.rb"].find { |f| File.exist?(f) }
    application_helper_source = "#{Dir.pwd}/app/helpers/application_helper.rb"
    load_paths = (::ActiveSupport.const_defined?(:Dependencies) ? ::ActiveSupport::Dependencies : ::Dependencies).load_paths
    load_paths.unshift(File.expand_path('rails_stub_files', File.dirname(__FILE__)))
    Spork.each_run do
      require application_controller_source
      require application_helper_source if File.exist?(application_helper_source)
    end
  end
end

#delay_observer_loadingObject



35
36
37
38
39
40
41
42
43
# File 'lib/spork/app_framework/rails.rb', line 35

def delay_observer_loading
  if ::Rails::Initializer.instance_methods.include?('load_observers')
    Spork.trap_method(::Rails::Initializer, :load_observers)
  end
  if Object.const_defined?(:ActionController)
    require "action_controller/dispatcher.rb"
    Spork.trap_class_method(::ActionController::Dispatcher, :define_dispatcher_callbacks) if ActionController::Dispatcher.respond_to?(:define_dispatcher_callbacks)
  end
end

#delay_route_loadingObject



73
74
75
76
77
# File 'lib/spork/app_framework/rails.rb', line 73

def delay_route_loading
  if ::Rails::Initializer.instance_methods.include?('initialize_routing')
    Spork.trap_method(::Rails::Initializer, :initialize_routing)
  end
end

#install_hooksObject



21
22
23
24
25
26
27
# File 'lib/spork/app_framework/rails.rb', line 21

def install_hooks
  auto_reestablish_db_connection
  delay_observer_loading
  delay_app_preload
  delay_application_controller_loading
  delay_route_loading
end

#load_environment_with_sporkObject



14
15
16
17
18
19
# File 'lib/spork/app_framework/rails.rb', line 14

def load_environment_with_spork
  reset_rails_env
  result = load_environment_without_spork
  install_hooks
  result
end

#reset_rails_envObject



29
30
31
32
33
# File 'lib/spork/app_framework/rails.rb', line 29

def reset_rails_env
  return unless ENV['RAILS_ENV']
  Object.send(:remove_const, :RAILS_ENV)
  Object.const_set(:RAILS_ENV, ENV['RAILS_ENV'].dup)
end