Sha256: 612782136b1fb5f70e91b00e47a6d34800b571be76b4b93e5857b9318ee65fd0

Contents?: true

Size: 1.19 KB

Versions: 16

Compression:

Stored size: 1.19 KB

Contents

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_data = LHS::Data.new(params, _data.parent, record)
      _data.merge_raw!(partial_data)
      data_sent = partial_update ? partial_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.to_json,
          headers: { 'Content-Type' => 'application/json' }
        )
      )
      _data.merge_raw!(response_data.unwrap(:item_created_key))
      true
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
lhs-15.1.1 lib/lhs/concerns/item/update.rb
lhs-15.1.0 lib/lhs/concerns/item/update.rb
lhs-15.0.2 lib/lhs/concerns/item/update.rb
lhs-15.0.1 lib/lhs/concerns/item/update.rb
lhs-15.0.0 lib/lhs/concerns/item/update.rb
lhs-14.6.5 lib/lhs/concerns/item/update.rb
lhs-14.6.4 lib/lhs/concerns/item/update.rb
lhs-14.6.3 lib/lhs/concerns/item/update.rb
lhs-14.6.2 lib/lhs/concerns/item/update.rb
lhs-14.6.1 lib/lhs/concerns/item/update.rb
lhs-14.6.0 lib/lhs/concerns/item/update.rb
lhs-14.5.0 lib/lhs/concerns/item/update.rb
lhs-14.4.0 lib/lhs/concerns/item/update.rb
lhs-14.3.4 lib/lhs/concerns/item/update.rb
lhs-14.3.3 lib/lhs/concerns/item/update.rb
lhs-14.3.2 lib/lhs/concerns/item/update.rb