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
26
27
28
# File 'lib/lhs/concerns/item/update.rb', line 23

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

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



30
31
32
# File 'lib/lhs/concerns/item/update.rb', line 30

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

#update(params, options = nil) ⇒ Object



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

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

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



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

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