Class: Api::V2::BaseResource Abstract
- Inherits:
-
JSONAPI::Resource
- Object
- JSONAPI::Resource
- Api::V2::BaseResource
- Defined in:
- app/resources/api/v2/base_resource.rb
Overview
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.
Direct Known Subclasses
AliquotResource, AssetAuditResource, AssetResource, BaitLibraryLayoutResource, BarcodePrinterResource, BulkTransferResource, CommentResource, CustomMetadatumCollectionResource, LabwareResource, LaneResource, LotResource, LotTypeResource, OrderResource, PickListResource, PlateConversionResource, PlateCreationResource, PlatePurposeResource, PlateResource, PlateTemplateResource, PolyMetadatumResource, PooledPlateCreationResource, PreCapturePoolResource, PrimerPanelResource, ProjectResource, PurposeResource, QcAssayResource, QcFileResource, QcResultResource, QcableResource, RackedTubeResource, ReceptacleResource, RequestMetadataResource, RequestResource, RequestTypeResource, SampleManifestResource, SampleMetadataResource, SampleResource, SpecificTubeCreationResource, StateChangeResource, StudyResource, SubmissionPoolResource, SubmissionResource, SubmissionTemplateResource, TagGroupAdapterTypeResource, TagGroupResource, TagLayoutResource, TagLayoutTemplateResource, TagResource, TemplateResource, TransferRequestCollectionResource, TransferRequestResource, TransferResource, TransferTemplateResource, TubeFromTubeCreationResource, TubePurposeResource, TubeRackResource, TubeRackStatusResource, TubeResource, UserResource, VolumeUpdateResource, WellResource, WorkOrderResource
Class Method Summary collapse
-
.apply_includes(records, options = {}) ⇒ Object
Extends the default behaviour to add our default inclusions if provided.
-
.creatable_fields(context) ⇒ Object
These extensions allow the use of readonly, write_once and writeonly properties.
-
.default_includes(*inclusions) ⇒ Object
Eager load specified models by default.
- .inclusions ⇒ Object
-
.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.
- .updatable_fields(context) ⇒ Object
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, = {}) if @default_includes.present? super(records.preload(*inclusions), ) 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, | [:readonly] }.keys - _relationships.select { |_rel_key, rel| rel.[: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 |
.inclusions ⇒ Object
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, = {}) case model_includes when Array return model_includes.map { |value| resolve_relationship_names_to_relations(resource_klass, value, ) } 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, ) model_includes[relationship.relation_name()] = 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() => 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, | [:readonly] || [:write_once] }.keys - _relationships.select { |_rel_key, rel| rel.[:readonly] || rel.[:write_once] }.keys end |
Instance Method Details
#fetchable_fields ⇒ Object
45 46 47 48 |
# File 'app/resources/api/v2/base_resource.rb', line 45 def fetchable_fields super - self.class._attributes.select { |_attr, | [:writeonly] }.keys - self.class._relationships.select { |_rel_key, rel| rel.[:writeonly] }.keys end |