Class: ActionProcessor::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/action_processor/base.rb', line 16

def initialize(params = {})
  @params = if params.class.name == 'Hash'
              params.with_indifferent_access
            else
              params
            end
  @errors = ActionProcessor::Errors.new
  @steps_stack = []
  @transaction_level = ActiveRecord::Base.connection.open_transactions
rescue ActiveRecord::ConnectionNotEstablished
  @transaction_level = nil
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/action_processor/base.rb', line 8

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/action_processor/base.rb', line 8

def params
  @params
end

Class Method Details

.run(params = {}) ⇒ Object



10
11
12
13
14
# File 'lib/action_processor/base.rb', line 10

def self.run(params = {})
  inst = new(params)
  inst.run
  inst
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/action_processor/base.rb', line 33

def errors?
  @errors.all.any?
end

#fail!(errs, attr = :not_specified) ⇒ Object

As an “errs” params we could pass several types:

  • String with error description

  • array of Strings (error messages)

  • ActiveRecord model (invalid) - its errors will be copied

Raises:

  • (ActiveRecord::Rollback)


76
77
78
79
80
81
82
83
84
# File 'lib/action_processor/base.rb', line 76

def fail!(errs, attr = :not_specified)
  if errs.class.ancestors.map(&:to_s).include?("ActiveRecord::Base")
    fail_active_record!(errs)
  else
    @errors.add(errs, @current_step, attr)
  end

  raise ActiveRecord::Rollback if in_transaction?
end

#failed_jsonObject

could be overriden to provide some specifics/advices/etc.



51
52
53
# File 'lib/action_processor/base.rb', line 51

def failed_json
  { success: false, errors: errors.grouped_by_attribute }
end

#json_outcomeObject



41
42
43
# File 'lib/action_processor/base.rb', line 41

def json_outcome
  errors? ? failed_json : successful_json
end

#runObject



29
30
31
# File 'lib/action_processor/base.rb', line 29

def run
  raise 'Error: method "run" should be overridden to implement business logic!'
end

#step(step_method, **options) ⇒ Object



55
56
57
58
59
# File 'lib/action_processor/base.rb', line 55

def step(step_method, **options)
  return if errors? # skip it if there are errors

  step_always(step_method, **options)
end

#step_always(step_method, **options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/action_processor/base.rb', line 61

def step_always(step_method, **options)
  @steps_stack << (@current_step || :not_specified)
  @current_step = step_method
  # performs even if there are errors
  # useful for: 
  #  - validation steps to return list of all errors
  #  - errors reporting and making decisions at the end of processing
  send step_method, **options
  @current_step = @steps_stack.pop
end

#success?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/action_processor/base.rb', line 37

def success?
  @errors.all.empty?
end

#successful_jsonObject

in most cases should be overriden to provide relevant data



46
47
48
# File 'lib/action_processor/base.rb', line 46

def successful_json
  { success: true }
end