Class: ProtectedRecord::UseCase::Update

Inherits:
Object
  • Object
show all
Includes:
PayDirt::UseCase
Defined in:
lib/protected_record/use_case/update.rb

Direct Known Subclasses

ProtectedRecord::Update

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Update

Returns a new instance of Update.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/protected_record/use_case/update.rb', line 5

def initialize(options)
  # Defaults
  options = {
    change_request: UseCase::ChangeRequest::Create,
    change_filter:  UseCase::ChangeFilter::Create,
    change_log:     UseCase::ChangeLog::Create
  }.merge!(options)

  load_options(:params, :protected_record, :change_request, :change_log, :change_filter, :user, options)
  validate_state
end

Instance Method Details

#execute!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/protected_record/use_case/update.rb', line 17

def execute!
  form_change_request

  # We are successful if all changes have been applied
  if !@protected_record.changes.present?
    return PayDirt::Result.new({
      data:    {
        updated: @protected_record,
        change_request: @change_request_record
      },
      success:   true
    })
  else
    return PayDirt::Result.new({
      data:    { failed: @protected_record.changes },
      success:   false
    })
  end
end