Sha256: d7feea7a13f834f11f73b4343110a900603f4e96e8a62fe7e098966f550dc23b

Contents?: true

Size: 1007 Bytes

Versions: 76

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

require 'active_support'

class LHS::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

76 entries across 76 versions & 1 rubygems

Version Path
lhs-26.2.0 lib/lhs/concerns/proxy/create.rb
lhs-26.1.0 lib/lhs/concerns/proxy/create.rb
lhs-26.0.1 lib/lhs/concerns/proxy/create.rb
lhs-26.0.0 lib/lhs/concerns/proxy/create.rb
lhs-25.2.0 lib/lhs/concerns/proxy/create.rb
lhs-25.1.0 lib/lhs/concerns/proxy/create.rb
lhs-25.0.4 lib/lhs/concerns/proxy/create.rb
lhs-25.0.3 lib/lhs/concerns/proxy/create.rb
lhs-25.0.2 lib/lhs/concerns/proxy/create.rb
lhs-25.0.1 lib/lhs/concerns/proxy/create.rb
lhs-25.0.0 lib/lhs/concerns/proxy/create.rb
lhs-24.1.2 lib/lhs/concerns/proxy/create.rb
lhs-24.1.1 lib/lhs/concerns/proxy/create.rb
lhs-24.1.0 lib/lhs/concerns/proxy/create.rb
lhs-24.1.0.pre.2 lib/lhs/concerns/proxy/create.rb
lhs-24.1.0.pre.1 lib/lhs/concerns/proxy/create.rb
lhs-24.0.0 lib/lhs/concerns/proxy/create.rb
lhs-23.0.2 lib/lhs/concerns/proxy/create.rb
lhs-23.0.1 lib/lhs/concerns/proxy/create.rb
lhs-23.0.0 lib/lhs/concerns/proxy/create.rb