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

Instance Method Summary collapse

Class Method Details

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

Extends the default behaviour to add our default inclusions if provided


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

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.


43
44
45
# File 'app/resources/api/v2/base_resource.rb', line 43

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

.inclusionsObject


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

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/resources/api/v2/base_resource.rb', line 65

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

Instance Method Details

#fetchable_fieldsObject

This extension allows the writeonly property to be used on attributes/relationships

This avoids the need to override fetchable_fields on

every resource.


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

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