Class: SteelWheel::Handler

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Memery, Callbacks, Components, Composition, Filters, Preconditions, Shortcuts
Defined in:
lib/steel_wheel/handler.rb

Overview

Base class that defines main flow

Direct Known Subclasses

ApplicationHandler

Constant Summary

Constants included from Callbacks

Callbacks::NOOP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composition

included

Methods included from Callbacks

#callbacks, #failure, #success

Methods included from Shortcuts

#depends_on, #finder, included, #verify

Methods included from Components

included

Methods included from Filters

included

Constructor Details

#initialize(params = {}) ⇒ Handler



30
31
32
33
# File 'lib/steel_wheel/handler.rb', line 30

def initialize(params = {})
  @http_status = :ok
  prepare_params(params)
end

Instance Attribute Details

#form_inputObject (readonly)

Returns the value of attribute form_input.



19
20
21
# File 'lib/steel_wheel/handler.rb', line 19

def form_input
  @form_input
end

#helpersObject

Returns the value of attribute helpers.



20
21
22
# File 'lib/steel_wheel/handler.rb', line 20

def helpers
  @helpers
end

#http_statusObject

Returns the value of attribute http_status.



20
21
22
# File 'lib/steel_wheel/handler.rb', line 20

def http_status
  @http_status
end

#inputObject (readonly)

Returns the value of attribute input.



19
20
21
# File 'lib/steel_wheel/handler.rb', line 19

def input
  @input
end

Class Method Details

.generic_validation_keys(*keys) ⇒ Object



23
24
25
# File 'lib/steel_wheel/handler.rb', line 23

def self.generic_validation_keys(*keys)
  include SteelWheel::SkipActiveModelErrorsKeys[*keys]
end

.handle(input = {}, &block) ⇒ Object



36
37
38
# File 'lib/steel_wheel/handler.rb', line 36

def handle(input = {}, &block)
  new(input).handle(&block)
end

.inherited(subclass) ⇒ Object



44
45
46
47
48
# File 'lib/steel_wheel/handler.rb', line 44

def inherited(subclass)
  super
  subclass.url_params(url_params_definition)
  subclass.form(form_definition)
end

.nameObject



40
41
42
# File 'lib/steel_wheel/handler.rb', line 40

def name
  to_s.match?(/Class/) ? 'SteelWheel::Handler' : to_s
end

Instance Method Details

#callObject



51
52
53
# File 'lib/steel_wheel/handler.rb', line 51

def call
  raise SteelWheel::ActionNotImplementedError, 'Subclass must implement #call'
end

#current_actionObject



80
81
82
# File 'lib/steel_wheel/handler.rb', line 80

def current_action
  ActiveSupport::StringInquirer.new(url_params.action)
end

#handle {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
59
60
61
62
63
# File 'lib/steel_wheel/handler.rb', line 55

def handle(&block)
  yield(self) if block
  validate_preconditions
  return self unless success?

  on_validation_success
  errors.empty? ? success_callback : failure_self
  self
end

#on_validation_successObject



65
66
67
# File 'lib/steel_wheel/handler.rb', line 65

def on_validation_success
  # NOOP
end

#statusObject



73
74
75
76
77
78
# File 'lib/steel_wheel/handler.rb', line 73

def status
  return :ok if errors.empty?
  return errors.keys.first unless defined?(ActiveModel::Error)

  errors.map(&:type).first
end

#success?Boolean



69
70
71
# File 'lib/steel_wheel/handler.rb', line 69

def success?
  http_status == :ok
end