Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y24-213 - Removed default_includes from v2 API Resources #4635

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/resources/api/v2/aliquot_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module V2
# or look at the [JSONAPI::Resources](http://jsonapi-resources.com/) package for Sequencescape's implementation
# of the JSON:API standard.
class AliquotResource < BaseResource
default_includes :tag, :tag2

# Associations
has_one :study
has_one :project
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/asset_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ module V2
class AssetResource < BaseResource
attributes :uuid

default_includes :uuid_object

has_one :custom_metadatum_collection
has_many :comments, readonly: true

Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/barcode_printer_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class BarcodePrinterResource < BaseResource
immutable

default_includes :uuid_object

###
# Attributes
###
Expand Down
54 changes: 0 additions & 54 deletions app/resources/api/v2/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,60 +47,6 @@ 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

# Eager load specified models by default. Useful when attributes are
# dependent on an associated model.
def self.default_includes(*inclusions)
@default_includes = inclusions.freeze
end

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

# Extends the default behaviour to add our default inclusions if provided
def self.apply_includes(records, options = {})
if @default_includes.present?
super(records.preload(*inclusions), options)
else
super
end
end

# 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
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
# rubocop:enable all
end
end
end
2 changes: 0 additions & 2 deletions app/resources/api/v2/custom_metadatum_collection_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module V2
# or look at the [JSONAPI::Resources](http://jsonapi-resources.com/) package for Sequencescape's implementation
# of the JSON:API standard.
class CustomMetadatumCollectionResource < BaseResource
default_includes :uuid_object, :custom_metadata

###
# Attributes
###
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/labware_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class LabwareResource < BaseResource
# is automatically available on plate and tube.
include Api::V2::SharedBehaviour::Labware

default_includes :uuid_object, :barcodes

# Custom methods
# These shouldn't be used for business logic, and are more about
# I/O and isolating implementation details.
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/lot_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class LotResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:
has_one :lot_type
has_one :user
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/lot_type_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class LotTypeResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:
has_one :target_purpose, write_once: true, class_name: 'Purpose'

Expand Down
3 changes: 0 additions & 3 deletions app/resources/api/v2/plate_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class PlateResource < BaseResource
# labware
include Api::V2::SharedBehaviour::Labware

# TODO: {Y24-213} Possibly this line doesn't actually do anything and could be removed.
default_includes :uuid_object, :barcodes, :plate_purpose, :transfer_requests

###
# Attributes
###
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/plate_template_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class PlateTemplateResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/pre_capture_pool_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class PreCapturePoolResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/project_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class ProjectResource < BaseResource
immutable

default_includes :uuid_object

attribute :name
attribute :cost_code, delegate: :project_cost_code
attribute :uuid, readonly: true
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/purpose_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class PurposeResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/qcable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class QcableResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object, :barcodes

# Associations:
has_one :lot
has_one :asset, polymorphic: true
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/receptacle_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class ReceptacleResource < BaseResource
# attributes and filters. By adding behaviour here we ensure that it
# is automatically available on well.
include Api::V2::SharedBehaviour::Receptacle

default_includes :uuid_object
end
end
end
6 changes: 0 additions & 6 deletions app/resources/api/v2/request_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module V2
# or look at the [JSONAPI::Resources](http://jsonapi-resources.com/) package for Sequencescape's implementation
# of the JSON:API standard.
class RequestResource < BaseResource
default_includes :uuid_object,
{ request_metadata: %i[bait_library primer_panel] },
:pooled_request,
:order_role,
:submission

# Attributes
attribute :uuid, readonly: true
attribute :role, write_once: true
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/request_type_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class RequestTypeResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/sample_manifest_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class SampleManifestResource < BaseResource
immutable

default_includes :uuid_object

# Name of the supplier of the sample manifest
attribute :supplier_name
end
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/sample_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module V2
# or look at the [JSONAPI::Resources](http://jsonapi-resources.com/) package for Sequencescape's implementation
# of the JSON:API standard.
class SampleResource < BaseResource
default_includes :uuid_object

has_one :sample_metadata, class_name: 'SampleMetadata', foreign_key_on: :related
has_one :sample_manifest

Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/submission_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class SubmissionResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object, :sequencing_requests

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/submission_template_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class SubmissionTemplateResource < BaseResource
immutable

default_includes :uuid_object

###
# Attributes
###
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/tag_group_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class TagGroupResource < BaseResource

# model_name / model_hint if required

default_includes :uuid_object, :tags

# Associations:
has_one :tag_group_adapter_type,
foreign_key: :adapter_type_id,
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/transfer_request_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class TransferRequestResource < BaseResource
immutable

default_includes :uuid_object

# Attributes
attribute :uuid, readonly: true
attribute :state, readonly: true
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/transfer_template_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module V2
class TransferTemplateResource < BaseResource
immutable

default_includes :uuid_object

###
# Attributes
###
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/tube_rack_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class TubeRackResource < BaseResource
# Instead this should be done as part of adding authentication to
# the API in the security OKR.

default_includes :uuid_object, :barcodes

# Attributes
attribute :created_at, readonly: true
attribute :labware_barcode, write_once: true
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/tube_rack_status_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ module V2
class TubeRackStatusResource < BaseResource
# model_name / model_hint if required

default_includes :uuid_object

# Associations:

# Attributes
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/tube_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class TubeResource < BaseResource

immutable

default_includes :uuid_object, :barcodes, :transfer_requests_as_target

###
# Attributes
###
Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/well_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ module V2
class WellResource < BaseResource
include Api::V2::SharedBehaviour::Receptacle

default_includes :uuid_object, :map, :transfer_requests_as_target, plate: :barcodes

# Attributes
attribute :position, readonly: true

Expand Down
2 changes: 0 additions & 2 deletions app/resources/api/v2/work_order_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ module V2
class WorkOrderResource < BaseResource
IGNORED_METADATA_FIELDS = %w[id request_id created_at updated_at].freeze

default_includes [{ example_request: :request_metadata }, :work_order_type]

# Attributes
attribute :at_risk
attribute :options, readonly: true
Expand Down
Loading