Module: Spree::Api::V3::Admin::SubclassedResource

Extended by:
ActiveSupport::Concern
Included in:
PaymentMethodsController, PromotionActionsController, PromotionRulesController
Defined in:
app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb

Overview

Shared ‘create` / `update` flow for STI parents whose subclass is picked at request time and whose configuration lives in a `preferences` hash (PaymentMethod, PromotionAction, PromotionRule).

Including controllers declare:

subclassed_via -> { Spree::PaymentMethod.providers },
               unknown_type_error: 'unknown_payment_method_type'

The body picks the subclass against the registry (returns 422 with the configured error code on miss), strips ‘type`/`preferences` from the permitted attrs, builds/assigns the rest, and routes preference values through the typed `preferred_<name>=` setters so booleans/decimals/etc. get coerced. Unknown preference keys are silently dropped — the schema endpoint is the source of truth for what’s settable.

Instance Method Summary collapse

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb', line 40

def create
  klass = resolve_subclass(params[:type])
  return render_unknown_type unless klass

  permitted = permitted_params_for(klass)
  attrs, preferences, calculator = extract_subclass_params(permitted)

  @resource = build_subclassed_resource(klass, attrs)
  apply_preferences(@resource, preferences) if preferences.present?
  apply_calculator(@resource, calculator) if calculator.present?
  authorize_resource!(@resource, :create)

  if @resource.save
    render json: serialize_resource(@resource), status: :created
  else
    render_validation_error(@resource.errors)
  end
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb', line 59

def update
  @resource = find_resource
  authorize_resource!(@resource, :update)

  permitted = permitted_params_for(@resource.class)
  attrs, preferences, calculator = extract_subclass_params(permitted)

  @resource.assign_attributes(attrs)
  apply_preferences(@resource, preferences) if preferences.present?
  apply_calculator(@resource, calculator) if calculator.present?

  if @resource.save
    render json: serialize_resource(@resource)
  else
    render_validation_error(@resource.errors)
  end
end