Sha256: 9f685fa1540ee9f712e87e52bfaa0c8a8d1a04e60fcb2c5a1c972c01a211930c

Contents?: true

Size: 1007 Bytes

Versions: 12

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

require 'active_support'

class DHS::Proxy

  module Create
    extend ActiveSupport::Concern

    def create(data = {}, options = nil)
      record_creation!(options) do
        record_from_link.create(
          data,
          options_for_creation(options)
        )
      end
    end

    def create!(data = {}, options = nil)
      record_creation!(options) do
        record_from_link.create!(
          data,
          options_for_creation(options)
        )
      end
    end

    private

    def record_creation!(options)
      raise(ArgumentError, 'Record already exists') if _raw.keys != [:href] && item?

      record = yield
      # Needed to handle unexpanded collection which looks the same as item
      reload!(options) if record.errors.empty?
      record
    end

    def options_for_creation(options)
      return options if params_from_link.blank?
      options = {} if options.blank?
      options.deep_merge(params: params_from_link)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dhs-1.6.0 lib/dhs/concerns/proxy/create.rb
dhs-1.5.0 lib/dhs/concerns/proxy/create.rb
dhs-1.4.2 lib/dhs/concerns/proxy/create.rb
dhs-1.4.1 lib/dhs/concerns/proxy/create.rb
dhs-1.4.0 lib/dhs/concerns/proxy/create.rb
dhs-1.3.0 lib/dhs/concerns/proxy/create.rb
dhs-1.2.0 lib/dhs/concerns/proxy/create.rb
dhs-1.1.0 lib/dhs/concerns/proxy/create.rb
dhs-1.0.3 lib/dhs/concerns/proxy/create.rb
dhs-1.0.2 lib/dhs/concerns/proxy/create.rb
dhs-1.0.1 lib/dhs/concerns/proxy/create.rb
dhs-1.0.0 lib/dhs/concerns/proxy/create.rb