Class: JSONAPI::CreateHasManyAssociationOperation

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_values) ⇒ CreateHasManyAssociationOperation

Returns a new instance of CreateHasManyAssociationOperation.



114
115
116
117
118
119
# File 'lib/jsonapi/operation.rb', line 114

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

Instance Attribute Details

#association_typeObject (readonly)

Returns the value of attribute association_type.



112
113
114
# File 'lib/jsonapi/operation.rb', line 112

def association_type
  @association_type
end

#key_valuesObject (readonly)

Returns the value of attribute key_values.



112
113
114
# File 'lib/jsonapi/operation.rb', line 112

def key_values
  @key_values
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



112
113
114
# File 'lib/jsonapi/operation.rb', line 112

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/jsonapi/operation.rb', line 121

def apply(context)
  resource = @resource_klass.find_by_key(@resource_id, context: context)
  @key_values.each do |value|
    resource.create_has_many_link(@association_type, value)
  end

  return JSONAPI::OperationResult.new(:no_content)
end