Module: AbstractController::Helpers::ClassMethods

Includes:
Resolution
Defined in:
lib/abstract_controller/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Resolution

#all_helpers_from_path, #helper_modules_from_paths, #modules_for_helpers

Instance Attribute Details

#_helpers=(value) ⇒ Object (writeonly)

Sets the attribute _helpers

Parameters:

  • value

    the value to set the attribute _helpers to.



92
93
94
# File 'lib/abstract_controller/helpers.rb', line 92

def _helpers=(value)
  @_helpers = value
end

Instance Method Details

#_helpers_for_modificationObject



231
232
233
234
235
236
# File 'lib/abstract_controller/helpers.rb', line 231

def _helpers_for_modification
  unless @_helpers
    self._helpers = define_helpers_module(self, superclass._helpers)
  end
  _helpers
end

#clear_helpersObject

Clears up all existing helpers in this class, only keeping the helper with the same name as this class.



222
223
224
225
226
227
228
229
# File 'lib/abstract_controller/helpers.rb', line 222

def clear_helpers
  inherited_helper_methods = _helper_methods
  self._helpers = Module.new
  self._helper_methods = Array.new

  inherited_helper_methods.each { |meth| helper_method meth }
  default_helper_module! unless anonymous?
end

#helper(*args, &block) ⇒ Object

Includes the given modules in the template class.

Modules can be specified in different ways. All of the following calls include FooHelper:

# Module, recommended.
helper FooHelper

# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo"
helper :Foo
helper "foo"
helper :foo

The last two assume that "foo".camelize returns “Foo”.

When strings or symbols are passed, the method finds the actual module object using String#constantize. Therefore, if the module has not been yet loaded, it has to be autoloadable, which is normally the case.

Namespaces are supported. The following calls include Foo::BarHelper:

# Module, recommended.
helper Foo::BarHelper

# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo::Bar"
helper :"Foo::Bar"
helper "foo/bar"
helper :"foo/bar"

The last two assume that "foo/bar".camelize returns “Foo::Bar”.

The method accepts a block too. If present, the block is evaluated in the context of the controller helper module. This simple call makes the wadus method available in templates of the enclosing controller:

helper do
  def wadus
    "wadus"
  end
end

Furthermore, all the above styles can be mixed together:

helper FooHelper, "woo", "bar/baz" do
  def wadus
    "wadus"
  end
end


211
212
213
214
215
216
217
218
# File 'lib/abstract_controller/helpers.rb', line 211

def helper(*args, &block)
  modules_for_helpers(args).each do |mod|
    next if _helpers.include?(mod)
    _helpers_for_modification.include(mod)
  end

  _helpers_for_modification.module_eval(&block) if block_given?
end

#helper_method(*methods) ⇒ Object

Declare a controller method as a helper. For example, the following makes the current_user and logged_in? controller methods available to the view:

class ApplicationController < ActionController::Base
  helper_method :current_user, :logged_in?

  private
    def current_user
      @current_user ||= User.find_by(id: session[:user])
    end

    def logged_in?
      current_user != nil
    end
end

In a view:

<% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>

Parameters

  • method[, method] - A name or names of a method on the controller to be made available on the view.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/abstract_controller/helpers.rb', line 140

def helper_method(*methods)
  methods.flatten!
  self._helper_methods += methods

  location = caller_locations(1, 1).first
  file, line = location.path, location.lineno

  methods.each do |method|
    # def current_user(*args, &block)
    #   controller.send(:'current_user', *args, &block)
    # end
    _helpers_for_modification.class_eval <<~ruby_eval.lines.map(&:strip).join(";"), file, line
      def #{method}(*args, &block)
        controller.send(:'#{method}', *args, &block)
      end
      ruby2_keywords(:'#{method}')
    ruby_eval
  end
end

#inherited(klass) ⇒ Object

When a class is inherited, wrap its helper module in a new module. This ensures that the parent class’s module can be changed independently of the child class’s.



84
85
86
87
88
89
90
# File 'lib/abstract_controller/helpers.rb', line 84

def inherited(klass)
  # Inherited from parent by default
  klass._helpers = nil

  klass.class_eval { default_helper_module! } unless klass.anonymous?
  super
end