lib/rails_helper.rb in rails_helper-1.1.0 vs lib/rails_helper.rb in rails_helper-1.2.0

- old
+ new

@@ -1,101 +1,89 @@ -class Helper - def Helper.version() '1.1.0' end +class Helper < (defined?(ActionView::Base) ? ActionView::Base : Object) + def Helper.version + '1.2.0' + end def Helper.dependencies { - 'rails_current' => [ 'rails_current', ' >= 1.0' ] + 'rails_current' => [ 'rails_current', ' >= 1.0' ], + 'rails_default_url_options' => [ 'rails_default_url_options', ' >= 1.0' ] } end - if defined?(Rails::Engine) + attr_accessor 'options' + attr_accessor 'args' + attr_accessor 'block' + attr_accessor 'controller' + attr_accessor 'modules' - class Engine < Rails::Engine - config.after_initialize do - if defined?(ActionView::Helpers) - ::Helper.send(:include, ActionView::Helpers) - end + def initialize(*args, &block) + @options = args.extract_options!.to_options! + @args = args + @block = block - if defined?(Rails.application.routes.url_helpers) - ::Helper.send(:include, Rails.application.routes.url_helpers) - end + controllers, args = args.partition{|arg| ActionController::Base === arg} + + @controller = controllers.first || Helper.current_controller || Helper.mock_controller + + @modules = args + + @controller = controller + @modules.push(nil) if @modules.empty? + @modules.flatten.uniq.each do |mod| + case mod + when NilClass, :all, 'all' + extend ::ActionView::Helpers + when Module + extend mod + else + raise ArgumentError, mod.class.name end end + end + ### see ./actionpack/test/controller/caching_test.rb OUCH! + def Helper.mock_controller + require 'action_dispatch/testing/test_request.rb' + require 'action_dispatch/testing/test_response.rb' + store = ActiveSupport::Cache::MemoryStore.new + controller = ApplicationController.new + controller.perform_caching = true + controller.cache_store = store + request = ActionDispatch::TestRequest.new + response = ActionDispatch::TestResponse.new + controller.request = request + controller.response = response + controller.send(:initialize_template_class, response) + controller.send(:assign_shortcuts, request, response) + controller.send(:default_url_options).merge!(DefaultUrlOptions) if defined?(DefaultUrlOptions) + controller end - def controller - @controller ||= (Helper.current_controller || Helper.mock_controller) - @controller + def Helper.current_controller + Current.controller end - def controller=(controller) - @controller = controller - ensure - default_url_options[:protocol] = @controller.request.protocol - default_url_options[:host] = @controller.request.host - default_url_options[:port] = @controller.request.port + def _routes + Rails.application.routes end - def set_controller(controller) - self.controller = controller + def default_url_options + defined?(DefaultUrlOptions) ? DefaultUrlOptions : {} end - controller_delegates = %w( - render - render_to_string - ) - - controller_delegates.each do |method| - module_eval <<-__, __FILE__, __LINE__ - def #{ method }(*args, &block) - controller.#{ method }(*args, &block) - end - __ + def session + controller.session end - class << Helper - def mock_controller - ensure_rails_application do - require 'action_dispatch/testing/test_request.rb' - require 'action_dispatch/testing/test_response.rb' - store = ActiveSupport::Cache::MemoryStore.new - controller = - begin - ApplicationController.new - rescue NameError - ActionController::Base.new - end - controller.perform_caching = true - controller.cache_store = store - request = ActionDispatch::TestRequest.new - response = ActionDispatch::TestResponse.new - controller.request = request - controller.response = response - controller.send(:initialize_template_class, response) - controller.send(:assign_shortcuts, request, response) - controller.send(:default_url_options).merge!(DefaultUrlOptions) if defined?(DefaultUrlOptions) - controller - end - end - - def ensure_rails_application(&block) - if Rails.application.nil? - mock = Class.new(Rails::Application) - Rails.application = mock.instance - begin - block.call() - ensure - Rails.application = nil + if defined?(::Rails) + if defined?(Rails::Engine) + class Engine < Rails::Engine + config.after_initialize do + Helper.send(:include, Rails.application.routes.url_helpers) end - else - block.call() end - end - - def current_controller - Current.controller end end end Rails_helper = Helper