Class: Spree::DigitalAssetProvider::Base

Inherits:
Object
  • Object
show all
Extended by:
ApiTyped
Defined in:
app/models/spree/digital_asset_provider/base.rb

Overview

Strategy that answers "how is this digital asset's deliverable produced?".

Stored as a class-name string in DigitalAsset#provider_type and constantized at call time; registered via Spree.digital_asset_providers. A blank provider_type resolves to File, the default — so an uploaded file is just the provider every asset already had.

Unlike delivery-rate or tax providers, a digital-asset provider does NOT resolve credentials through a Spree::Integration: it is host-app glue into internal software and owns its own configuration. The base gives it only the asset.

Direct Known Subclasses

File

Constant Summary collapse

SETTING_TYPES =
%i[string number boolean select].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiTyped

api_type, api_type_for, api_typed_family, class_name_for, find, registered_class

Constructor Details

#initialize(digital_asset) ⇒ Base

Returns a new instance of Base.

Parameters:



83
84
85
# File 'app/models/spree/digital_asset_provider/base.rb', line 83

def initialize(digital_asset)
  @digital_asset = digital_asset
end

Instance Attribute Details

#digital_asset ⇒ Object (readonly)

Returns the value of attribute digital_asset.



17
18
19
# File 'app/models/spree/digital_asset_provider/base.rb', line 17

def digital_asset
  @digital_asset
end

Class Method Details

.own_settings ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'app/models/spree/digital_asset_provider/base.rb', line 54

def own_settings
  @own_settings ||= {}
end

.provider_name ⇒ String

Human-readable name for admin UIs. Provider gems following the SpreeAcme::LicenseProvider convention get the outer module ("Acme"); a bare in-app class gets its own titleized name.

Returns:

  • (String)


63
64
65
66
67
68
69
70
# File 'app/models/spree/digital_asset_provider/base.rb', line 63

def provider_name
  leaf = name.demodulize
  outer = name.deconstantize.delete_prefix('Spree')

  return leaf.titleize if outer.blank? || !leaf.end_with?('Provider')

  outer.delete_prefix('::')
end

.requires_attachment? ⇒ Boolean

Whether an asset backed by this provider must carry an uploaded file. File needs one; a provider that resolves its deliverable elsewhere does not. Drives conditional validation and the admin create form.

Returns:

  • (Boolean)


77
78
79
# File 'app/models/spree/digital_asset_provider/base.rb', line 77

def requires_attachment?
  false
end

.setting(key, type = :string, default: nil, in: nil) ⇒ Object

Declares one per-asset configuration field the dashboard renders when a merchant adds an asset backed by this provider. Mirrors the preference shape used elsewhere, but the values live on the DigitalAsset (in metadata), not on the stateless provider instance.

Parameters:

  • key (Symbol)
  • type (Symbol) (defaults to: :string) —
  • default (Object) (defaults to: nil)
  • in (Array) (defaults to: nil) —

    allowed values for a :select field

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/digital_asset_provider/base.rb', line 31

def setting(key, type = :string, default: nil, in: nil)
  raise ArgumentError, "unknown setting type #{type}" unless SETTING_TYPES.include?(type)

  own_settings[key.to_sym] = {
    key: key.to_sym,
    type: type,
    default: default,
    in: binding.local_variable_get(:in)
  }.compact
end

.settings_schema ⇒ Array<Hash>

[{ key:, type:, default:, in? }] for every setting declared on this provider and its ancestors. Same wire shape the admin form renders from, so no provider ships dashboard code.

Returns:

  • (Array<Hash>)


47
48
49
50
51
# File 'app/models/spree/digital_asset_provider/base.rb', line 47

def settings_schema
  ancestors.reverse.each_with_object({}) do |ancestor, merged|
    merged.merge!(ancestor.own_settings) if ancestor.respond_to?(:own_settings)
  end.values
end

Instance Method Details

#deliver(digital_link, expires_in:) ⇒ Spree::DigitalDelivery

Produces the deliverable for one authorized download.

Parameters:

  • digital_link (Spree::DigitalLink) —

    the authorized grant

  • expires_in (ActiveSupport::Duration) —

    lifetime of any signed URL

Returns:

Raises:

  • (NotImplementedError)


92
93
94
# File 'app/models/spree/digital_asset_provider/base.rb', line 92

def deliver(digital_link, expires_in:)
  raise NotImplementedError, "#{self.class} must implement #deliver"
end