360 Secure SkillHub
360 安全技能中心

spree-extensions

该 Skill 提供安装 Spree 扩展 gem、用 spree_extension 脚手架创建自定义 Rails engine、注册 API/权限/hooks/provider,以及配套 dashboard 插件和测试的步骤与注意事项。

分类:开发
spree
热度 0.00
最近更新 2026-11-23 23:44:11

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add spree/agent-skills/spree-extensions
兼容性
通用兼容
联网要求
需要联网
数据出网
数据不出本机
Metadata
name
spree-extensions
description
Use when installing a Spree gem (spree_stripe, spree_easypost, spree_meilisearch, spree_opentelemetry, spree_i18n, spree_adyen, spree_multi_store…) or building your own reusable Spree 6 extension — a gem (Rails engine) for models, API endpoints, permissions, hooks and providers, paired with a dashboard plugin (npm package) for admin screens. Common phrasings - "add Stripe", "install spree_X", "create a Spree extension", "build a gem for Spree", "spree-extension create", "share this across apps", "register a provider from my gem", "engine initializer", "after_initialize vs to_prepare", "extension migrations", "ship openapi.yaml in my gem", "agent tools from my extension", "npm client for my extension". For single-app customization start with spree-customization — most work doesn't need a gem.

Spree Extensions

Commands use the spree CLI form (Rails app in server/). On a classic Rails app use bundle … / bin/rails … from the app root — see spree-project.

A Spree 6 extension has two halves:

Half What it is Holds
Gem (Rails engine) spree_<name> Ruby gem Models, migrations, Store/Admin API endpoints, serializers, permission scopes, workflow hooks, subscribers, provider classes, integrations
Dashboard plugin (optional) npm package using defineDashboardPlugin Admin screens, nav entries, product-page widgets — talking to the gem's Admin API endpoints

There are no views to add: Spree 6 has no Rails admin and no Rails storefront. Storefront pages live in your storefront app via @spree/sdk.

Is a gem the right shape? Only when the customization is shared across apps or published. For one app, put the same code in server/app/ and register it in server/config/initializers/spree.rb — see spree-customization.

Installing an extension gem

spree add --gem spree_reviews          # adds the gem, runs spree_reviews:install and migrations, restarts the server

# By hand instead: edit server/Gemfile, then
spree exec bundle install
spree generate spree_reviews:install    # convention: every extension ships <gem>:install
spree migrate
spree restart                           # Gemfile changes need a full restart (Ctrl+C `spree dev` if running)

On a fresh create-spree-app project using the prebuilt image, run spree eject once so the container builds from your server/ Gemfile.

The install generator typically copies migrations (db/migrate/<ts>_<name>.<engine_name>.rb) and may add an initializer. If the extension has a dashboard plugin, also pnpm add @scope/plugin in apps/dashboard/ — plugins are auto-discovered via the spree.dashboard.plugin marker in their package.json, no host code edit.

Gems in a scaffolded project

The spree-starter server Gemfile (what create-spree-app clones into server/) includes:

Gem Provides
spree Meta gem → spree_core + spree_api
spree_dashboard Serves the React admin at /dashboard
spree_emails Transactional emails
spree_stripe Stripe payments + Stripe Connect payout provider
spree_paypal_checkout PayPal payments
spree_adyen Adyen payments (cards, wallets, local methods via Drop-in)
spree_easypost EasyPost carrier rates, labels, tracking (delivery rate + fulfillment provider)
spree_meilisearch Meilisearch search provider (SpreeMeilisearch::SearchProvider)
spree_opentelemetry OpenTelemetry tracing (dormant until OTEL_* env is set)
spree_mcp The Admin MCP server at /api/v3/admin/mcp (spree add mcp on older projects). See spree-mcp
spree_i18n UI translations

spree_paypal_checkout ships with Spree at the same version from 6.0.3 and spree_adyen from 6.0.6; projects scaffolded from older templates add them with spree add paypal / spree add adyen, which pin the gem to the project's Spree version (upgrade Spree first on an older patch). The 0.x releases of both gems do not run on Spree 6. For any community gem from the Spree 4/5 era any community gem from the Spree 4/5 era (spree_reviews, spree_avatax_official, …), check the spree_core constraint first; most need porting (no spree_admin, no Spree::Adjustment, no state machines). Integrations catalog: https://spreecommerce.org/docs/integrations.

Multi-store sharing (one product/promotion/payment method across several stores) is not core — core records belong to one store. Use the spree_multi_store extension for that.

Building an extension

gem install spree_extension -v '>= 2.0'   # 1.x generates a Spree 5 scaffold (spree_admin, importmap) — don't use it
spree-extension create reviews            # → ./spree_reviews
cd spree_reviews

The 2.x scaffold depends on spree_core + spree_api (>= 6.0.0.beta), and ships:

  • lib/spree_reviews/engine.rb — a config.to_prepare that loads every app/**/*_decorator*.rb (boot + each dev reload) and a commented additional_permitted_attributes += example.
  • config/initializers/spree.rb — the extension's registration file, loaded by the host at boot like any initializer, with commented examples for register_scope, Spree.hooks.register and an after_initialize block for subscribers and registries.
  • config/routes.rb — the Spree::Core::Engine.add_routes hook, lib/spree_reviews/factories.rb, and an install generator that copies migrations.

