Class: Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_helper.rb

Defined Under Namespace

Classes: Engine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Helper

Returns a new instance of Helper.



60
61
62
# File 'lib/rails_helper.rb', line 60

def initialize(*args, &block)
  :noop
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



57
58
59
# File 'lib/rails_helper.rb', line 57

def controller
  @controller
end

Class Method Details

.current_controllerObject



118
119
120
# File 'lib/rails_helper.rb', line 118

def Helper.current_controller
  Current.controller
end

.dependenciesObject



6
7
8
9
10
11
# File 'lib/rails_helper.rb', line 6

def Helper.dependencies
  {
    'rails_current'             => [ 'rails_current',             ' >= 1.0' ],
    'rails_default_url_options' => [ 'rails_default_url_options', ' >= 1.0' ]
  }
end

.helpers=(*args) ⇒ Object



64
65
66
# File 'lib/rails_helper.rb', line 64

def Helper.helpers=(*args)
  :noop
end

.mock_controllerObject

see ./actionpack/test/controller/caching_test.rb OUCH!



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rails_helper.rb', line 71

def Helper.mock_controller
  require 'rails'
  require 'action_controller'
  require 'action_dispatch/testing/test_request.rb'
  require 'action_dispatch/testing/test_response.rb'

  store = ActiveSupport::Cache::MemoryStore.new

  controller = mock_controller_class.new
  controller.perform_caching = true
  controller.cache_store = store

  request = ActionDispatch::TestRequest.new
  response = ActionDispatch::TestResponse.new

  controller.request = request
  controller.response = response

  singleton_class =
    class << controller
      self
    end

  singleton_class.module_eval do
    define_method(:default_url_options) do
      @default_url_options ||= (
        defined?(DefaultUrlOptions) ? DefaultUrlOptions.dup : {}
      )
    end
  end

  controller
end

.mock_controller_classObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_helper.rb', line 105

def Helper.mock_controller_class
  unless const_defined?(:Controller)
    controller_class =
      if defined?(::ApplicationController)
        Class.new(::ApplicationController)
      else
        Class.new(::ActionController::Base)
      end
    const_set(:Controller, controller_class)
  end
  return const_get(:Controller)
end

.new(*args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_helper.rb', line 13

def Helper.new(*args, &block)
  options = args.extract_options!.to_options!
  controllers, args = args.partition{|arg| ActionController::Base === arg}
  controller = controllers.first || Helper.current_controller || Helper.mock_controller

  controller_class = controller.send(:class)

  helpers = args
  helpers.push(nil) if helpers.empty?

  helpers.flatten!
  helpers.uniq!

  modules = []

  helpers.each do |mod|
    case mod
      when NilClass, :all, 'all'
        controller_class.send(:all_application_helpers).each do |name|
          file_name  = name.to_s.underscore + '_helper'
          mod = file_name.camelize.constantize
          modules.push(mod)
        end
      when Module
        modules.push(mod)
      else
        raise(ArgumentError, mod.inspect)
    end
  end

  view_class =
    Class.new(self) do
      modules.each do |mod|
        include mod
      end
      self.helpers = helpers
    end

  view_class.allocate.tap do |helper|
    helper.send(:initialize, context=nil, assigns={}, controller, formats=nil)
    helper
  end
end

.versionObject



2
3
4
# File 'lib/rails_helper.rb', line 2

def Helper.version
  '2.2.2'
end

Instance Method Details

#_routesObject



122
123
124
# File 'lib/rails_helper.rb', line 122

def _routes
  Rails.application.routes
end

#default_url_optionsObject



126
127
128
# File 'lib/rails_helper.rb', line 126

def default_url_options
  defined?(DefaultUrlOptions) ? DefaultUrlOptions : {}
end

#sessionObject



130
131
132
# File 'lib/rails_helper.rb', line 130

def session
  controller.session
end