Sha256: 5097b2d911c3bc50c5e3a1601151d4958c35ca35c39f2b21010863702ae4d650

Contents?: true

Size: 1.41 KB

Versions: 35

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'active_support'

class LHS::Item < LHS::Proxy
  autoload :EndpointLookup,
    'lhs/concerns/item/endpoint_lookup'

  module Update
    extend ActiveSupport::Concern

    included do
      include EndpointLookup
    end

    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 = 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
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
lhs-21.1.4 lib/lhs/concerns/item/update.rb
lhs-21.1.3 lib/lhs/concerns/item/update.rb
lhs-21.1.2 lib/lhs/concerns/item/update.rb
lhs-21.1.2.pre.keyfix.2 lib/lhs/concerns/item/update.rb
lhs-21.1.2.pre.keyfix.1 lib/lhs/concerns/item/update.rb
lhs-21.1.2.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/item/update.rb
lhs-21.1.1.pre.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/item/update.rb
lhs-21.1.0 lib/lhs/concerns/item/update.rb
lhs-21.0.0 lib/lhs/concerns/item/update.rb
lhs-20.1.4 lib/lhs/concerns/item/update.rb
lhs-20.1.3.pre.paginationfix.3 lib/lhs/concerns/item/update.rb
lhs-20.1.3.pre.paginationfix.2 lib/lhs/concerns/item/update.rb
lhs-20.1.3.pre.paginationfix.1 lib/lhs/concerns/item/update.rb
lhs-20.1.2 lib/lhs/concerns/item/update.rb
lhs-20.1.1 lib/lhs/concerns/item/update.rb
lhs-20.0.0 lib/lhs/concerns/item/update.rb
lhs-19.10.0 lib/lhs/concerns/item/update.rb
lhs-19.9.0 lib/lhs/concerns/item/update.rb
lhs-19.8.2 lib/lhs/concerns/item/update.rb
lhs-19.8.1 lib/lhs/concerns/item/update.rb