Module: LHS::Item::Update

Extended by:
ActiveSupport::Concern
Included in:
LHS::Item
Defined in:
lib/lhs/concerns/item/update.rb

Instance Method Summary collapse

Instance Method Details

#partial_update(params, options = nil) ⇒ Object



23
24
25
# File 'lib/lhs/concerns/item/update.rb', line 23

def partial_update(params, options = nil)
  update(params, options, true)
end

#partial_update!(params, options = nil) ⇒ Object



27
28
29
# File 'lib/lhs/concerns/item/update.rb', line 27

def partial_update!(params, options = nil)
  update!(params, options, true)
end

#update(params, options = nil, partial_update = false) ⇒ Object



16
17
18
19
20
21
# File 'lib/lhs/concerns/item/update.rb', line 16

def update(params, options = nil, partial_update = false)
  update!(params, options, partial_update)
rescue LHC::Error => e
  self.errors = LHS::Problems::Errors.new(e.response, record)
  false
end

#update!(params, options = {}, partial_update = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lhs/concerns/item/update.rb', line 31

def update!(params, options = {}, partial_update = false)
  options = options.present? ? options.dup : {}
  partial_record = _record.new(LHS::Data.new(params, _data.parent, _record))
  _data.merge_raw!(partial_record._data)
  data = _data._raw.dup
  partial_data = partial_record._data._raw.dup
  url = url_for_persistance!(data, options)
  data_sent = partial_update ? partial_data.extract!(*data.keys) : data
  response_data = record.request(
    options.merge(
      method: options.fetch(:method, :post),
      url: url,
      body: data_sent
    )
  )
  _data.merge_raw!(response_data.unwrap(:item_created_key))
  true
end