Class: Api::V2::BaseResource

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

Overview

Provides extensions to JSONAPI::Resource as well as global behaviour See: jsonapi-resources.com/ for JSONAPI::Resource documentation

Class Method Summary collapse

Class Method Details

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

Extends the default behaviour to add our default inclusions if provided


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

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

.default_includes(*inclusions) ⇒ Object

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


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

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

.inclusionsObject


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

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


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/resources/api/v2/base_resource.rb', line 57

def self.resolve_relationship_names_to_relations(resource_klass, model_includes, options = {})
  case model_includes
    when Array
      return model_includes.map do |value|
        resolve_relationship_names_to_relations(resource_klass, value, options)
      end
    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

This extension allows the readonly property to be used on attributes/relationships prior to the 0.10 upgrade. This avoids the need to override updatable_fields on every resource. Readonly does not work on attributes in 0.9 by default This can be removed as soon as we update to 0.10, which is currently only in alpha


28
29
30
31
# File 'app/resources/api/v2/base_resource.rb', line 28

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