Sha256: 2d14597389ada1a0a0fe9029f1af6a12b8234378308fce2dba35f0d5a2876b58

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'active_support'

class LHS::Item < LHS::Proxy

  module Update
    extend ActiveSupport::Concern

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

    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

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

    def update!(params, options = {}, partial_update = false)
      options ||= {}
      partial_record = _record.new(LHS::Data.new(params, _data.parent, _record))
      _data.merge_raw!(partial_record._data)
      data_sent = partial_update ? partial_record._data : _data
      url = href || record.find_endpoint(id: id).compile(id: id)
      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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lhs-18.0.3 lib/lhs/concerns/item/update.rb
lhs-18.0.2 lib/lhs/concerns/item/update.rb
lhs-18.0.1 lib/lhs/concerns/item/update.rb
lhs-18.0.0 lib/lhs/concerns/item/update.rb
lhs-17.0.1 lib/lhs/concerns/item/update.rb
lhs-17.0.1.pre1 lib/lhs/concerns/item/update.rb
lhs-18.0.0.pre.pre1 lib/lhs/concerns/item/update.rb
lhs-17.0.0 lib/lhs/concerns/item/update.rb
lhs-16.1.6 lib/lhs/concerns/item/update.rb