Class: JSONAPI::ShowRelatedResourceOperation

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

Instance Attribute Summary collapse

Attributes inherited from Operation

#options, #resource_klass, #transactional

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, options = {}) ⇒ ShowRelatedResourceOperation

Returns a new instance of ShowRelatedResourceOperation.



118
119
120
121
122
123
124
# File 'lib/jsonapi/operation.rb', line 118

def initialize(resource_klass, options = {})
  @source_klass = options.fetch(:source_klass)
  @source_id = options.fetch(:source_id)
  @association_type = options.fetch(:association_type)
  @transactional = false
  super(resource_klass, options)
end

Instance Attribute Details

#association_typeObject (readonly)

Returns the value of attribute association_type.



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

def association_type
  @association_type
end

#source_idObject (readonly)

Returns the value of attribute source_id.



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

def source_id
  @source_id
end

#source_klassObject (readonly)

Returns the value of attribute source_klass.



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

def source_klass
  @source_klass
end

Instance Method Details

#applyObject



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

def apply
  source_resource = @source_klass.find_by_key(@source_id, context: @context)

  related_resource = source_resource.send(@association_type)

  return JSONAPI::ResourceOperationResult.new(:ok, related_resource)

rescue JSONAPI::Exceptions::Error => e
  return JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors)
end