Class: Api::V2::BaseResource Abstract

Inherits:
JSONAPI::Resource
  • Object
show all
Defined in:
app/resources/api/v2/base_resource.rb

Overview

This class is abstract.
TODO:

This documentation does not yet include complete descriptions of methods and what this class offers to its sub-classes.

Provides a base class for JSON:API representations of ApplicationRecord sub-classes.

For more information about JSON:API see the JSON:API Specifications or look at the JSONAPI::Resources package for Sequencescape’s implementation of the JSON:API standard.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_includes(records, options = {}) ⇒ Object

Extends the default behaviour to add our default inclusions if provided



61
62
63
64
65
66
67
# File 'app/resources/api/v2/base_resource.rb', line 61

def self.apply_includes(records, options = {})
  if @default_includes.present?
    super(records.preload(*inclusions), options)
  else
    super
  end
end

.creatable_fields(context) ⇒ Object

These extensions allow the use of readonly, write_once and writeonly properties. readonly - The attribute/relationship can be read but not written to. write_once - The attribute/relationship can be written to once on creation but not updated. writeonly - The attribute/relationship can be written to but not read. This avoids the need to override self.creatable_fields, self.updatable_fields and fetchable_fields on every resource. readonly does not work on attributes in JSONAPI:Resources 0.9 by default. This can be removed as soon as we update to 0.10, which is currently only in alpha



35
36
37
38
# File 'app/resources/api/v2/base_resource.rb', line 35

def self.creatable_fields(context)
  super - _attributes.select { |_attr, options| options[:readonly] }.keys -
    _relationships.select { |_rel_key, rel| rel.options[:readonly] }.keys
end

.default_includes(*inclusions) ⇒ Object

Eager load specified models by default. Useful when attributes are dependent on an associated model.



52
53
54
# File 'app/resources/api/v2/base_resource.rb', line 52

def self.default_includes(*inclusions)
  @default_includes = inclusions.freeze
end

.inclusionsObject



56
57
58
# File 'app/resources/api/v2/base_resource.rb', line 56

def self.inclusions
  @default_includes || [].freeze
end

.resolve_relationship_names_to_relations(resource_klass, model_includes, options = {}) ⇒ Object

The majority of this is lifted from JSONAPI::Resource We’ve had to modify the when Symbol chunk to handle nested includes We disable the cops for the shared section to avoid accidental drift due to auto-correct. rubocop:disable all



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/resources/api/v2/base_resource.rb', line 74

def self.resolve_relationship_names_to_relations(resource_klass, model_includes, options = {})
  case model_includes
  when Array
    return model_includes.map { |value| resolve_relationship_names_to_relations(resource_klass, value, options) }
  when Hash
    model_includes.keys.each do |key|
      relationship = resource_klass._relationships[key]
      value = model_includes[key]
      model_includes.delete(key)

      # MODIFICATION BEGINS
      included_relationships =
        resolve_relationship_names_to_relations(relationship.resource_klass, value, options)
      model_includes[relationship.relation_name(options)] = relationship.resource_klass.inclusions +
        included_relationships
      # MODIFICATION ENDS
    end
    return model_includes
  when Symbol
    relationship = resource_klass._relationships[model_includes]

    # MODIFICATION BEGINS
    # return relationship.relation_name(options)
    inclusions = relationship.resource_klass.inclusions
    { relationship.relation_name(options) => inclusions }
    # MODIFICATION ENDS
  end
end

.updatable_fields(context) ⇒ Object



40
41
42
43
# File 'app/resources/api/v2/base_resource.rb', line 40

def self.updatable_fields(context)
  super - _attributes.select { |_attr, options| options[:readonly] || options[:write_once] }.keys -
    _relationships.select { |_rel_key, rel| rel.options[:readonly] || rel.options[:write_once] }.keys
end

Instance Method Details

#fetchable_fieldsObject



45
46
47
48
# File 'app/resources/api/v2/base_resource.rb', line 45

def fetchable_fields
  super - self.class._attributes.select { |_attr, options| options[:writeonly] }.keys -
    self.class._relationships.select { |_rel_key, rel| rel.options[:writeonly] }.keys
end