Model

# db/migrate/20260901000000_create_spree_reviews.rb
create_table :spree_reviews do |t|
  t.references :store, null: false, index: true, foreign_key: false
  t.references :product, null: false, index: true, foreign_key: false
  t.integer :rating, null: false
  t.text :body
  t.boolean :approved, null: false, default: false
  t.timestamps
end
# app/models/spree/review.rb
module Spree
  class Review < Spree.base_class
    include Spree::SingleStoreResource     # store filled in; API scopes to current store

    has_prefix_id :review                  # review_k5nR8xLq on the wire
    publishes_lifecycle_events             # review.created / .updated / .deleted

    belongs_to :product, class_name: 'Spree::Product'   # required by default ("must exist")

    validates :rating, presence: true, inclusion: { in: 1..5 }
    scope :approved, -> { where(approved: true) }

    self.whitelisted_ransackable_attributes = %w[rating approved]
  end
end

Conventions: Spree.base_class, no FK constraints, class_name on associations, store-scoped data, prefixed IDs, events instead of callbacks.

API

Store API controller inherits Spree::Api::V3::Store::ResourceController; Admin API controller inherits Spree::Api::V3::Admin::ResourceController and must declare scoped_resource :reviews (the permission gate) and resource_permitted_attributes. Serializers subclass Spree::Api::V3::BaseSerializer (admin serializer extends the store one). Routes:

# config/routes.rb
Spree::Core::Engine.add_routes do
  namespace :api, defaults: { format: 'json' } do
    namespace :v3 do
      namespace(:store) { resources :reviews, only: [:index, :show] }
      namespace(:admin) { resources :reviews }
    end
  end
end

Full controller/serializer templates: spree-resource (the spree:api_resource generator emits them) and spree-api-v3. Namespace paths of plugin-owned resources to avoid collisions with future core resources.

Documenting the API: agent tools and an SDK client

A gem documents its endpoints in its own openapi/admin.yaml (and openapi/store.yaml for Store endpoints). Spree merges every installed gem's admin document into core's at runtime, so each documented admin action becomes an Admin MCP / dashboard-assistant tool with no registration.

bundle exec rails g spree_dev_tools:openapi   # swagger helper, contract spec, skipped skeleton specs (spree_dev_tools >= 1.0.1)
bundle exec rake spree:openapi:generate       # writes openapi/*.yaml from spec/integration/**/*_spec.rb (task from extension_rake)
  • Write the specs with admin_operation, request_body_for and filter_parameters_for, then commit the YAML. See spree-resource § Documenting endpoints.
  • Keep spec/agent_tools_spec.rb (it_behaves_like 'an engine whose admin writes are agent tools', MyExtension::Engine). It fails on:
    • undocumented admin actions,
    • opt-outs without a reason,
    • tool-name clashes with core or other gems,
    • conflicts with core's document.
  • Ship the YAML in the gem. A git ls-files gemspec already does. A hand-listed one needs s.files = Dir['{app,config,db,lib}/**/*', 'openapi/*.yaml', 'LICENSE', 'README.md']. Without it the tools vanish once the gem is installed.
  • Typed client. An npm package next to the gem sets "spree": { "extension": "../my_extension" } in its package.json and runs spree sdk generate. Apps then use extendAdminClient(createAdminClient(…)). See spree-typescript-sdk.
  • Give the gem's serializers names the SDK doesn't use (not OrderSerializer), or generated types clash.

Extension hooks (6.0.3+)

  • Spree::Dependencies.register puts the extension's own workflows behind a key that a store can swap, the same way core's *_workflow keys work (see spree-dependencies for swapping).
  • Spree.cart_copiers copies an extension's cart-owned records onto the order the cart becomes.
  • The orders.complete.validate hook can refuse an order's placement while holding its row lock.

Registrations — where and when

Registrations go in the extension's config/initializers/spree.rb; model-touching code goes in the engine's config.to_prepare. Timing matters, because core assigns some registries with = inside its own config.after_initialize, wiping anything appended earlier:

Registration Put it in
Spree.payment_methods <<, Spree.fulfillment_providers <<, Spree.integrations <<, Spree.stock_splitters <<, Spree.tracking_carriers[...] =, Spree.adjusters <<, Spree.promotions.actions <<, Spree.*_authentication_strategies.add Rails.application.config.after_initialize do … end — core assigns these in its own after_initialize, which runs first
Spree.subscribers, Spree.delivery_rate_providers, tax_providers, payout_providers, digital_asset_providers, pricing_providers, inventory_providers, order_routing.rules/strategies, promotions.rules, Spree.reporting Seeded before app initializers and core concatenates — any point works; after_initialize is the safe uniform choice (and avoids autoloading classes mid-boot)
Spree.hooks.register(...), Spree.permissions.register_scope(...), Spree.search_provider = Top level of the initializer (string/registry-based, reload-safe)
Spree::Product.additional_permitted_attributes += [...], decorator loading, anything touching model classes Engine config.to_prepare (re-runs on dev reload)
# config/initializers/spree.rb (inside the gem)
Spree.permissions.register_scope(:reviews, group: :catalog, resources: -> { [Spree::Review] })
Spree.hooks.register('products.activate.validate', 'SpreeReviews::RequireDescription')

