Module: JSONAPI::Resources::AttributesDsl::FieldResolution

Defined in:
lib/json_api/resources/concerns/attributes_dsl.rb

Instance Method Summary collapse

Instance Method Details

#inherits_field?(ivar, method) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 66

def inherits_field?(ivar, method)
  superclass != JSONAPI::Resource &&
    superclass.respond_to?(method) &&
    superclass.instance_variable_defined?(ivar)
end

#permitted_attributesObject



29
30
31
32
33
34
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 29

def permitted_attributes
  declared_attributes = instance_variable_defined?(:@attributes)
  attrs = @attributes || []
  attrs = superclass.permitted_attributes + attrs if should_inherit_attributes?(declared_attributes)
  attrs.uniq
end

#permitted_creatable_fieldsObject



36
37
38
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 36

def permitted_creatable_fields
  resolve_field_list(:@creatable_fields, :permitted_creatable_fields)
end

#permitted_updatable_fieldsObject



40
41
42
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 40

def permitted_updatable_fields
  resolve_field_list(:@updatable_fields, :permitted_updatable_fields)
end

#relationship_field_writable?(name, action) ⇒ Boolean

Whether a relationship may be written for the given action (:create or :update). A relationship is gated by the creatable_fields/updatable_fields allow-list only when the resource opted it in by naming it in one of those lists; relationships named in neither keep the default (readonly-only) behavior. This lets a resource make a relationship create-only — name it in creatable_fields, omit it from updatable_fields — the same way it restricts attributes.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 50

def relationship_field_writable?(name, action)
  name = name.to_sym
  creatable = permitted_creatable_fields.map(&:to_sym)
  updatable = permitted_updatable_fields.map(&:to_sym)
  return true unless creatable.include?(name) || updatable.include?(name)

  (action == :create ? creatable : updatable).include?(name)
end

#resolve_field_list(ivar, method) ⇒ Object



59
60
61
62
63
64
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 59

def resolve_field_list(ivar, method)
  return (instance_variable_get(ivar) || []).uniq if instance_variable_defined?(ivar)
  return superclass.public_send(method).uniq if inherits_field?(ivar, method)

  permitted_attributes.uniq
end

#should_inherit_attributes?(declared_attributes) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/json_api/resources/concerns/attributes_dsl.rb', line 72

def should_inherit_attributes?(declared_attributes)
  !declared_attributes &&
    superclass != JSONAPI::Resource &&
    superclass.respond_to?(:permitted_attributes)
end