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.



98
99
100
101
102
103
# File 'lib/jsonapi/operation.rb', line 98

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

Instance Attribute Details

#association_typeObject (readonly)

Returns the value of attribute association_type.



96
97
98
# File 'lib/jsonapi/operation.rb', line 96

def association_type
  @association_type
end

#key_valuesObject (readonly)

Returns the value of attribute key_values.



96
97
98
# File 'lib/jsonapi/operation.rb', line 96

def key_values
  @key_values
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



96
97
98
# File 'lib/jsonapi/operation.rb', line 96

def resource_id
  @resource_id
end

Instance Method Details

#apply(context) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/jsonapi/operation.rb', line 105

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

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