Class: JSONAPI::ReplacePolymorphicToOneRelationshipOperation

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 = {}) ⇒ ReplacePolymorphicToOneRelationshipOperation

Returns a new instance of ReplacePolymorphicToOneRelationshipOperation.



242
243
244
245
246
247
248
# File 'lib/jsonapi/operation.rb', line 242

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

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



240
241
242
# File 'lib/jsonapi/operation.rb', line 240

def key_type
  @key_type
end

#key_valueObject (readonly)

Returns the value of attribute key_value.



240
241
242
# File 'lib/jsonapi/operation.rb', line 240

def key_value
  @key_value
end

#relationship_typeObject (readonly)

Returns the value of attribute relationship_type.



240
241
242
# File 'lib/jsonapi/operation.rb', line 240

def relationship_type
  @relationship_type
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



240
241
242
# File 'lib/jsonapi/operation.rb', line 240

def resource_id
  @resource_id
end

Instance Method Details

#applyObject



250
251
252
253
254
255
# File 'lib/jsonapi/operation.rb', line 250

def apply
  resource = @resource_klass.find_by_key(@resource_id, context: @context)
  result = resource.replace_polymorphic_to_one_link(@relationship_type, @key_value, @key_type)

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