module DHS::Proxy::Create

Public Instance Methods

create(data = {}, options = nil) click to toggle source
# File lib/dhs/concerns/proxy/create.rb, line 10
def create(data = {}, options = nil)
  record_creation!(options) do
    record_from_link.create(
      data,
      options_for_creation(options)
    )
  end
end
create!(data = {}, options = nil) click to toggle source
# File lib/dhs/concerns/proxy/create.rb, line 19
def create!(data = {}, options = nil)
  record_creation!(options) do
    record_from_link.create!(
      data,
      options_for_creation(options)
    )
  end
end

Private Instance Methods

options_for_creation(options) click to toggle source
# File lib/dhs/concerns/proxy/create.rb, line 39
def options_for_creation(options)
  return options if params_from_link.blank?
  options = {} if options.blank?
  options.deep_merge(params: params_from_link)
end
record_creation!(options) { || ... } click to toggle source
# File lib/dhs/concerns/proxy/create.rb, line 30
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