Class: JSONAPI::ReplaceHasOneAssociationOperation

Inherits:
Operation
  • Object
show all
Defined in:
lib/jsonapi/operation.rb

Instance Attribute Summary collapse

Attributes inherited from Operation

#options, #resource_klass, #transactional

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, options = {}) ⇒ ReplaceHasOneAssociationOperation

Returns a new instance of ReplaceHasOneAssociationOperation.



226
227
228
229
230
231
# File 'lib/jsonapi/operation.rb', line 226

def initialize(resource_klass, options = {})
  @resource_id = options.fetch(:resource_id)
  @key_value = options.fetch(:key_value)
  @association_type = options.fetch(:association_type).to_sym
  super(resource_klass, options)
end

Instance Attribute Details

#association_typeObject (readonly)

Returns the value of attribute association_type.



224
225
226
# File 'lib/jsonapi/operation.rb', line 224

def association_type
  @association_type
end

#key_valueObject (readonly)

Returns the value of attribute key_value.



224
225
226
# File 'lib/jsonapi/operation.rb', line 224

def key_value
  @key_value
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



224
225
226
# File 'lib/jsonapi/operation.rb', line 224

def resource_id
  @resource_id
end

Instance Method Details

#applyObject



233
234
235
236
237
238
# File 'lib/jsonapi/operation.rb', line 233

def apply
  resource = @resource_klass.find_by_key(@resource_id, context: @context)
  result = resource.replace_has_one_link(@association_type, @key_value)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end