class WarpEngine::AdditionalResource
A host-owned association shown on an engine admin page and, optionally, in the admin API: the hostβs own fields next to an engine record (SEO metadata, notes, translations) without the engine knowing what they are. Built by WarpEngine::Configuration::DSL#additional_resource and kept in WarpEngine.config.additional_resources.
Attributes
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
- subject
-
the engine resource the association hangs on, e.g.
:software - association
-
the association name on that resource
- label
-
the fieldset title on the admin form
- permitted
-
the attributes the admin form permits
- fields
-
the callable that renders the form fields
- multiple
-
truefor ahas_many,falsefor ahas_one api_schema-
the OpenAPI schema of one entry, or
nilto keep it out of the API api_serializer-
the callable that turns one record into its API Hash, or
nil
Public Class Methods
Source
# File lib/warp_engine/additional_resource.rb, line 105 def api_for(subject) self.for(subject).select(&:api?) end
Returns the resources on subject that appear in the API.
Source
# File lib/warp_engine/additional_resource.rb, line 100 def for(subject) Array(WarpEngine.config.additional_resources).select { |resource| resource.subject == subject.to_sym } end
Returns the configured resources hanging on subject.
# File lib/warp_engine/additional_resource.rb, line 20 def initialize(subject:, association:, label:, permitted:, fields:, multiple: true, api_schema: nil, api_serializer: nil) @subject = subject.to_sym @association = association.to_sym @label = label @permitted = Array(permitted) @fields = fields @multiple = multiple @api_schema = api_schema @api_serializer = api_serializer raise ArgumentError, "fields must be callable" unless fields.respond_to?(:call) raise ArgumentError, "api_serializer must be callable" if api_serializer && !api_serializer.respond_to?(:call) end
Creates the resource from the attributes of the same names. Raises ArgumentError when fields, or a given api_serializer, is not callable.
# File lib/warp_engine/additional_resource.rb, line 110 def permitted_params_for(subject) self.for(subject).map(&:permitted_params) end
Returns the strong parameters entries of every resource on subject.
Public Instance Methods
Source
# File lib/warp_engine/additional_resource.rb, line 51 def api? api_schema.present? end
Returns true when the resource has an API schema and so appears in the API.
Source
# File lib/warp_engine/additional_resource.rb, line 56 def api_attributes permitted.map(&:to_s) - [ "_destroy" ] end
Returns the permitted attribute names as strings, without _destroy.
Source
# File lib/warp_engine/additional_resource.rb, line 36 def attributes_key :"#{association}_attributes" end
Returns the nested attributes key, e.g. :seo_metadata_attributes.
Source
# File lib/warp_engine/additional_resource.rb, line 81 def nested_attributes(value) permitted_keys = permitted.map(&:to_s) clean = lambda do |item| raise ArgumentError, "#{association} expects an object per entry" unless item.is_a?(Hash) item.to_h.transform_keys { |key| key.to_s.underscore }.slice(*permitted_keys) end if multiple raise ArgumentError, "#{association} expects an array" unless value.is_a?(Array) value.map(&clean) else clean.call(value) end end
Source
# File lib/warp_engine/additional_resource.rb, line 41 def permitted_params { attributes_key => permitted } end
Returns the strong parameters entry: { attributes_key => permitted }.
Source
# File lib/warp_engine/additional_resource.rb, line 46 def render(form) fields.call(form) end
Renders the form fields by calling fields with the ActiveAdmin form.
Source
# File lib/warp_engine/additional_resource.rb, line 62 def serialize(record) return nil if record.nil? return api_serializer.call(record).as_json if api_serializer api_attributes.select { |name| record.respond_to?(name) } .to_h { |name| [ name.camelize(:lower), record.public_send(name) ] } .as_json end
Returns the JSON-ready Hash of record: the api_serializer result, or else its api_attributes under camelCase keys. Returns nil for nil.
Source
# File lib/warp_engine/additional_resource.rb, line 73 def value_for(owner) value = owner.public_send(association) multiple ? Array(value).map { |record| serialize(record) } : serialize(value) end
Returns the serialized association of the engine record owner: an Array of Hashes when multiple, else one Hash or nil.