Module: SteelWheel::RailsHelpers::InstanceMethods

Defined in:
lib/steel_wheel/railtie.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#compose_handler(handler_name, handler_params = params) ⇒ Object



34
35
36
37
38
39
# File 'lib/steel_wheel/railtie.rb', line 34

def compose_handler(handler_name, handler_params = params)
  handler_class_for(handler_name).new(handler_params).tap do |handler|
    handler.helpers = view_context
    handler.owner = self
  end
end

#failure_callbacks(handler) ⇒ Object



48
49
50
51
52
# File 'lib/steel_wheel/railtie.rb', line 48

def failure_callbacks(handler)
  handler.failure(:not_found) do
    render file: Rails.root.join('public', '404.html').to_s, status: handler.http_status
  end
end

#handler_class_for(handler) ⇒ Object



41
42
43
44
45
46
# File 'lib/steel_wheel/railtie.rb', line 41

def handler_class_for(handler)
  different_namespace = handler.to_s.split('/').size > 1
  return "#{handler.to_s.camelize}Handler".constantize if different_namespace

  "#{[params[:controller], handler].compact.join('/')}_handler".classify.constantize
end

#run_action(handler, composed_handlers, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/steel_wheel/railtie.rb', line 22

def run_action(handler, composed_handlers, &block)
  compose_handler(handler).handle do |primary_handler|
    if composed_handlers.empty?
      instance_exec(primary_handler, &block)
    else
      handlers = [primary_handler, *composed_handlers.map { |name| compose_handler(name) }]
      instance_exec(*handlers, &block)
    end
    failure_callbacks(primary_handler)
  end
end