Sha256: 70c4d32acc3f0b53dd067004332e8d51c45a95c362531982dc7fefed19dd44bc

Contents?: true

Size: 976 Bytes

Versions: 102

Compression:

Stored size: 976 Bytes

Contents

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

102 entries across 102 versions & 1 rubygems

Version Path
lhs-16.1.5 lib/lhs/concerns/proxy/create.rb
lhs-16.1.4 lib/lhs/concerns/proxy/create.rb
lhs-16.1.3 lib/lhs/concerns/proxy/create.rb
lhs-16.1.2 lib/lhs/concerns/proxy/create.rb
lhs-16.1.1 lib/lhs/concerns/proxy/create.rb
lhs-16.1.0 lib/lhs/concerns/proxy/create.rb
lhs-16.0.1 lib/lhs/concerns/proxy/create.rb
lhs-16.0.0 lib/lhs/concerns/proxy/create.rb
lhs-15.7.0 lib/lhs/concerns/proxy/create.rb
lhs-15.6.1 lib/lhs/concerns/proxy/create.rb
lhs-15.6.0 lib/lhs/concerns/proxy/create.rb
lhs-15.5.1 lib/lhs/concerns/proxy/create.rb
lhs-15.5.0 lib/lhs/concerns/proxy/create.rb
lhs-15.4.1 lib/lhs/concerns/proxy/create.rb
lhs-15.4.0 lib/lhs/concerns/proxy/create.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/concerns/proxy/create.rb
lhs-15.3.3 lib/lhs/concerns/proxy/create.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/concerns/proxy/create.rb
lhs-15.3.2 lib/lhs/concerns/proxy/create.rb
lhs-15.3.1 lib/lhs/concerns/proxy/create.rb