Class: Wf::CaseCommand::CreateEntry

Inherits:
Object
  • Object
show all
Includes:
SimpleCommand
Defined in:
app/models/wf/case_command/create_entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, workitem, user, params) ⇒ CreateEntry

Returns a new instance of CreateEntry.



7
8
9
10
11
12
# File 'app/models/wf/case_command/create_entry.rb', line 7

def initialize(form, workitem, user, params)
  @form       = form
  @workitem   = workitem
  @params     = params
  @user       = user
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



6
7
8
# File 'app/models/wf/case_command/create_entry.rb', line 6

def form
  @form
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'app/models/wf/case_command/create_entry.rb', line 6

def params
  @params
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'app/models/wf/case_command/create_entry.rb', line 6

def user
  @user
end

#workitemObject (readonly)

Returns the value of attribute workitem.



6
7
8
# File 'app/models/wf/case_command/create_entry.rb', line 6

def workitem
  @workitem
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
# File 'app/models/wf/case_command/create_entry.rb', line 14

def call
  create_entry
rescue StandardError
  binding.pry
  puts $ERROR_INFO
  # TODO: more detail
  errors.add(:base, :failure)
end

#create_entryObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/wf/case_command/create_entry.rb', line 23

def create_entry
  Wf::ApplicationRecord.transaction do
    entry = form.entries.find_or_create_by!(user: user, workitem: workitem)
    params.each do |field_id, field_value|
      if field = entry.field_values.where(form: form, field_id: field_id).first
        field.update!(value: field_value)
      else
        entry.field_values.create!(form: form, field_id: field_id, value: field_value)
      end
    end
    entry.update_payload!
    entry
  end
end