Spork::AppFramework::Rails::NinjaPatcher

TODO - subclass this out to handle different versions of rails Also… this is the nastiest duck punch ever. Clean this up.

Public Class Methods

included(klass) click to toggle source
# File lib/spork/app_framework/rails.rb, line 6
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

    def self.run_with_spork(*args, &block) # it's all fun and games until someone gets an eye poked out
      if ENV['RAILS_ENV']
        Object.send(:remove_const, :RAILS_ENV)
        Object.const_set(:RAILS_ENV, ENV['RAILS_ENV'].dup)
      end
      run_without_spork(*args, &block)
    end

    class << self
      unless method_defined?(:run_without_spork)
        alias :run_without_spork :run
        alias :run :run_with_spork
      end
    end
  end
end
run(*args, &block) click to toggle source
Also aliased as: run_without_spork
Alias for: run_with_spork
run_with_spork(*args, &block) click to toggle source
# File lib/spork/app_framework/rails.rb, line 13
def self.run_with_spork(*args, &block) # it's all fun and games until someone gets an eye poked out
  if ENV['RAILS_ENV']
    Object.send(:remove_const, :RAILS_ENV)
    Object.const_set(:RAILS_ENV, ENV['RAILS_ENV'].dup)
  end
  run_without_spork(*args, &block)
end
Also aliased as: run
run_without_spork(*args, &block) click to toggle source
Alias for: run

Public Instance Methods

auto_reestablish_db_connection() click to toggle source
# File lib/spork/app_framework/rails.rb, line 81
def auto_reestablish_db_connection
  if Object.const_defined?(:ActiveRecord)
    Spork.each_run do
      # rails lib/test_help.rb 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_preload() click to toggle source
# File lib/spork/app_framework/rails.rb, line 61
def delay_app_preload
  if ::Rails::Initializer.instance_methods.map(&:to_sym).include?(:load_application_classes)
    Spork.trap_method(::Rails::Initializer, :load_application_classes)
  end
end
delay_application_controller_loading() click to toggle source
# File lib/spork/app_framework/rails.rb, line 67
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)
      # update the rails magic to refresh the module
      ApplicationController.send(:helper, ApplicationHelper)
    end
  end
end
delay_eager_view_loading() click to toggle source
# File lib/spork/app_framework/rails.rb, line 97
def delay_eager_view_loading
  # So, in testing mode it seems it would be optimal to not eager load
  # views (as your may only run a test that uses one or two views).
  # However, I decided to delay eager loading rather than force it to
  # disable because you may wish to eager load your views (I.E. you're
  # testing concurrency)

  # Rails 2.3.x +
  if defined?(::ActionView::Template::EagerPath)
    Spork.trap_method(::ActionView::Template::EagerPath, :load!)
  end
  # Rails 2.2.x
  if defined?(::ActionView::PathSet::Path)
    Spork.trap_method(::ActionView::PathSet::Path, :load)
  end
  # Rails 2.0.5 - 2.1.x don't appear to eager cache views.
end
delay_observer_loading() click to toggle source
# File lib/spork/app_framework/rails.rb, line 51
def delay_observer_loading
  if ::Rails::Initializer.instance_methods.map(&:to_sym).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_loading() click to toggle source
# File lib/spork/app_framework/rails.rb, line 91
def delay_route_loading
  if ::Rails::Initializer.instance_methods.map(&:to_sym).include?(:initialize_routing)
    Spork.trap_method(::Rails::Initializer, :initialize_routing)
  end
end
install_hooks() click to toggle source
# File lib/spork/app_framework/rails.rb, line 36
def install_hooks
  auto_reestablish_db_connection
  delay_observer_loading
  delay_app_preload
  delay_application_controller_loading
  delay_route_loading
  delay_eager_view_loading
end
load_environment_with_spork() click to toggle source
# File lib/spork/app_framework/rails.rb, line 30
def load_environment_with_spork
  result = load_environment_without_spork
  install_hooks
  result
end
reset_rails_env() click to toggle source
# File lib/spork/app_framework/rails.rb, line 45
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.