Rails.application.config.after_initialize do
  Spree.subscribers << SpreeReviews::ReviewRequestSubscriber   # `bin/rails g spree:subscriber` adds these; pass the class — a String crashed production boot through 6.0.0.beta4
  Spree.integrations << 'SpreeReviews::Integration'            # if the gem needs per-store credentials
end
# lib/spree_reviews/engine.rb
module SpreeReviews
  class Engine < Rails::Engine
    require 'spree/core'
    isolate_namespace Spree
    engine_name 'spree_reviews'

    config.to_prepare do
      Dir.glob(SpreeReviews::Engine.root.join('app/**/*_decorator*.rb')) do |decorator|
        Rails.configuration.cache_classes ? require(decorator) : load(decorator)
      end
      Spree::Product.additional_permitted_attributes += [:reviews_enabled]  # += never << (frozen array)
    end
  end
end

A host app registers the same things the same way from server/config/initializers/spree.rb. to_prepare is not enough for the core-assigned registries — at boot it runs before after_initialize, so core overwrites it.

What each registration buys you:

  • register_scope mints read_reviews / write_reviews, shown in the staff role editor and grantable to secret API keys. Label it in config/locales/en.yml under en.spree.permissions_catalog.resources.reviews.{label,description}. Options: write: false, audiences:, read_only_for:. See spree-auth-permissions.
  • Workflow hooks run inside a core flow and can stop it (workflow.reject!('…')). Hook keys are validated at boot when eager loading — a typo fails startup. See spree-workflows.
  • Subscribers (Spree::Subscriber, subscribes_to 'order.placed') react after the fact. See spree-events-webhooks.
  • Providers plug into tax, delivery rates, fulfillment, payments, search, payouts, digital assets, auth. See spree-providers for every base class and registry.
  • Spree.dependencies swaps a core service/workflow (*_workflow keys). See spree-dependencies. Prefer hooks — a swapped workflow must be kept in sync with core.

Reach for decorators last (e.g. adding has_many :reviews to Spree::Product) — see spree-decorators.

Dashboard plugin half

npx @spree/cli plugin new reviews       # or `spree plugin new reviews`

The plugin calls defineDashboardPlugin(...) to add nav entries, routes and slot widgets, and reads/writes your endpoints through @spree/admin-sdk's client.request escape hatch. Ship it as an npm package with the spree.dashboard.plugin marker; consumers pnpm add it and it auto-registers. Namespace nav keys, locale keys and API paths (acme-brands, not brands) — duplicate keys throw at boot. See spree-dashboard-plugins.

Testing the extension

bundle exec rake test_app     # generate spec/dummy (re-run after adding migrations)
bundle exec rspec
bundle exec rake spree:openapi:generate   # after API changes; commit openapi/*.yaml

Uses RSpec + Factory Bot via spree_dev_tools. Put your factories in lib/spree_reviews/factories.rb. API specs use shared contexts from spree/api/testing_support/v3/base — 'API v3 Store', 'API v3 Admin authenticated', 'API v3 Admin with custom permissions', 'API v3 Seller authenticated'. See spree-testing.

Gotchas

  • Registered in the wrong phase = silently missing. A payment method, fulfillment provider or integration appended outside after_initialize disappears at boot (core reassigns those arrays). Symptom: not selectable in the dashboard / "type must be registered" 422.
  • additional_permitted_attributes << :x raises FrozenError. Always +=.
  • Admin controller without scoped_resource raises at request time — every Admin API controller must name its permission scope.
  • Migrations don't auto-apply. After bumping an extension, re-run its install generator (or spree exec bin/rails railties:install:migrations) then spree migrate. spree migrate alone only installs core migrations.
  • Don't rename copied migrations — the .<engine_name>.rb suffix is how Rails skips already-copied ones and how Spree's boot check spots missing ones.
  • Credentials belong in a Spree::Integration (per store, :password preferences are masked), not ENV — a multi-store app pays/ships from different accounts.
  • Two decorators on the same method — last loaded wins. Prefer hooks/events so extensions compose.
  • Subscriber code hot-reloads; changing a registration (the initializer) needs a restart.

Where to read further

  • Extension tutorial: node_modules/@spree/docs/dist/developer/contributing/creating-an-extension.md
  • Providers & integrations: node_modules/@spree/docs/dist/developer/providers/overview.md
  • Dashboard plugins: node_modules/@spree/docs/dist/developer/dashboard/plugins/{overview,scaffolding,distributing}.md
  • Customization quickstart: https://spreecommerce.org/docs/developer/customization/quickstart
  • Related skills: spree-customization, spree-providers, spree-workflows, spree-resource, spree-mcp, spree-typescript-sdk, spree-auth-permissions, spree-dashboard-plugins, spree-testing