Class: JSONAPI::ReplaceHasOneAssociationOperation

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

Instance Attribute Summary collapse

Attributes inherited from Operation

#resource_klass

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, resource_id, association_type, key_value) ⇒ ReplaceHasOneAssociationOperation

Returns a new instance of ReplaceHasOneAssociationOperation.



79
80
81
82
83
84
# File 'lib/jsonapi/operation.rb', line 79

def initialize(resource_klass, resource_id, association_type, key_value)
  @resource_id = resource_id
  @key_value = key_value
  @association_type = association_type
  super(resource_klass)
end

Instance Attribute Details

#association_typeObject (readonly)

Returns the value of attribute association_type.



77
78
79
# File 'lib/jsonapi/operation.rb', line 77

def association_type
  @association_type
end

#key_valueObject (readonly)

Returns the value of attribute key_value.



77
78
79
# File 'lib/jsonapi/operation.rb', line 77

def key_value
  @key_value
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



77
78
79
# File 'lib/jsonapi/operation.rb', line 77

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/jsonapi/operation.rb', line 86

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

  return JSONAPI::OperationResult.new(:created, resource)
end