Class: JSONAPI::ReplaceHasManyAssociationOperation

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) ⇒ ReplaceHasManyAssociationOperation

Returns a new instance of ReplaceHasManyAssociationOperation.



260
261
262
263
264
265
# File 'lib/jsonapi/operation.rb', line 260

def initialize(resource_klass, options)
  @resource_id = options.fetch(:resource_id)
  @data = options.fetch(:data)
  @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.



258
259
260
# File 'lib/jsonapi/operation.rb', line 258

def association_type
  @association_type
end

#dataObject (readonly)

Returns the value of attribute data.



258
259
260
# File 'lib/jsonapi/operation.rb', line 258

def data
  @data
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



258
259
260
# File 'lib/jsonapi/operation.rb', line 258

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



267
268
269
270
271
272
# File 'lib/jsonapi/operation.rb', line 267

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

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