Class: JSONAPI::RemoveHasManyAssociationOperation

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, associated_key) ⇒ RemoveHasManyAssociationOperation

Returns a new instance of RemoveHasManyAssociationOperation.



146
147
148
149
150
151
# File 'lib/jsonapi/operation.rb', line 146

def initialize(resource_klass, resource_id, association_type, associated_key)
  @resource_id = resource_id
  @associated_key = associated_key
  @association_type = association_type.to_sym
  super(resource_klass)
end

Instance Attribute Details

#associated_keyObject (readonly)

Returns the value of attribute associated_key.



144
145
146
# File 'lib/jsonapi/operation.rb', line 144

def associated_key
  @associated_key
end

#association_typeObject (readonly)

Returns the value of attribute association_type.



144
145
146
# File 'lib/jsonapi/operation.rb', line 144

def association_type
  @association_type
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



144
145
146
# File 'lib/jsonapi/operation.rb', line 144

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/jsonapi/operation.rb', line 153

def apply(context)
  resource = @resource_klass.find_by_key(@resource_id, context: context)
  resource.remove_has_many_link(@association_type, @associated_key)

  return JSONAPI::OperationResult.new(:no_content)

rescue ActiveRecord::RecordNotFound => e
  raise JSONAPI::Exceptions::RecordNotFound.new(@associated_key)
end