From 237135ef62db68448712d9065493ca73f5b86ef1 Mon Sep 17 00:00:00 2001 From: yoldas Date: Wed, 17 Jan 2024 23:44:27 +0000 Subject: [PATCH] Added resource to register new plate purposes via SS API v2 --- .../api/v2/plate_purpose_resource.rb | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/resources/api/v2/plate_purpose_resource.rb diff --git a/app/resources/api/v2/plate_purpose_resource.rb b/app/resources/api/v2/plate_purpose_resource.rb new file mode 100644 index 0000000000..33d28d3265 --- /dev/null +++ b/app/resources/api/v2/plate_purpose_resource.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +module Api + module V2 + # Provides a JSON API representation of Plate + # See: http://jsonapi-resources.com/ for JSONAPI::Resource documentation + class PlatePurposeResource < BaseResource + model_name 'PlatePurpose' + + # This resource is similar to PurposeResource but it was created to + # migrate the registration of plate purposes done by the Limber rake + # task config:generate from API version 1 to API version 2. + + # The following options are sent by Limber for a new plate purpose. + + # @!attribute name + # @return [String] the name of the plate purpose + attribute :name + + # @!attribute stock_plate + # @return [Boolean] whether the plates of this purpose are stock plates + attribute :stock_plate + + # @!attribute cherrypickable_target + # @return [Boolean] whether the plates of this purpose are cherrypickable + attribute :cherrypickable_target + + # @!attribute input_plate + # @return [Boolean] whether the plates of this purpose are input plates + attribute :input_plate + + # @!attribute size + # @return [Integer] the size of the plates of this purpose + attribute :size + + # @!attribute asset_shape + # @return [String] the name of the shape of the plates of this purpose + attribute :asset_shape + + # Sets the asset shape of the plate purpose by name if given. + # 'asset_shape' can be given via the Limber purpose configuration and + # defaults to 'Standard' if not provided. If the name is given and not + # found, an error is raised. Note that the name is case-sensitive. + # + # @param name [String] the name of the asset shape + # @return [void] + def asset_shape=(name) + @model.asset_shape = (AssetShape.find_by!(name: name) if name.present?) || AssetShape.default + end + + # Returns the name of the asset shape of the plate purpose. + # The asset_shape association is not utilized in Limber. This method + # returns the name of the asset shape associated with the plate purpose. + # + # @return [String] the name of the asset shape + def asset_shape + @model.asset_shape.name + end + + # Returns the input_plate attribute from the type of the plate purpose. + # This method is the counterpart to the model's attribute writer for + # input_plate. It performs the inverse operation, determining the value + # of input_plate attribute based on the model's type. + # + # @return [Boolean] whether the plate purpose is an input plate + def input_plate + @model.type == 'PlatePurpose::Input' + end + + # Prevents updating existing plate purposes. + # + # @param _context [JSONAPI::Resource::Context] not used + # @return [Array] empty array + def updatable_fields(_context) + [] + end + end + end +end