Module: JSONAPI::RelationshipGuard

Defined in:
lib/json_api/support/relationship_guard.rb

Class Method Summary collapse

Class Method Details

.ensure_writable!(association, error_target, readonly: false, field_writable: true) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/json_api/support/relationship_guard.rb', line 9

def ensure_writable!(association, error_target, readonly: false, field_writable: true)
  # A relationship excluded from the resource's creatable_fields/updatable_fields
  # allow-list is not writable for this action, on either the main-body path or the
  # dedicated endpoint. This gate is independent of the AR reflection.
  raise JSONAPI::AuthorizationError, "Relationship '#{error_target}' is not writable" unless field_writable

  return unless association
  return unless readonly

  raise JSONAPI::Errors::ParameterNotAllowed, [error_target]
end