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.



128
129
130
131
132
133
# File 'lib/jsonapi/operation.rb', line 128

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.



126
127
128
# File 'lib/jsonapi/operation.rb', line 126

def associated_key
  @associated_key
end

#association_typeObject (readonly)

Returns the value of attribute association_type.



126
127
128
# File 'lib/jsonapi/operation.rb', line 126

def association_type
  @association_type
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



126
127
128
# File 'lib/jsonapi/operation.rb', line 126

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/jsonapi/operation.rb', line 135

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