Class: JSONAPI::RelationshipsController

Inherits:
BaseController show all
Defined in:
lib/json_api/controllers/relationships_controller.rb

Instance Method Summary collapse

Methods included from ResourceActions

#build_resource_from_params, #create, #deserialize_params, #determine_sti_class_for_create, #determine_sti_resource_class_for_create, #index, #render_invalid_signed_id_error

Methods included from ActiveStorageSupport

#active_storage_attachment?, #append_only_enabled?, #attach_active_storage_files, #extract_active_storage_params_from_hash, #filter_active_storage_from_includes, #filter_polymorphic_from_includes, #find_blob_by_signed_id, #process_active_storage_attachment, #purge_on_nil_enabled?, #serialize_active_storage_relationship, #serialize_blob_identifier

Methods included from ControllerHelpers

#current_user

Instance Method Details

#destroyObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/json_api/controllers/relationships_controller.rb', line 51

def destroy
  authorize_resource_action!(@resource, action: :update, context: { relationship: @relationship_name })
  relationship_data = parse_relationship_data
  return head :no_content if relationship_data.nil?

  remove_relationship(relationship_data)

  # For to-many relationships, resources are updated directly in remove_from_many_relationship
  # For to-one relationships, the parent resource needs to be saved
  association = @resource.class.reflect_on_association(@relationship_name)
  if association && !association.collection?
    if @resource.save
      emit_relationship_event(:removed, relationship_data)
      head :no_content
    else
      render_validation_errors(@resource)
    end
  else
    emit_relationship_event(:removed, relationship_data)
    head :no_content
  end
rescue ArgumentError => e
  render_invalid_relationship_error(e)
rescue JSONAPI::Exceptions::ParameterNotAllowed => e
  render_parameter_not_allowed_error(e)
end

#showObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/json_api/controllers/relationships_controller.rb', line 18

def show
  authorize_resource_action!(@resource, action: :show, context: { relationship: @relationship_name })
  relationship_data = serialize_relationship_data
  links = serialize_relationship_links
  meta = serialize_relationship_meta

  response_data = { data: relationship_data, links: }
  response_data[:meta] = meta if meta.present?

  render json: response_data, status: :ok
end

#updateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/json_api/controllers/relationships_controller.rb', line 30

def update
  authorize_resource_action!(@resource, action: :update, context: { relationship: @relationship_name })
  relationship_data = parse_relationship_data
  update_relationship(relationship_data)

  if @resource.save
    emit_relationship_event(:updated, relationship_data)
    render json: {
      data: serialize_relationship_data,
      links: serialize_relationship_links,
      meta: serialize_relationship_meta
    }.compact, status: :ok
  else
    render_validation_errors(@resource)
  end
rescue ArgumentError => e
  render_invalid_relationship_error(e)
rescue JSONAPI::Exceptions::ParameterNotAllowed => e
  render_parameter_not_allowed_error(e)
end