Class: Batch::RequestFailAndRemover

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/batch/request_fail_and_remover.rb

Overview

Handles failure of requests or their removal from the batch via BatchesController#fail_items

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentObject

Returns the value of attribute comment


8
9
10
# File 'app/models/batch/request_fail_and_remover.rb', line 8

def comment
  @comment
end

#reasonObject

Returns the value of attribute reason


8
9
10
# File 'app/models/batch/request_fail_and_remover.rb', line 8

def reason
  @reason
end

Instance Method Details

#failure=(failure_hash) ⇒ Object


35
36
37
38
39
# File 'app/models/batch/request_fail_and_remover.rb', line 35

def failure=(failure_hash)
  @reason = failure_hash[:reason]
  @comment = failure_hash[:comment]
  @fail_but_charge = failure_hash[:fail_but_charge]
end

#id=(batch_id) ⇒ Object

ID is the batch id passed in from the controller


42
43
44
# File 'app/models/batch/request_fail_and_remover.rb', line 42

def id=(batch_id)
  @batch = Batch.find(batch_id)
end

#noticeObject


31
32
33
# File 'app/models/batch/request_fail_and_remover.rb', line 31

def notice
  @notice ||= []
end

#requested_fail=(selected_fields) ⇒ Object

This input comes in via the controller. It represents a series of checkboxes which each have the value 'on' when checked. We filter out 'control' which is used in place of the request id for some older batches. We convert it to an array of the request_ids we care about


50
51
52
# File 'app/models/batch/request_fail_and_remover.rb', line 50

def requested_fail=(selected_fields)
  @requested_fail = selected_fields.except('control').select { |_k, v| v == 'on' }.keys
end

#requested_remove=(selected_fields) ⇒ Object

This input comes in via the controller. It represents a series of checkboxes which each have the value 'on' when checked. We filter out 'control' which is used in place of the request id for some older batches. We convert it to an array of the request_ids we care about


58
59
60
# File 'app/models/batch/request_fail_and_remover.rb', line 58

def requested_remove=(selected_fields)
  @requested_remove = selected_fields.except('control').select { |_k, v| v == 'on' }.keys
end

#saveObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/batch/request_fail_and_remover.rb', line 16

def save
  return false unless valid?

  ActiveRecord::Base.transaction do
    fail_requests if requested_fail.present?
    remove_requests if requested_remove.present?
  end

  true
rescue ActiveRecord::RecordNotFound => e
  # Make the returned error a little more user friendly by stripping out the SQL
  errors.add(:base, e.message.gsub(/\[[^\[]*\] /, ''))
  false
end