Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Store preferences as JSON and encrypt secrets (V-3730) (#14789)

Damian Legawiecf52fc12c788b
diff --git a/.changeset/calculator-summary-lists.md b/.changeset/calculator-summary-lists.md
new file mode 100644
index 0000000000..1e0b0eba48
--- /dev/null
+++ b/.changeset/calculator-summary-lists.md
@@ -0,0 +1,5 @@
+---
+"@spree/dashboard-ui": patch
+---
+
+The calculator summary on promotion action rows shows how many tiers a tiered calculator has, instead of `[object Object]`.
diff --git a/.changeset/tiers-editor-input-labels.md b/.changeset/tiers-editor-input-labels.md
new file mode 100644
index 0000000000..2ddd17aaf5
--- /dev/null
+++ b/.changeset/tiers-editor-input-labels.md
@@ -0,0 +1,5 @@
+---
+"@spree/dashboard-core": patch
+---
+
+Tier threshold and discount inputs now carry accessible names, so screen readers announce what each field is.
diff --git a/.changeset/tiers-editor-list-shape.md b/.changeset/tiers-editor-list-shape.md
new file mode 100644
index 0000000000..1149b86f34
--- /dev/null
+++ b/.changeset/tiers-editor-list-shape.md
@@ -0,0 +1,5 @@
+---
+"@spree/dashboard-core": patch
+---
+
+The tiers editor for the tiered percent and tiered flat rate calculators now reads and writes tiers as a list of `{ threshold, value }` entries, the shape Spree 6.0 stores them in. It still reads the old hash keyed by threshold, so a calculator the upgrade has not converted yet opens correctly.
diff --git a/CLAUDE.md b/CLAUDE.md
index d20b2de758..804618f1b1 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -190,6 +190,7 @@ end
- No foreign key constraints
- No default values on string/status columns (statuses are set by the creating workflow); integer, decimal and boolean columns DO carry defaults (`quantity` 1, amounts 0) so raw inserts can't produce nulls
- Every metadata-carrying table has a single `metadata` JSON column — the `public_metadata`/`private_metadata` split was consolidated in 6.0
+- `preferences` columns are JSON, never `text` — YAML preferences are converted in 6.0 (see `docs/plans/6.0-json-preferences.md`); hash preferences never use number keys; secrets are `:password` preferences, stored encrypted in a `secret_preferences` `text` column
- Always add `null: false` on required columns
- One migration per feature when possible
- Data transformations go in rake tasks, never in migrations
@@ -747,6 +748,7 @@ Re-run `parallel_setup` after schema changes (`scripts/test/rspec <engine>` does
- Controller specs: always add `render_views`, use `stub_authorization!` for auth
- Use controller specs for testing edge cases, API integration tests are only for happy path/simple 422 failures to generate OpenAPI examples; otherwise they get too brittle and high-maintenance
- Time-based tests: use `Timecop`
+- NEVER update or save the shared default store (`@default_store`) in specs — it outlives the example, so the change leaks into other specs. Stub what the code reads (`allow(Spree::Store).to receive(:default).and_return(build(:store, ...))`) or create a separate store
- Don't over-engineer or repeat tests
- Fold specs into the existing describe blocks, use context blocks for different scenarios, NEVER create new test files for a single new scenario unless it is a completely new feature
diff --git a/docs/developer/customization/model-preferences.mdx b/docs/developer/customization/model-preferences.mdx
index 1cd789d462..67f1612d79 100644
--- a/docs/developer/customization/model-preferences.mdx
+++ b/docs/developer/customization/model-preferences.mdx
@@ -14,12 +14,14 @@ store.save
To define a model preference, you need to add them to your model class.
-Make sure to generate a migration to add the `preferences` column to the table. This column will store the preferences in a serialized format.
+Make sure to generate a migration to add the `preferences` column to the table. This column stores the preferences as JSON.
```bash
-spree generate migration AddPreferencesToSpreeBrands preferences:text
+spree generate migration AddPreferencesToSpreeBrands preferences:jsonb
```
+`jsonb` is for PostgreSQL, the default database. On MySQL or SQLite use `preferences:json`.
+
Run the migration.
```bash
@@ -92,7 +94,28 @@ You can get a hash of all stored preferences by accessing the `preferences` help
brand.preferences # => { 'featured' => false, 'display_name' => 'Wilson' }
```
-This hash will contain the value for every preference that has been defined for the model instance, whether the value is the default or one that has been previously stored.
+This hash will contain the value for every preference that has been defined for the model instance, whether the value is the default or one that has been previously stored. Its keys are strings, and it can be read with either strings or symbols (`brand.preferences[:featured]`). Values are stored as JSON, so read typed values through the `preferred_*` methods: a `:decimal` preference comes back as a `BigDecimal` there, while the raw hash holds its exact string (`"9.99"`).
+
+`preferences` never holds secrets — see [Secret preferences](#secret-preferences).
+
+## Secret preferences
+
+Declare API keys, signing secrets and other credentials with the `:password` type:
+
+```ruby server/app/models/my_app/gateway.rb
+module MyApp
+ class Gateway < Spree::Gateway
+ preference :publishable_key, :string
+ preference :secret_key, :password
+ end
+end
+```
+
+A `:password` preference works like any other — `preferred_secret_key`, `preferred_secret_key = ...`, `get_preference(:secret_key)` — but it is stored apart from the other preferences, in an encrypted `secret_preferences` column, whenever [Active Record encryption keys](/developer/deployment/environment_variables#active-record-encryption) are configured. The Admin API returns it masked, showing only its last four characters.
+
+Payment methods and integrations already have that column. To declare a secret on another model, include `Spree::SecretPreferences` and add a `secret_preferences` text column to its table; Spree refuses a `:password` preference on a model that cannot encrypt it.
+
+Always declare a credential as `:password`. A secret declared as `:string`, or nested inside a `:hash` or `:array` preference, is stored in plain text.
## Models with preferences
diff --git a/docs/developer/deployment/environment_variables.mdx b/docs/developer/deployment/environment_variables.mdx
index 3da240eb79..587d276475 100644
--- a/docs/developer/deployment/environment_variables.mdx
+++ b/docs/developer/deployment/environment_variables.mdx
@@ -18,7 +18,7 @@ You'll almost always want to set [`RAILS_HOST`](#urls-and-hosts) too — without
## Active Record encryption
-Spree encrypts sensitive values at rest with [Active Record encryption](https://guides.rubyonrails.org/active_record_encryption.html): webhook endpoint signing secrets, payment gateway customer IDs, and the OAuth tokens stored on user identities. It does so only when encryption keys are configured — without them, these values are stored in plain text and a production app logs a warning at boot.
+Spree encrypts sensitive values at rest with [Active Record encryption](https://guides.rubyonrails.org/active_record_encryption.html): payment gateway and integration secrets (API keys and webhook signing secrets), webhook endpoint signing secrets, payment gateway customer IDs, and the OAuth tokens stored on user identities. It does so only when encryption keys are configured — without them, these values are stored in plain text and a production app logs a warning at boot.
| Variable | Description |
| --- | --- |
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdx
index 5efe25c361..0bcc3ca2e9 100644
--- a/docs/developer/upgrades/5.6-to-6.0.mdx
+++ b/docs/developer/upgrades/5.6-to-6.0.mdx
@@ -53,6 +53,26 @@ Re-running is safe — `bundle exec rake spree:upgrade` figures out what still n
Reference material — the data backfills `bundle exec rake spree:upgrade` executes. Every task is idempotent.
+### Store preferences as JSON
+
+<CodeGroup>
+
+```bash Spree CLI (Docker)
+spree rake spree:upgrade:preferences_json
+spree rake spree:upgrade:encrypt_secret_preferences
+```
+
+```bash Without Spree CLI
+bundle exec rake spree:upgrade:preferences_json
+bundle exec rake spree:upgrade:encrypt_secret_preferences
+```
+
+</CodeGroup>
+
+Preferences were stored as YAML; in 6.0 they are JSON. The migration converts every `preferences` column itself, and moves gateway and integration secrets (preferences declared as `:password`) into a new `secret_preferences` column. The first task is a safety net for anything the migration could not reach — a schema changed by hand, or an extension gem installed after the migration ran. The second encrypts the moved secrets; until it runs they stay readable and are encrypted the next time their payment method or integration is saved. It does nothing until [encryption keys](#enable-active-record-encryption) are configured.
+
+See [Preferences are stored as JSON](#preferences-are-stored-as-json) for what changes in your code.
+
### Convert incomplete orders into carts
```bash
@@ -187,7 +207,7 @@ Countries and states are no longer database records in 6.0. Addresses, delivery
## Enable Active Record encryption
-Spree encrypts webhook endpoint signing secrets, payment gateway customer IDs and the OAuth tokens on user identities with [Active Record encryption](https://guides.rubyonrails.org/active_record_encryption.html) — but only when encryption keys are configured. Without keys these values are stored in plain text. New 6.0 projects get keys at setup; upgraded applications need to add them.
+Spree encrypts payment gateway and integration secrets, webhook endpoint signing secrets, payment gateway customer IDs and the OAuth tokens on user identities with [Active Record encryption](https://guides.rubyonrails.org/active_record_encryption.html) — but only when encryption keys are configured. Without keys these values are stored in plain text. New 6.0 projects get keys at setup; upgraded applications need to add them.
**1. Make the app read the keys.** Projects created from `spree-starter` read them from the `ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY`, `ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY` and `ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT` env vars, falling back to the `active_record_encryption` entry in Rails credentials. If your `config/application.rb` predates this, add inside the `Application` class:
@@ -222,7 +242,31 @@ Recreate the containers so they pick up the new `.env` (`spree update`, or `spre
Never change or lose the keys once data is encrypted — the encrypted records become unreadable.
</Warning>
-**3. Encrypt existing rows.** Rows written before the keys were set are plain text. OAuth tokens on user identities stay readable and are encrypted on their next write. Webhook endpoint secrets and gateway customer IDs can't be read in plain text once encryption is on — follow [Enabling encryption on an existing installation](/developer/deployment/environment_variables#enabling-encryption-on-an-existing-installation): turn on `support_unencrypted_data` and `extend_queries`, deploy with the keys, run `find_each(&:encrypt)` over `Spree::WebhookEndpoint`, `Spree::GatewayCustomer` and `Spree::UserIdentity`, then turn the two settings off again.
+**3. Encrypt existing rows.** Rows written before the keys were set are plain text. Gateway and integration secrets and the OAuth tokens on user identities stay readable and are encrypted on their next write; run `spree rake spree:upgrade:encrypt_secret_preferences` (`bundle exec rake spree:upgrade:encrypt_secret_preferences` without the Spree CLI) to encrypt every gateway and integration secret at once. Webhook endpoint secrets and gateway customer IDs can't be read in plain text once encryption is on — follow [Enabling encryption on an existing installation](/developer/deployment/environment_variables#enabling-encryption-on-an-existing-installation): turn on `support_unencrypted_data` and `extend_queries`, deploy with the keys, run `find_each(&:encrypt)` over `Spree::WebhookEndpoint`, `Spree::GatewayCustomer` and `Spree::UserIdentity`, then turn the two settings off again.
+
+## Preferences are stored as JSON
+
+Model preferences moved from YAML to JSON, and secrets moved out of them. Declarations do not change, and the `preferred_*` methods return the same values as before. What can affect your code:
+
+- **The raw `preferences` hash has string keys.** It still answers to symbols (`calculator.preferences[:amount]`), but `preferences.keys` returns strings. Decimals sit in it as exact strings (`"9.99"`); read them through `preferred_*`, which returns a `BigDecimal`.
+- **Secrets are not in `preferences`.** Preferences declared `:password` live in the encrypted `secret_preferences` column. Read them with `preferred_*` or `get_preference`. A secret you assign as part of a whole hash (`update(preferences: { secret_key: ... })`) is moved there when the record is saved. To declare a secret on a model other than a payment method or an integration, see [Secret preferences](/developer/customization/model-preferences#secret-preferences).
+- **Your own preference tables need a JSON column.** The migration converts every table a model storing Spree preferences reads, including your application's own; a table another library owns is left alone. A new table should use `t.jsonb :preferences` on PostgreSQL, the default database (`t.json` on MySQL or SQLite).
+- **Tiered calculators take a list of tiers.** `Spree::Calculator::TieredPercent` and `TieredFlatRate` store `tiers` as a list instead of a hash keyed by threshold, in Ruby and in the Admin API. The migration converts existing calculators.
+
+ ```json
+ // Before
+ { "tiers": { "100": 10, "250": 15 } }
+ // After
+ { "tiers": [{ "threshold": "100", "value": "10" }, { "threshold": "250", "value": "15" }] }
+ ```
+
+ `value` is a percentage for `TieredPercent` and an amount for `TieredFlatRate`.
+- **Spree no longer allows extra classes in YAML columns.** It used to add `Symbol`, `BigDecimal` and a few time classes to `config.active_record.yaml_column_permitted_classes`. If your application serializes its own columns as YAML with those classes, add them in `config/application.rb` yourself.
+- **Preference change methods follow Rails' naming.** Each preference is a Rails store accessor, so `preferred_<name>_changed?`, `preferred_<name>_change` and `preferred_<name>_was` work as before, while the after-save forms are Rails' own: `saved_change_to_preferred_<name>?` replaces `preferred_<name>_previously_changed?`, `saved_change_to_preferred_<name>` replaces `preferred_<name>_previous_change`, and `preferred_<name>_before_last_save` replaces `preferred_<name>_previously_was`.
+- **Per-preference helper methods are gone.** Use `preference_type(:name)`, `preference_default(:name)` and `preference_deprecated(:name)` instead of `preferred_<name>_type`, `preferred_<name>_default` and `preferred_<name>_deprecated`. `clear_preferences`, `restore_preferences_for`, `preferences_of_type`, the class methods `preference_getter_method`, `preference_setter_method` and `prefers_query_method`, `Spree::Preferences::ScopedStore`, `Spree::Preferences::Store` and the `Spree::Preference` model are removed.
+- **The `spree_preferences` table is dropped.** The installation id it held is not carried over: the default store generates a new one the next time it is saved. Rows an application wrote there itself are dropped with it: copy any you need before upgrading.
+- **Preferences live only on models.** `Spree::Preferences::Preferable` is included by `Spree::Base` and needs a `preferences` JSON column; including it in a plain Ruby class is no longer supported. A hash stored inside a preference comes back with string keys.
+- **A record whose preferences still hold YAML cannot be read.** The migration converts every row, so this only happens to rows written by older code after it ran. Run `spree rake spree:upgrade:preferences_json` (`bundle exec rake spree:upgrade:preferences_json` without the Spree CLI) to convert it.
## The Cart/Order split
diff --git a/docs/plans/6.0-json-preferences.md b/docs/plans/6.0-json-preferences.md
new file mode 100644
index 0000000000..f55b017c97
--- /dev/null
+++ b/docs/plans/6.0-json-preferences.md
@@ -0,0 +1,163 @@
+# Preferences Stored as JSON, Secrets Encrypted
+
+**Status:** In Progress — everything this plan targets for 6.0 is built (JSON coder, encrypted secret column, conversion migration, upgrade tasks, tiered calculators, docs); waiting on review and CI
+**Target:** Spree 6.0
+**Depends on:** nothing. Follows the precedent of the metadata consolidation (`20260809120001_consolidate_metadata_columns.rb`)
+**Author:** Damian + Claude
+**Last updated:** 2026-10-01 (implementation)
+**Tracking:** Linear V-3730 · GitHub spree/spree#14786
+
+## Summary
+
+Every model's `preferences` column is a YAML document, written by one line in `Spree::Preferences::Preferable` (`serialize :preferences, type: Hash, coder: YAML`). This plan moves them to a JSON column (`jsonb` on PostgreSQL, `json` on MySQL and SQLite) in 6.0, converting the stored data during the 6.0 upgrade. At the same time, preferences declared `:password` (gateway and integration secrets) move out of `preferences` into an encrypted `secret_preferences` column, so the main column never holds a secret. Declarations do not change: `preference :secret_key, :password` and `preferred_secret_key` work as before.
+
+Reasons, in order: gateway secrets — including live Stripe secret keys — sit in the database in plain text today; loading YAML is an attack surface Rails has to fence with `yaml_column_permitted_classes`, a list Spree injects into every application and every Rails upgrade touches; and two tables built in 6.0 already declare `preferences` as JSON but, through the shared coder, hold a YAML document wrapped in a JSON string. Being able to query or index preferences is a side benefit only — nothing queries them today. 6.0 is a clean-break release and already carries a data-converting migration of the same shape, which makes it the right release for this.
+
+## Inventory
+
+Measured on `main` at 2026-10-01. Smaller than expected: there is no log entry table any more, and global configuration (`Spree::Config`) is held in memory and environment variables, not in the database.
+
+| Column | Today | Notes |
+| --- | --- | --- |
+| `preferences` on `spree_calculators`, `spree_payment_methods`, `spree_promotion_rules`, `spree_stores`, `spree_integrations`, `spree_imports`, `spree_exports`, `spree_price_rules`, `spree_channels`, `spree_order_routing_rules`, `spree_seller_requirements` | `text`, YAML | The main body of the work |
+| `preferences` on `spree_delivery_method_rules`, `spree_commission_rules` | `jsonb`/`json` column holding a YAML string | Already the right column type; only the data is wrong |
+| `spree_preferences.value` | `text`, YAML | One live row: the installation id. The table is dropped without copying it; the default store generates a new id in an internal `install_id` preference when it is next saved (settled during implementation) |
+| `spree_gateways.preferences` | `text` | Leftover 4.3 table, no model reads it. Not touched: the migration converts only tables a model storing Spree preferences reads, so a table another library owns is never changed |
+| `Spree::Address` `serialize :preferences` | declaration only | `spree_addresses` has no such column. Kept, with the JSON coder: removing it changes the address attribute set that duplicate detection compares |
+
+`metadata` is already JSON (`Spree::Metadata::HashSerializer`) and is out of scope. No provider gem (`spree_stripe`, `spree_easypost`, `spree_meilisearch`) has a preferences table of its own.
+
+**Values found in the YAML today**, by declared preference type (counted across core and providers): string 69, boolean 58, integer 54, decimal 36 (stored as `BigDecimal`), password 7 (Stripe publishable key, secret key and two webhook signing secrets; EasyPost API key and webhook secret; the test gateway's key), array 16, hash 4, any 3, date 2 (already stored as a `yyyy-MM-dd` string), text 1. No `datetime` preference is declared anywhere. Keys are symbols.
+
+## Key Decisions (do not deviate without discussion)
+
+1. **One JSON coder replaces YAML in `Preferable`.** It writes a plain JSON object with string keys and loads it as a `HashWithIndifferentAccess`, so `preferences[:amount]`, `fetch(:amount)` and the generated `preferred_*` readers keep working unchanged.
+2. **Decimals and times are restored on read from their declared type** (narrowed 2026-10-01 during implementation). A `:decimal` preference read from JSON comes back as a `BigDecimal` and a `:datetime` one as a time, exactly as they did from YAML; both are stored as their exact JSON strings. Only these two: every other declared type survives JSON unchanged, and running the full writer cast on read would turn a stored `nil` into `""` or `0` for non-nullable strings and numbers. The preference declarations are the schema; no second schema is introduced.
+3. **Symbols are not stored.** Keys become strings; symbol values become strings. Code comparing preference values to symbols must compare strings.
+4. **The conversion runs inside the 6.0 migration, not in an upgrade task.** `spree:upgrade` runs its manifest *after* `db:migrate` (see `upgrade.rake` and the CLI's `upgrade` command), so a backfill scheduled there would find the YAML column already replaced. This is the reasoning `ConsolidateMetadataColumns` recorded for the same problem. The proposal's "add a column, backfill with a task, swap later" order does not work with Spree's upgrade runner.
+5. **The migration never uses models.** It reads rows with `select_all`, parses with `YAML.safe_load` and an explicit permitted-class list, and writes with `exec_update`. A model would load through whichever coder the current code declares, which is the thing being changed. The one thing it asks a class is which of its preferences are `:password` (Decision 12), from the declaration alone, without loading a record.
+6. **No batching or background job.** Every table in the inventory is configuration-sized (one row per calculator, rule, gateway, store, import). The append-only log table the proposal anticipated no longer exists.
+7. **A row still holding YAML fails when it is read, not at boot** (decided 2026-10-01). A YAML document cannot be loaded as a hash, so reading that record raises, and the `spree:upgrade:preferences_json` task finds and converts any leftovers. A dedicated error naming the task was built first and then dropped with the custom coder (see Coder below): the migration converts every row it can read and stops on any it cannot, so the error only covered rows written by pre-6.0 code after the migration ran. A boot check was rejected: boot also happens where there is no database (asset builds) and before the converting migration has run, so it would block the very migration that repairs the data, and it would cost a query per table on every start. Accepted trade-off: a missed row breaks the requests that touch that record rather than the whole application, so the failure is loud but local.
+8. **Decimals are stored as exact strings** (decided 2026-10-01): `{"amount": "9.99"}`, which is what `BigDecimal#as_json` produces and what the API already returns. The writer keeps that same string in memory (as do tier ladders), so a record just written and one read back hold identical values and setting a decimal to the value it already has is not a change; the `preferred_*` reader returns the `BigDecimal`. A JSON number was rejected because Ruby and JavaScript parse it as a float before the read cast can restore it. Accepted trade-off: a numeric comparison in SQL needs an explicit cast (`(preferences->>'amount')::numeric`).
+9. **Tiered calculators store tiers as a list, not a hash with number keys** (decided 2026-10-01). `Spree::Calculator::TieredPercent` and `TieredFlatRate` change `preference :tiers, :hash` to `preference :tiers, :array` of `{ "threshold", "value" }` objects, sorted by threshold. One `value` key for both calculators (settled during implementation, after review found the dashboard's tiers editor cannot tell which calculator it is editing): the calculator's type already says whether the value is a percentage or an amount, and per-calculator keys would have needed a new field in the preference schema. JSON object keys are always strings, so the hash shape could only survive with a hidden cast; a list reads naturally in JSON and TypeScript. This changes the `tiers` value these calculators show through the Admin API, which is a breaking change listed in the upgrade guide. The migration converts the shape. Dropping both calculators was considered and rejected (2026-10-01): they discount by spend, while price-list and catalog volume pricing works per variant by quantity, so they are not duplicates; banded promotions with the item-total rule could replace them, but only as several promotions for what is one today.
+10. **Secrets live in their own encrypted column, `secret_preferences`** (decided 2026-10-01). A `:password` preference is read from and written to `secret_preferences`, a `text` column holding a JSON object and declared with Rails' standard `encrypts`. Everything else stays in `preferences`. The declaration DSL, the `preferred_*` methods, masking and `PreferenceSchema` are unchanged — only where the value is stored moves. Rejected: encrypting each secret inside the `preferences` JSON (it would call Rails' encryptor by hand and rebuild key rotation, plain-text fallback and log filtering that `encrypts` already provides, and secrets would still travel wherever `preferences` is exported or logged); and a polymorphic `Spree::Credential` model (an extra query per gateway load, preference dirty tracking that must look across a child record, secrets silently lost when a record is duplicated — a credential lifecycle nothing needs yet; it can be built later on top of this without touching declarations again).
+11. **Encryption follows Spree's existing rule: on whenever keys are configured.** New projects get Active Record encryption keys at setup, so their secrets are encrypted from the first save. An installation without keys keeps working with secrets in plain text inside `secret_preferences`, and gets the production boot warning Spree already logs for its other encrypted fields. This is the same condition `Spree::WebhookEndpoint`, `Spree::GatewayCustomer` and `Spree::UserIdentity` use.
+12. **Existing secrets are moved by the migration and encrypted by an upgrade task; nothing breaks in between.** The migration copies each `:password` value out of `preferences` into `secret_preferences` as plain JSON — it needs no encryption keys and so can never fail for lack of them. `encrypts :secret_preferences, support_unencrypted_data: true` keeps those plain values readable (the precedent is `Spree::UserIdentity`), so every gateway keeps working the moment the migration finishes. The `spree:upgrade:encrypt_secret_preferences` task then encrypts every row that is still plain; without it a row is encrypted on its next save anyway. A secret's reader looks in `preferences` first and then in `secret_preferences` (settled during implementation): a secret assigned as part of a whole hash waits there until `before_save` moves it, and a secret of a class the migration could not load stays readable until `spree:upgrade:preferences_json` moves it. The writer always writes the encrypted column and removes any copy from `preferences`.
+
+## Design Details
+
+### Coder
+
+No Spree-specific coder (settled during implementation). The `preferences` column is a native JSON column, which Rails already converts to and from a hash, so the only coder is `Spree::Metadata::HashSerializer`, the one `metadata` uses, whose sole job is to let the loaded hash answer to symbol and string keys alike. Without it, a symbol lookup on the raw hash (`calculator.preferences[:amount]`, common in Spree and in applications) would quietly return `nil` instead of the value. Decimals are kept as exact strings by the preference writer, so the coder does not need to normalize them.
+
+```ruby
+serialize :preferences, coder: Spree::Metadata::HashSerializer
+```
+
+`as_json` turns `BigDecimal` into its exact string (`"9.99"`), `Date` into ISO 8601 and symbols into strings, so `dump` never hands the adapter a Ruby-only type. Rails' JSON column type has already decoded the value before `load` sees it, on every adapter, so a string reaching `load` can only be a leftover YAML document.
+
+### Reads cast through the declared type
+
+```ruby
+define_method preference_getter_method(name) do
+ stored = (preferences || {}).fetch(name) { return default.call }
+ convert_preference_value(stored, type, nullable: nullable)
+end
+```
+
+`convert_preference_value` is idempotent for every type it handles, so casting on read changes nothing for values written in this process.
+
+### Places that assume symbol keys
+
+Three raw reads (`provision_defaults.rb`, `promotion/rules/country.rb` twice) keep working through indifferent access. Two places compare key sets and need a stringify: `Spree::Base`'s `after_initialize` backfill (`default_preferences.except(*preferences.keys)` would otherwise see every default as missing and dirty every record on load) and `Preferable#preference_change`.
+
+### Migration
+
+One migration, `ConvertPreferencesToJson`, for every table in the inventory:
+
+1. For each table that has a `preferences` column whose values are YAML: add `preferences_json` (`jsonb` if the adapter has it, else `json`), convert each row, drop `preferences`, rename `preferences_json` to `preferences`.
+2. For the two tables already typed JSON: rewrite each row whose value is a string in place.
+3. Drop `spree_preferences`. The installation id is not copied; rolling back recreates the table empty.
+
+Conversion per row: `YAML.safe_load(text, permitted_classes: [Symbol, BigDecimal, Date, Time, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone, ActiveSupport::HashWithIndifferentAccess], aliases: true)` — the list Spree's engine already permits, plus `Date` — then `as_json`. Tables are discovered from the schema (any table with a `preferences` column), as the metadata migration does, so an application's own `Preferable` table converts too.
+
+A row that does not parse — typically an old `!ruby/hash-with-ivars:ActionController::Parameters` dump, which the current code already cannot load — stops the migration with the table name and row id, rather than being converted to `{}`.
+
+### Secret preferences
+
+```ruby
+# PreferableClassMethods — routing by declared type; the DSL is untouched
+store_accessor(type == :password ? :secret_preferences : :preferences, name, prefix: :preferred)
+
+# A model whose table has the column
+encrypts :secret_preferences, support_unencrypted_data: true if ActiveRecord::Encryption.config.has_primary_key?
+serialize :secret_preferences, coder: Spree::SecretPreferences::TextHashSerializer # JSON in a text column, then HashSerializer
+```
+
+- **Which tables get the column:** `spree_payment_methods` and `spree_integrations`, the only tables whose classes declare `:password` preferences today. A model opts in by including `Spree::SecretPreferences`; an application's own table adds the column itself.
+- **Declaring a secret on a model that has not opted in raises** `ArgumentError` at declaration time, naming the concern and the column to add. A secret is never silently written to the plain column.
+- **A secret's default is never copied into storage.** The backfill that fills in missing defaults on load skips secrets; their defaults are read through the `preferred_*` reader. Masking reads only the stored value, so an unset secret never reveals its default.
+- **Gateway connection options** (`Spree::Gateway#options`) merge both columns and every secret's reader, so gateways still receive their keys.
+- **An empty `secret_preferences` column reads as `nil`, not `{}`.** Encrypted attributes compare the stored value with the loaded one, so an empty hash would mark every record without secrets as changed the moment a secret was read.
+- **Declarations stay current down the class tree.** Each class keeps only its own declarations and merges its ancestors' on demand, cached until the next declaration anywhere — so a secret a decorator adds to `Spree::PaymentMethod` after its subclasses loaded is still routed to the encrypted column.
+- **Code that writes preferences without callbacks writes both columns.** `update_columns(preferences: ...)` skips the save hook that moves secrets, so a writer like Stripe's `migrate_stripe_webhook_keys` sets values through the `preferred_*` writers and then writes `preferences` and `secret_preferences` together.
+- **The safety-net task converts a column that is still `text`** — a table whose model the migration did not load — with the same steps as the migration, instead of pointing back at a migration that has already run.
+- **Each preference is a Rails store accessor** (decided 2026-10-01, at the author's request, in the same pull request). `preference` registers a store accessor on `preferences` (or `secret_preferences` for a secret) with the `preferred` prefix, so Rails supplies the change methods; the hand-written ones are gone, and their after-save forms take Rails' names (`saved_change_to_preferred_<name>?`, `preferred_<name>_before_last_save`). One table of definitions per class (`preference_definitions`) replaces the five helper methods every preference generated and the `methods.grep` that found them. The raw `preferences` hash still holds every default (the author's requirement), so the backfill on load stays. `activerecord-typedstore` was considered and rejected: it wants all fields declared in one block per column, fixes defaults at class load, and would still need Spree's layer for `parse_on_set`, nullable values, choices and deprecations.
+- **Rows of an uninstalled class are skipped by both upgrade tasks**, with a line naming the class, so one leftover gateway from a removed gem cannot stop the run before the other rows. Their secrets stay readable where they are.
+- **Change detection across both columns:** `previously_changed_preference_names` lists what the last save changed in either column. Stripe uses it to recognise a save that only stored webhook registration results.
+- **`preferences` is the only column the Admin API, exports and event payloads read.** Secrets reach the API only through the existing masking, which now reads through `preferred_*` rather than the raw hash.
+- **Copying a record** (`dup`) carries `secret_preferences` along with `preferences`, so duplicating a payment method keeps its keys.
+- **Which keys are secrets** comes from the class declarations (`declared_preference_types`, recorded per class when a preference is declared, so no record is built). The migration resolves each row's class from its `type` column; a row whose class cannot be loaded (an uninstalled gem) keeps its secrets in `preferences` and is reported, and the safety-net task moves them once the class is back.
+
+### Tiered calculators
+
+```ruby
+# Before: { 100.0 => 10.0, 250.0 => 15.0 }
+preference :tiers, :array, default: []
+# After: [{ "threshold" => "100.0", "value" => "10.0" }, { "threshold" => "250.0", "value" => "15.0" }]
+```
+
+`compute` picks the last tier whose threshold the amount reaches. Validation checks that each entry has a positive threshold and, for the percent calculator, a rate from 0 to 100, and that thresholds are unique. The migration converts the hash for rows whose `type` is either calculator or an application's subclass of one, keeping the decimal-string rule above. The dashboard's tiers editor (`preferences-form.tsx` in `@spree/dashboard-core`) reads and writes the list.
+
+### Afterwards
+
+`yaml_column_permitted_classes` injection is removed from `Spree::Core::Engine` and from the dummy app generator. The comments in `preferable.rb` and `base.rb` that explain YAML behaviour go with it.
+
+## Migration Path
+
+1. Change the tiered calculators to the list shape (code and specs).
+2. Switch `Preferable` to JSON through `Spree::Metadata::HashSerializer`; restore decimals on read; stringify the two key-set comparisons; delete the dead `serialize` on `Spree::Address`.
+3. Add the `secret_preferences` column and routing for `:password` preferences; raise when a secret is declared on a table without it; read masking through the `preferred_*` methods.
+4. Add the `ConvertPreferencesToJson` migration, including the tiers reshaping and moving secrets into `secret_preferences`, with a reversible `down` (JSON back to YAML restores every value through the declared types; tiers convert back to the hash).
+5. Add two steps to the 5.6→6.0 manifest: `spree:upgrade:preferences_json` (safety net: reports and converts any row still holding YAML or a secret left in `preferences`) and `spree:upgrade:encrypt_secret_preferences` (encrypts every plain `secret_preferences` row; reports and skips when no keys are configured). Both idempotent.
+6. Remove the YAML permitted-classes configuration, `Spree::Preference`, `Spree::Preferences::Store` and `Spree::Preferences::ScopedStore`.
+7. Document the change in `docs/developer/upgrades/5.6-to-6.0.mdx` (including that gateway secrets are now encrypted when keys are set, and linking the existing encryption-keys section), list gateway secrets among encrypted values in `docs/developer/deployment/environment_variables.mdx`, and update `docs/developer/customization/model-preferences.mdx` (keys are strings in the raw hash; values come back typed through the `preferred_*` readers). (`docs/developer/core-concepts/promotions.mdx` names the tiered calculators but shows no tiers, so it needed no change.)
+8. Specs: a coder round trip for every declared type, secrets landing only in `secret_preferences` (encrypted with keys, plain without), a moved plain secret staying readable, the encrypt task, the migration on each adapter, the existing preference specs passing unchanged, and the tiered calculator specs moved to the list shape.
+
+## Constraints on Current Work
+
+- **New `preferences` columns are JSON.** Use the `t.respond_to?(:jsonb)` guard from the root CLAUDE.md. Do not add another `text` preferences column.
+- **Read preferences through `preferred_*` or `get_preference`, not the raw hash.** The raw hash is where YAML and JSON differ; the readers hide it.
+- **Do not store symbols, `BigDecimal` or `Time` objects in a preference you expect back unchanged** without a declared type that casts it.
+- **Every secret is a `:password` preference.** That type is what sends a value to the encrypted column. A secret declared `:string`, or nested inside a `:hash` or `:array` preference, stays in plain text.
+- **New gateway or integration tables need a `secret_preferences` `text` column** if their classes declare `:password` preferences.
+- **Never write `preferences` with `update_columns`/`update_all` when it may hold a secret.** Those skip the hook that moves secrets to the encrypted column. Set values through the `preferred_*` writers and write both `preferences` and `secret_preferences`.
+- **No number keys in `:hash` preferences.** JSON turns them into strings. Use a list of objects, as the tiered calculators now do.
+- **Do not query `preferences` in SQL until this lands.** On the two JSON-typed tables the value is a YAML string today and any JSON operator silently matches nothing.
+
+## Open Questions
+
+None blocking. Deferred:
+
+- **A credential lifecycle** (rotation history, last used, expiry, its own permission). Nothing needs it yet; it can be built on `secret_preferences` without changing declarations.
+- **Dropping `spree_gateways`**, the unused 4.3 table. Unblocked by confirming no extension still reads it; a candidate for 6.1 cleanup.
+
+## References
+
+- `docs/plans/decisions.md` — 2026-08-09 metadata consolidation (the migration precedent) and the Stripe port entry naming encryption of `preferences` as a live question
+- `docs/plans/6.0-store-scoped-configuration.md` — store preferences and the backfill-marker wrinkle
+- `docs/developer/customization/model-preferences.mdx`
+- `docs/developer/deployment/environment_variables.mdx` — encryption keys, enabling encryption on an existing installation
+- `spree/core/app/models/spree/user_identity.rb` — the plain-text-then-encrypt precedent
+- `spree/core/lib/spree/core/preferences/preferable.rb`, `preferable_class_methods.rb`
+- `spree/core/db/migrate/20260809120001_consolidate_metadata_columns.rb`
diff --git a/docs/plans/decisions.md b/docs/plans/decisions.md
index c711600d8b..835f87e93a 100644
--- a/docs/plans/decisions.md
+++ b/docs/plans/decisions.md
@@ -7,6 +7,15 @@
**Consequences:** A second request answers 201 with a new session, and the previous one is canceled. Checkout that reloads the payment step opens a new PaymentIntent each time. A buyer who already paid still gets `payment_in_progress` rather than a second charge.
**Plans amended:** `6.0-payment-gateways-monorepo.md` (the one-open-session constraint now says the previous session is canceled, not handed back).
+
+## 2026-10-01: Preferences are stored as JSON, and secrets move to an encrypted column
+
+**Context:** Every `preferences` column is a YAML document written by one line in `Spree::Preferences::Preferable`. YAML loading is an attack surface Rails fences with a permitted-class list Spree has to inject; the columns cannot be queried; and the two 6.0 tables that declared `preferences` as JSON (delivery method rules, commission rules) actually hold a YAML document wrapped in a JSON string, because the shared coder still writes YAML.
+
+**Decision:** JSON replaces YAML for every preferences column, with no Spree-specific coder: the column type does the conversion, and the `metadata` columns' `HashSerializer` lets the loaded hash answer to symbol and string keys; the `preferred_*` readers restore decimals and times from their declared type, and every other value comes back as JSON stores it. Decimals are exact strings. The conversion runs inside the 6.0 migration, reading rows without models, because `spree:upgrade` runs after `db:migrate` and would find the YAML column gone. A row still holding YAML raises when read and the upgrade task converts it; a boot check was rejected because it would block the migration that repairs the data. The tiered promotion calculators move their tiers from a hash with number keys to a list of `{threshold, value}` objects. Preferences declared `:password` are stored in a separate `secret_preferences` column with Rails' `encrypts`, so `preferences` never holds a secret; the declaration DSL does not change. Encryption applies whenever keys are configured, as for Spree's other encrypted fields; new projects have keys from setup. The migration moves existing secrets across in plain text, they stay readable through `support_unencrypted_data`, and an upgrade task encrypts them. The `spree_preferences` key-value table, whose only live row was the installation id, is dropped without copying the id; the default store generates a new one in an internal preference. Rejected: encrypting values inside the JSON by hand, and a polymorphic credential model.
+
+**Consequences:** New `preferences` columns are JSON. Hash preferences must not use number keys. Every secret must be declared `:password`, the only type that is encrypted, and a table whose classes declare one needs a `secret_preferences` column. The tiered calculators' `tiers` value changes shape in the Admin API. See `6.0-json-preferences.md`.
+
## 2026-09-30: A cart keeps one open Stripe intent, and the ones left behind are canceled once the order is placed
**Context:** Two Stripe payment sessions on one cart could both be paid (V-3726). Reproduced on `main`: a $55.00 cart, two `POST /carts/:id/payment_sessions` answering 201 with two $55.00 PaymentIntents, both confirmed through their own client secrets, and order R1001 placed with one $55.00 payment while Stripe held $110.00. Two concurrent requests are not needed, since the endpoint's cart lock already runs them one after the other: `SpreeStripe::Gateway#create_payment_session` opened a new intent on every call. The standalone gem reused the order's intent (`CreatePaymentIntent`, then `CreatePaymentSession`), but only its Rails checkout called that service, the port left the service with no callers, and it was deleted on 2026-08-12. The storefront creates a session every time the payment step loads, and whenever the buyer picks another saved card, so a second tab was enough. Switching payment method had the same result: a Stripe session followed by a Check payment placed order R1002, paid by check, and the abandoned intent could still be confirmed for $55.00 afterwards. Stripe's notification for the extra charge then fails the payment's cap on what is still owed, is reported, and records nothing.
diff --git a/packages/dashboard-core/src/components/preferences-form.tsx b/packages/dashboard-core/src/components/preferences-form.tsx
index 3583ed13e6..250eaa0953 100644
--- a/packages/dashboard-core/src/components/preferences-form.tsx
+++ b/packages/dashboard-core/src/components/preferences-form.tsx
@@ -170,8 +170,8 @@ export function PreferenceField({
}
// `tiers` preference (Spree::Calculator::TieredPercent /
- // TieredFlatRate) — a Hash<threshold, value> that the default
- // hash-as-text input would render as `[object Object]`. Render a row
+ // TieredFlatRate) — a list of `{ threshold, value }` objects that the
+ // default array input would render as `[object Object]`. Render a row
// editor so the merchant can add tier breakpoints directly.
if (field.key === 'tiers') {
return <TiersEditor value={value} onChange={onChange} />
@@ -356,6 +356,11 @@ function isMaxKey(key: string): boolean {
return /(?:^|_)max(?:imum)?(?:_|$)/.test(key)
}
+interface TierValue {
+ threshold: string
+ value: string
+}
+
interface TierRowState {
/** Stable across edits — used for React keys so input focus survives reorder. */
uid: string
@@ -365,21 +370,21 @@ interface TierRowState {
/**
* Editor for `Spree::Calculator::TieredPercent` / `TieredFlatRate`
- * preferences. The underlying shape is `Hash<threshold, value>` — orders
- * at or above `threshold` get `value` (% or $ depending on the
- * calculator).
+ * preferences. The underlying shape is a list of `{ threshold, value }`
+ * objects — orders at or above `threshold` get `value` (% or $ depending
+ * on the calculator).
*
* Owns its row list locally so the user can add an empty row and fill
- * it in afterwards — projecting to the hash on every change would drop
+ * it in afterwards — projecting to the list on every change would drop
* empty-threshold rows the moment they're added. Empty rows are
- * filtered when projecting to the parent's hash value.
+ * filtered when projecting to the parent's value.
*/
function TiersEditor({
value,
onChange,
}: {
value: unknown
- onChange: (next: Record<string, number>) => void
+ onChange: (next: TierValue[]) => void
}) {
const { t } = useTranslation()
const idPrefix = useId()
@@ -392,14 +397,12 @@ function TiersEditor({
function commit(next: TierRowState[]) {
setRows(next)
- const out: Record<string, number> = {}
- for (const row of next) {
- const t = row.threshold.trim()
- if (!t) continue
- const parsed = Number(row.value)
- out[t] = Number.isFinite(parsed) ? parsed : 0
- }
- onChange(out)
+ // Sent as the strings typed, so the server stores the exact decimal.
+ onChange(
+ next
+ .filter((row) => row.threshold.trim())
+ .map((row) => ({ threshold: row.threshold.trim(), value: row.value.trim() || '0' })),
+ )
}
function updateRow(uid: string, patch: Partial<Pick<TierRowState, 'threshold' | 'value'>>) {
@@ -471,6 +474,7 @@ function TierRow({
min={0}
value={row.threshold}
placeholder="100"
+ aria-label={t('admin.components.preferences_form.tiers.header_threshold')}
onChange={(e) => onChange({ threshold: e.target.value })}
/>
<Input
@@ -479,6 +483,7 @@ function TierRow({
min={0}
value={row.value}
placeholder="10"
+ aria-label={t('admin.components.preferences_form.tiers.header_value')}
onChange={(e) => onChange({ value: e.target.value })}
/>
<Button
@@ -495,17 +500,20 @@ function TierRow({
}
/**
- * Normalize whatever shape the server sent — typically `Hash<number,
- * number>` but JSON serialization stringifies the keys, so we may
- * receive `{ "100": 10 }` or `{ 100: 10 }`. Either form converts to
- * `TierRowState[]` for editor state.
+ * Converts the server's list of `{ threshold, value }` objects into
+ * editor rows. A calculator saved before Spree 6.0 that the upgrade has
+ * not converted yet still sends a hash keyed by threshold, so that shape
+ * is read too.
*/
function parseTiers(value: unknown, idPrefix: string): TierRowState[] {
- if (!value || typeof value !== 'object' || Array.isArray(value)) return []
- return Object.entries(value as Record<string, unknown>)
+ if (!value || typeof value !== 'object') return []
+ const pairs: [unknown, unknown][] = Array.isArray(value)
+ ? value.map((tier) => [tier?.threshold, tier?.value])
+ : Object.entries(value as Record<string, unknown>)
+ return pairs
.map(([threshold, v], i) => ({
uid: `${idPrefix}-seed-${i}`,
- threshold: String(threshold),
+ threshold: threshold === null || threshold === undefined ? '' : String(threshold),
value: v === null || v === undefined ? '' : String(v),
}))
.sort((a, b) => Number(a.threshold) - Number(b.threshold))
diff --git a/packages/dashboard-ui/src/spree/calculator-summary.tsx b/packages/dashboard-ui/src/spree/calculator-summary.tsx
index 8a1799f28e..ef192dc72a 100644
--- a/packages/dashboard-ui/src/spree/calculator-summary.tsx
+++ b/packages/dashboard-ui/src/spree/calculator-summary.tsx
@@ -85,6 +85,7 @@ export function formatCalculatorSummary(
* - `*_percent` decimals → `N%`.
* - `currency` strings → omitted (folded into the money formatting).
* - booleans → humanized key when true; skipped when false.
+ * - lists (tiers) → their length; other objects → omitted.
* - everything else → `key: value`.
*/
function formatField(
@@ -105,6 +106,10 @@ function formatField(
if (field.type === 'boolean') {
return value ? humanize(field.key) : null
}
+ if (field.type === 'array') {
+ return Array.isArray(value) && value.length ? `${humanize(field.key)}: ${value.length}` : null
+ }
+ if (field.type === 'hash') return null
return `${humanize(field.key)}: ${value}`
}
diff --git a/packages/dashboard/e2e/promotions.spec.ts b/packages/dashboard/e2e/promotions.spec.ts
index ddcb02d6eb..06096b4e79 100644
--- a/packages/dashboard/e2e/promotions.spec.ts
+++ b/packages/dashboard/e2e/promotions.spec.ts
@@ -67,6 +67,56 @@ async function submitCreate(page: Page, name: string) {
await expect(page.getByRole('heading', { name })).toBeVisible({ timeout: 15_000 })
}
+// Starts a whole-order adjustment on the Tiered Percent calculator and adds
+// one row per `[threshold, value]` pair, in the order given.
+async function addTieredPercentAction(page: Page, tiers: [string, string][]) {
+ await pickAction(page, /^create whole-order adjustment\b/i)
+ const calculatorSelect = page.locator('#calculator-type')
+ await expect(calculatorSelect).toBeEnabled({ timeout: 10_000 })
+ await calculatorSelect.click()
+ await page.getByRole('option', { name: /^tiered percent$/i }).click()
+
+ const dialog = page.getByRole('dialog')
+ for (const [index, [threshold, value]] of tiers.entries()) {
+ await dialog.getByRole('button', { name: /^add tier$/i }).click()
+ await dialog
+ .getByLabel(/^order total/i)
+ .nth(index)
+ .fill(threshold)
+ await dialog
+ .getByLabel(/^discount$/i)
+ .nth(index)
+ .fill(value)
+ }
+}
+
+async function openAdjustmentAction(page: Page, calculator: RegExp) {
+ await page
+ .locator('div.items-stretch')
+ .filter({ hasText: /create whole-order adjustment/i })
+ .first()
+ .getByRole('button')
+ .first()
+ .click()
+ await expect(page.getByRole('heading', { name: /^create whole-order adjustment$/i })).toBeVisible(
+ { timeout: 5_000 },
+ )
+ await expect(page.locator('#calculator-type')).toContainText(calculator, { timeout: 10_000 })
+}
+
+// The server keeps each number as an exact decimal, so 100 reads back as
+// "100.0"; compare the numbers rather than their text.
+const decimal = (n: string) => new RegExp(`^${n}(\\.0+)?$`)
+
+async function expectTiers(page: Page, tiers: [string, string][]) {
+ const dialog = page.getByRole('dialog')
+ await expect(dialog.getByLabel(/^order total/i)).toHaveCount(tiers.length)
+ for (const [index, [threshold, value]] of tiers.entries()) {
+ await expect(dialog.getByLabel(/^order total/i).nth(index)).toHaveValue(decimal(threshold))
+ await expect(dialog.getByLabel(/^discount$/i).nth(index)).toHaveValue(decimal(value))
+ }
+}
+
const PROMOTIONS_PATH = (storeId: string) => `/${storeId}/promotions`
const CTA = /new promotion/i
@@ -337,17 +387,7 @@ test.describe('promotions', () => {
await submitCreate(page, name)
- await page
- .locator('div.items-stretch')
- .filter({ hasText: /create whole-order adjustment/i })
- .first()
- .getByRole('button')
- .first()
- .click()
- await expect(
- page.getByRole('heading', { name: /^create whole-order adjustment$/i }),
- ).toBeVisible({ timeout: 5_000 })
- await expect(calculatorSelect).toContainText(/flat rate/i, { timeout: 10_000 })
+ await openAdjustmentAction(page, /flat rate/i)
})
test('creates a promotion with a Create Adjustment action', async ({ page }) => {
@@ -372,6 +412,81 @@ test.describe('promotions', () => {
await submitCreate(page, name)
})
+ test('saves the tiers of a tiered calculator, lowest threshold first', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, PROMOTIONS_PATH(creds.store_id), CTA)
+
+ const name = `E2E Tiers Promo ${Date.now()}`
+ await startNewPromotion(page, creds.store_id, name)
+
+ // Entered out of order: the saved tiers come back sorted by threshold.
+ await addTieredPercentAction(page, [
+ ['250', '15'],
+ ['100', '10'],
+ ])
+ await saveEditor(page)
+ await expect(page.getByText(/tiered percent · tiers: 2/i).first()).toBeVisible({
+ timeout: 5_000,
+ })
+
+ await submitCreate(page, name)
+
+ await openAdjustmentAction(page, /tiered percent/i)
+ await expectTiers(page, [
+ ['100', '10'],
+ ['250', '15'],
+ ])
+ })
+
+ test('edits and removes tiers on a saved promotion', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, PROMOTIONS_PATH(creds.store_id), CTA)
+
+ const name = `E2E Tiers Edit ${Date.now()}`
+ await startNewPromotion(page, creds.store_id, name)
+ await addTieredPercentAction(page, [
+ ['100', '10'],
+ ['250', '15'],
+ ])
+ await saveEditor(page)
+ await submitCreate(page, name)
+
+ await openAdjustmentAction(page, /tiered percent/i)
+ const dialog = page.getByRole('dialog')
+ await dialog
+ .getByRole('button', { name: /^remove tier$/i })
+ .first()
+ .click()
+ await dialog
+ .getByLabel(/^discount$/i)
+ .first()
+ .fill('20')
+ await saveEditor(page)
+
+ await page.getByRole('button', { name: /^save$/i }).click()
+ await expect(page.getByText(/promotion saved/i).first()).toBeVisible({ timeout: 15_000 })
+
+ // Read back from the server, not from the form's memory.
+ await page.reload()
+ await expect(page.getByRole('heading', { name })).toBeVisible({ timeout: 15_000 })
+ await openAdjustmentAction(page, /tiered percent/i)
+ await expectTiers(page, [['250', '20']])
+ })
+
+ test('refuses a tier discount above 100 percent', async ({ page }) => {
+ const creds = await login(page)
+ await gotoIndex(page, PROMOTIONS_PATH(creds.store_id), CTA)
+
+ const name = `E2E Tiers Invalid ${Date.now()}`
+ await startNewPromotion(page, creds.store_id, name)
+ await addTieredPercentAction(page, [['100', '150']])
+ await saveEditor(page)
+
+ await page.getByRole('button', { name: /^create promotion$/i }).click()
+ await expect(page.getByText(/between 0% and 100%/i).first()).toBeVisible({ timeout: 15_000 })
+ await expect(page.getByRole('heading', { name: /^new promotion$/i })).toBeVisible()
+ })
+
test('creates a promotion with a Create Item Adjustments action', async ({ page }) => {
const creds = await login(page)
await gotoIndex(page, PROMOTIONS_PATH(creds.store_id), CTA)
diff --git a/spree/api/app/serializers/spree/api/v3/admin/store_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/store_serializer.rb
index 16e8d3d3f4..67246c1d53 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/store_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/store_serializer.rb
@@ -93,11 +93,9 @@ module Spree
:preferred_inventory_provider_failure_policy,
:preferred_payout_provider,
:preferred_default_payouts_schedule_interval,
- :preferred_default_minimum_payout_amount,
:preferred_auto_approve_sellers,
:preferred_auto_approve_seller_products,
:preferred_send_seller_transactional_emails,
- :preferred_default_commission_tax_rate,
:preferred_document_number_format,
:preferred_order_number_prefix,
:preferred_order_number_suffix,
@@ -115,6 +113,14 @@ module Spree
Spree::NumberSequence.started?(store: store)
end
+ attribute :preferred_default_minimum_payout_amount do |store|
+ store.preferred_default_minimum_payout_amount&.to_f
+ end
+
+ attribute :preferred_default_commission_tax_rate do |store|
+ store.preferred_default_commission_tax_rate&.to_f
+ end
+
attribute :url, &:storefront_url
# The backend's own public URL — what a headless client sets as its
diff --git a/spree/core/app/models/concerns/spree/calculator/tiers.rb b/spree/core/app/models/concerns/spree/calculator/tiers.rb
new file mode 100644
index 0000000000..67bfaea156
--- /dev/null
+++ b/spree/core/app/models/concerns/spree/calculator/tiers.rb
@@ -0,0 +1,76 @@
+module Spree
+ class Calculator < Spree.base_class
+ # A tier ladder for the tiered calculators: a list of
+ # `{ "threshold" => "100.0", "value" => "10.0" }` objects, where the value
+ # is a percentage or an amount depending on the calculator. A list rather
+ # than a hash keyed by threshold, because JSON object keys are always
+ # strings and a number key would not survive storage.
+ module Tiers
+ extend ActiveSupport::Concern
+
+ included do
+ preference :tiers, :array, default: [], parse_on_set: ->(value) { Tiers.normalize(value) }
+
+ validate :preferred_tiers_content
+ end
+
+ # @param value [Object] tiers as an operator or API client sent them
+ # @return [Object] an array of string-keyed hashes with numbers as exact
+ # decimal strings — the form JSON stores, so a fresh value compares
+ # equal to one read back — or the value unchanged when it is not a list
+ # of hashes, so validation can name the problem
+ def self.normalize(value)
+ return value unless value.is_a?(Array) && value.all? { |tier| tier.respond_to?(:to_h) && !tier.is_a?(Array) }
+
+ value.map do |tier|
+ tier.to_h.stringify_keys.to_h { |key, number| [key, decimal(number)&.as_json || number] }
+ end
+ end
+
+ # @return [BigDecimal, nil] nil when the value is not a number
+ def self.decimal(value)
+ BigDecimal(value.to_s, exception: false)
+ end
+
+ # The value of the highest tier whose threshold the amount reaches.
+ #
+ # @param amount [Numeric]
+ # @return [BigDecimal, nil] nil when the amount reaches no tier
+ def tier_value_for(amount)
+ tier_pairs.reverse_each.detect { |threshold, _| amount >= threshold }&.last
+ end
+
+ private
+
+ # Tiers that are not numbers are skipped: validation refuses them on
+ # save, but an unsaved calculator can still be asked to compute. Tiers
+ # still in the pre-6.0 hash keyed by threshold, left by an upgrade that
+ # could not convert them, keep discounting until they are converted.
+ #
+ # @return [Array<Array(BigDecimal, BigDecimal)>] threshold and value, lowest threshold first
+ def tier_pairs
+ tiers = preferred_tiers
+ pairs = case tiers
+ when Hash then tiers.to_a
+ when Array then tiers.grep(Hash).map { |tier| tier.values_at('threshold', 'value') }
+ else []
+ end
+ pairs.map { |threshold, value| [Tiers.decimal(threshold), Tiers.decimal(value)] }.select(&:all?).sort_by(&:first)
+ end
+
+ def preferred_tiers_content
+ tiers = preferred_tiers
+ unless tiers.is_a?(Array) && tiers.all? { |tier| tier.is_a?(Hash) }
+ return errors.add(:preferred_tiers, :should_be_list)
+ end
+
+ thresholds = tiers.map { |tier| Tiers.decimal(tier['threshold']) }
+ values = tiers.map { |tier| Tiers.decimal(tier['value']) }
+
+ errors.add(:preferred_tiers, :thresholds_should_be_positive_number) unless thresholds.all? { |threshold| threshold&.positive? }
+ errors.add(:preferred_tiers, :thresholds_should_be_unique) unless thresholds.uniq.size == thresholds.size
+ validate_tier_values(values)
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/concerns/spree/preference_schema.rb b/spree/core/app/models/concerns/spree/preference_schema.rb
index fa7664cb5b..2190e73e70 100644
--- a/spree/core/app/models/concerns/spree/preference_schema.rb
+++ b/spree/core/app/models/concerns/spree/preference_schema.rb
@@ -93,17 +93,6 @@ module Spree
.freeze
end
- # Declared order first, so a form reads the way its author grouped the
- # fields; anything the macro did not record (an association writer that
- # looks like a preference) keeps its existing place at the end.
- def ordered_preferences(instance)
- defined = instance.defined_preferences
- return defined unless respond_to?(:declared_preference_order)
-
- declared = declared_preference_order & defined
- declared + (defined - declared)
- end
-
def compute_preference_schema
# Only instantiation is guarded. `new` touches the database to read
# the column list, so it fails whenever the schema is asked for before
@@ -121,7 +110,7 @@ module Spree
return nil
end
- ordered_preferences(instance).filter_map do |pref|
+ instance.defined_preferences.filter_map do |pref|
next if instance.preference_deprecated(pref)
# Written by Spree, not supplied by the operator — a value a
# provider hands back after we register something with it. Offering
diff --git a/spree/core/app/models/concerns/spree/secret_preferences.rb b/spree/core/app/models/concerns/spree/secret_preferences.rb
new file mode 100644
index 0000000000..4bd79f96ac
--- /dev/null
+++ b/spree/core/app/models/concerns/spree/secret_preferences.rb
@@ -0,0 +1,59 @@
+module Spree
+ # Stores a model's `:password` preferences in a separate `secret_preferences`
+ # text column, encrypted with Active Record encryption whenever keys are
+ # configured, so the `preferences` column never holds a secret. Declarations
+ # are unchanged: `preference :secret_key, :password` and
+ # `preferred_secret_key` read and write the encrypted column.
+ #
+ # Values written before encryption was enabled stay readable and are
+ # encrypted on the next save, or at once by
+ # `spree:upgrade:encrypt_secret_preferences`.
+ module SecretPreferences
+ extend ActiveSupport::Concern
+
+ included do
+ serialize :secret_preferences, coder: TextHashSerializer
+
+ if ActiveRecord::Encryption.config.has_primary_key?
+ encrypts :secret_preferences, support_unencrypted_data: true
+ end
+
+ before_save :move_secret_preferences
+ end
+
+ # The column is `text`, since ciphertext cannot live in a JSON column. An
+ # empty column stays nil rather than `{}`: encrypted attributes compare the
+ # stored value with the loaded one, so `{}` would mark every record without
+ # secrets as changed.
+ class TextHashSerializer
+ def self.dump(hash)
+ ActiveSupport::JSON.encode(hash) unless hash.nil?
+ end
+
+ def self.load(json)
+ Spree::Metadata::HashSerializer.load(ActiveSupport::JSON.decode(json)) unless json.nil?
+ end
+ end
+
+ # Both columns as they should be stored, when `preferences` holds a secret
+ # — one assigned as part of a whole hash, or one the upgrade has not moved
+ # yet. A secret found there is the newer value and wins.
+ #
+ # @return [Hash{Symbol => Hash}, nil] `preferences:` and `secret_preferences:`, or nil when there is nothing to move
+ def separated_secret_preferences
+ secrets = (preferences || {}).slice(*self.class.secret_preference_names)
+ return if secrets.empty?
+
+ { preferences: preferences.except(*secrets.keys), secret_preferences: (secret_preferences || {}).merge(secrets) }
+ end
+
+ private
+
+ # Keeps a secret out of the plain `preferences` column when the record is
+ # saved.
+ def move_secret_preferences
+ separated = separated_secret_preferences
+ assign_attributes(separated) if separated
+ end
+ end
+end
diff --git a/spree/core/app/models/concerns/spree/typed_associations.rb b/spree/core/app/models/concerns/spree/typed_associations.rb
index 31051e5e85..0600673fc4 100644
--- a/spree/core/app/models/concerns/spree/typed_associations.rb
+++ b/spree/core/app/models/concerns/spree/typed_associations.rb
@@ -93,6 +93,12 @@ module Spree
record.save! if record.changed?
record.id
+ rescue ActiveRecord::RecordInvalid => e
+ # A row saved from the owner's `after_save` fails inside the owner's own
+ # save, and a plain `save` swallows the exception — so its errors are
+ # copied onto the owner, or the caller learns only that something failed.
+ e.record.errors.full_messages.each { |message| errors.add(:base, message) }
+ raise
end
# Decode `*_ids` array preferences (`customer_group_ids`, `user_ids`,
diff --git a/spree/core/app/models/spree/address.rb b/spree/core/app/models/spree/address.rb
index 8d83325a50..731d657d5b 100644
--- a/spree/core/app/models/spree/address.rb
+++ b/spree/core/app/models/spree/address.rb
@@ -7,7 +7,7 @@ module Spree
include Spree::HasCustomFields
include Spree::Metadata
- serialize :preferences, type: Hash, coder: YAML, default: {}
+ serialize :preferences, coder: Spree::Metadata::HashSerializer, default: {}
NO_ZIPCODE_ISO_CODES ||= [
'AO', 'AG', 'AW', 'BS', 'BZ', 'BJ', 'BM', 'BO', 'BW', 'BF', 'BI', 'CM', 'CF', 'KM', 'CG',
diff --git a/spree/core/app/models/spree/base.rb b/spree/core/app/models/spree/base.rb
index e3806d8a21..411de00cc0 100644
--- a/spree/core/app/models/spree/base.rb
+++ b/spree/core/app/models/spree/base.rb
@@ -31,16 +31,12 @@ class Spree::Base < ApplicationRecord
class_attribute :additional_seller_permitted_attributes, instance_writer: false, default: [].freeze
# Backfills preferences added to the class after this row was last saved, so
- # a reader never sees nil for a newly defined preference. Assigning
- # unconditionally would dirty every record on load: `preferences` is a
- # YAML-serialized Hash, so the dirty check compares the serialized string and
- # the merge reorders keys even when nothing changed — which is enough to make
- # `with_lock` refuse the record ("unpersisted changes").
+ # a reader never sees nil for a newly defined preference. Only missing keys
+ # are assigned: assigning unconditionally would dirty every record on load,
+ # which is enough to make `with_lock` refuse the record ("unpersisted
+ # changes").
after_initialize do
- if has_attribute?(:preferences) && !preferences.nil?
- missing = default_preferences.except(*preferences.keys)
- self.preferences = preferences.merge(missing) if missing.any?
- end
+ backfill_default_preferences if has_attribute?(:preferences) && !preferences.nil?
end
# only for backwards compatibility with Kaminari
diff --git a/spree/core/app/models/spree/calculator/tiered_flat_rate.rb b/spree/core/app/models/spree/calculator/tiered_flat_rate.rb
index 8873d4fb35..73c0557a43 100644
--- a/spree/core/app/models/spree/calculator/tiered_flat_rate.rb
+++ b/spree/core/app/models/spree/calculator/tiered_flat_rate.rb
@@ -1,17 +1,9 @@
module Spree
class Calculator::TieredFlatRate < Calculator
+ include Spree::Calculator::Tiers
+
preference :base_amount, :decimal, default: 0
preference :currency, :string, default: -> { Spree::Store.default.default_currency }
- preference :tiers, :hash, default: {}
-
- before_validation do
- # Convert tier values to decimals. Strings don't do us much good.
- if preferred_tiers.is_a?(Hash)
- self.preferred_tiers = Hash[*preferred_tiers.flatten.map(&:to_f)]
- end
- end
-
- validate :preferred_tiers_content
def self.description
Spree.t(:tiered_flat_rate)
@@ -21,20 +13,13 @@ module Spree
return 0 unless object&.currency.present?
return 0 unless preferred_currency.casecmp(object.currency.upcase).zero?
- base, amount = preferred_tiers.sort.reverse.detect { |b, _| object.amount >= b }
- amount || preferred_base_amount
+ tier_value_for(object.amount) || preferred_base_amount
end
private
- def preferred_tiers_content
- if preferred_tiers.is_a? Hash
- unless preferred_tiers.keys.all? { |k| k.is_a?(Numeric) && k > 0 }
- errors.add(:base, :keys_should_be_positive_number)
- end
- else
- errors.add(:preferred_tiers, :should_be_hash)
- end
+ def validate_tier_values(values)
+ errors.add(:preferred_tiers, :values_should_be_number) unless values.all? { |value| value && value >= 0 }
end
end
end
diff --git a/spree/core/app/models/spree/calculator/tiered_percent.rb b/spree/core/app/models/spree/calculator/tiered_percent.rb
index 528ee32e66..3e86b0e72b 100644
--- a/spree/core/app/models/spree/calculator/tiered_percent.rb
+++ b/spree/core/app/models/spree/calculator/tiered_percent.rb
@@ -1,43 +1,27 @@
module Spree
class Calculator::TieredPercent < Calculator
- preference :base_percent, :decimal, default: 0
- preference :tiers, :hash, default: {}
+ include Spree::Calculator::Tiers
- before_validation do
- # Convert tier values to decimals. Strings don't do us much good.
- if preferred_tiers.is_a?(Hash)
- self.preferred_tiers = Hash[*preferred_tiers.flatten.map(&:to_f)]
- end
- end
+ preference :base_percent, :decimal, default: 0
validates :preferred_base_percent, numericality: {
greater_than_or_equal_to: 0,
less_than_or_equal_to: 100
}
- validate :preferred_tiers_content
def self.description
Spree.t(:tiered_percent)
end
def compute(object)
- base, percent = preferred_tiers.sort.reverse.detect { |b, _| object.amount >= b }
- (object.amount * (percent || preferred_base_percent) / 100).round(2)
+ percent = tier_value_for(object.amount) || preferred_base_percent
+ (object.amount * percent / 100).round(2)
end
private
- def preferred_tiers_content
- if preferred_tiers.is_a? Hash
- unless preferred_tiers.keys.all? { |k| k.is_a?(Numeric) && k > 0 }
- errors.add(:base, :keys_should_be_positive_number)
- end
- unless preferred_tiers.values.all? { |k| k.is_a?(Numeric) && k >= 0 && k <= 100 }
- errors.add(:base, :values_should_be_percent)
- end
- else
- errors.add(:preferred_tiers, :should_be_hash)
- end
+ def validate_tier_values(values)
+ errors.add(:preferred_tiers, :values_should_be_percent) unless values.all? { |value| value&.between?(0, 100) }
end
end
end
diff --git a/spree/core/app/models/spree/gateway.rb b/spree/core/app/models/spree/gateway.rb
index 9dcdf5becb..4c0b97f2a4 100644
--- a/spree/core/app/models/spree/gateway.rb
+++ b/spree/core/app/models/spree/gateway.rb
@@ -49,7 +49,7 @@ module Spree
end
def options
- preferences.each_with_object({}) { |(key, value), memo| memo[key.to_sym] = value; }
+ preference_values.symbolize_keys
end
def method_missing(method, *args)
diff --git a/spree/core/app/models/spree/integration.rb b/spree/core/app/models/spree/integration.rb
index 5731e96834..d81177fd8e 100644
--- a/spree/core/app/models/spree/integration.rb
+++ b/spree/core/app/models/spree/integration.rb
@@ -10,8 +10,10 @@ module Spree
include Spree::SingleStoreResource
include Spree::PreferenceSchema
+ include Spree::SecretPreferences
include Spree::ProviderListing
+
# Spree::Current.integrations snapshots the active set for the request;
# connecting or deactivating one mid-request (the activate-and-verify
# flow does) must not leave later reads serving the stale snapshot.
diff --git a/spree/core/app/models/spree/payment_method.rb b/spree/core/app/models/spree/payment_method.rb
index 914db02dfb..0548fd787b 100644
--- a/spree/core/app/models/spree/payment_method.rb
+++ b/spree/core/app/models/spree/payment_method.rb
@@ -13,6 +13,7 @@ module Spree
include Spree::StorePreferences
include Spree::HasCustomFields
include Spree::Metadata
+ include Spree::SecretPreferences
include Spree::CaptureMethod
include Spree::ProviderListing
if defined?(Spree::Security::PaymentMethods)
@@ -303,9 +304,7 @@ module Spree
end
def public_preferences
- public_preference_keys.each_with_object({}) do |key, hash|
- hash[key] = preferences[key]
- end
+ public_preference_keys.index_with { |key| get_preference(key) }
end
# @deprecated Use {#storefront_visible?}; removed in 6.1.
diff --git a/spree/core/app/models/spree/preference.rb b/spree/core/app/models/spree/preference.rb
deleted file mode 100644
index 9bf1042ec8..0000000000
--- a/spree/core/app/models/spree/preference.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-class Spree::Preference < Spree.base_class
- serialize :value, coder: YAML
-
- validates :key, presence: true,
- uniqueness: { case_sensitive: false, allow_blank: true, scope: spree_base_uniqueness_scope }
-
- if defined?(Spree::Security::Preferences)
- include Spree::Security::Preferences
- end
-end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index d6de57418c..1aab48bcd0 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -45,6 +45,9 @@ module Spree
preference :timezone, :string, default: Time.zone.name
preference :weight_unit, :string, default: 'lb'
preference :unit_system, :string, default: 'imperial'
+ # Anonymous identifier of this installation, kept on the default store
+ # (see Spree.install_id). Written by Spree, never by the operator.
+ preference :install_id, :string, internal: true, nullable: true
# email preferences
preference :send_consumer_transactional_emails, :boolean, default: true
# Sellers are a different audience from shoppers, with their own reasons to
@@ -384,6 +387,7 @@ module Spree
before_validation :set_default_code, on: :create
before_validation :normalize_preferred_storefront_url
before_save :ensure_default_exists_and_is_unique
+ before_save :set_install_id
after_create :create_default_policies
after_create :create_default_delivery_profile
@@ -714,6 +718,10 @@ module Spree
I18n.t(key, locale: locale, default: I18n.t(key, locale: :en))
end
+ def set_install_id
+ self.preferred_install_id = SecureRandom.uuid if preferred_install_id.blank?
+ end
+
def ensure_default_exists_and_is_unique
if default
Spree::Store.where.not(id: id).update_all(default: false)
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 2cf01b17a0..d899259b23 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -257,17 +257,18 @@ en:
must_be_origin_only: must be an origin (scheme and host) without path, query, or fragment
spree/calculator/tiered_flat_rate:
attributes:
- base:
- keys_should_be_positive_number: Tier keys should all be numbers larger than 0
preferred_tiers:
- should_be_hash: should be a hash
+ should_be_list: should be a list of tiers, each with a threshold and a value
+ thresholds_should_be_positive_number: should all have a threshold larger than 0
+ thresholds_should_be_unique: should not repeat a threshold
+ values_should_be_number: should all have a value of 0 or more
spree/calculator/tiered_percent:
attributes:
- base:
- keys_should_be_positive_number: Tier keys should all be numbers larger than 0
- values_should_be_percent: Tier values should all be percentages between 0% and 100%
preferred_tiers:
- should_be_hash: should be a hash
+ should_be_list: should be a list of tiers, each with a threshold and a value
+ thresholds_should_be_positive_number: should all have a threshold larger than 0
+ thresholds_should_be_unique: should not repeat a threshold
+ values_should_be_percent: should all have a value between 0% and 100%
spree/catalog:
attributes:
base:
diff --git a/spree/core/db/migrate/20261001000001_convert_preferences_to_json.rb b/spree/core/db/migrate/20261001000001_convert_preferences_to_json.rb
new file mode 100644
index 0000000000..e56547df46
--- /dev/null
+++ b/spree/core/db/migrate/20261001000001_convert_preferences_to_json.rb
@@ -0,0 +1,166 @@
+class ConvertPreferencesToJson < ActiveRecord::Migration[8.1]
+ # The tables whose classes declare `:password` preferences. Their secrets move
+ # to an encrypted `secret_preferences` column (see Spree::SecretPreferences).
+ SECRET_TABLES = %i[spree_payment_methods spree_integrations].freeze
+
+ # Records which tables had their `text` column replaced, so `down` restores
+ # `text` only there — a table whose column was already JSON (delivery method
+ # rules, commission rules) keeps it. Dropped on the way back down.
+ CONVERTED_TABLES_REGISTRY = :spree_converted_preference_tables
+
+ # The conversion lives here rather than in the upgrade rake task because the
+ # manifest runs AFTER db:migrate — a task scheduled there would find the YAML
+ # column already replaced. Secrets are moved in plain text, so this needs no
+ # encryption keys; they stay readable and `spree:upgrade:encrypt_secret_preferences`
+ # encrypts them.
+ def up
+ SECRET_TABLES.each do |table|
+ next unless table_exists?(table)
+
+ add_column table, :secret_preferences, :text unless column_exists?(table, :secret_preferences)
+ end
+
+ create_table CONVERTED_TABLES_REGISTRY, if_not_exists: true do |t|
+ t.string :table_name, null: false
+ end
+
+ conversion.preference_tables.each do |table|
+ if conversion.text_column?(table)
+ conversion.convert_text_table(table)
+ execute(Arel::InsertManager.new.tap do |insert|
+ registry = Arel::Table.new(CONVERTED_TABLES_REGISTRY)
+ insert.into(registry).insert([[registry[:table_name], table]])
+ end.to_sql)
+ else
+ conversion.convert_table(table)
+ end
+ end
+
+ drop_table :spree_preferences, if_exists: true
+ end
+
+ # Restores YAML: a `text` column where `up` replaced one, and a YAML document
+ # inside a JSON string where the column was already JSON, as before the
+ # upgrade. Secrets go back into `preferences` — decrypted, so rolling back an
+ # encrypted secret needs the encryption keys — tiers become a hash keyed by
+ # threshold again, and decimals return as BigDecimal for every row whose class
+ # can be resolved; anything else comes back as the string JSON held.
+ def down
+ restore = Restore.new(connection, conversion)
+ create_table :spree_preferences, if_not_exists: true do |t|
+ t.text :value
+ t.string :key
+ t.timestamps
+ t.index :key, unique: true
+ end
+
+ converted = converted_tables
+ conversion.preference_tables.each do |table|
+ if converted.include?(table)
+ add_column table, :preferences_yaml, :text
+ restore.table(table, target: 'preferences_yaml')
+ remove_column table, :preferences
+ rename_column table, :preferences_yaml, :preferences
+ else
+ restore.table(table, target: 'preferences', inside_json: true)
+ end
+ end
+
+ drop_table CONVERTED_TABLES_REGISTRY, if_exists: true
+
+ SECRET_TABLES.each do |table|
+ remove_column table, :secret_preferences if table_exists?(table) && column_exists?(table, :secret_preferences)
+ end
+ end
+
+ private
+
+ def converted_tables
+ return [] unless table_exists?(CONVERTED_TABLES_REGISTRY)
+
+ registry = Arel::Table.new(CONVERTED_TABLES_REGISTRY)
+ connection.select_values(registry.project(registry[:table_name]))
+ end
+
+ def conversion
+ @conversion ||= Spree::Preferences::JsonConversion.new(connection, log: ->(message) { say(message, true) })
+ end
+
+ class Restore
+ def initialize(connection, conversion)
+ @connection = connection
+ @conversion = conversion
+ end
+
+ def table(table, target:, inside_json: false)
+ typed = connection.column_exists?(table, :type)
+ secrets = connection.column_exists?(table, :secret_preferences)
+ columns = ['id', 'preferences', ('type' if typed), ('secret_preferences' if secrets)].compact
+
+ arel_table = Arel::Table.new(table)
+ connection.select_all(arel_table.project(*columns.map { |column| arel_table[column] })).each do |row|
+ next if row['preferences'].nil? && row['secret_preferences'].nil?
+
+ preferences = decode(row['preferences']) || {}
+ preferences.merge!(decode(decrypt(row['secret_preferences'])) || {}) if secrets
+ model = typed ? conversion.model_for(row['type']) : nil
+ restore_tiers(preferences, model)
+ restore_typed_values(preferences, model)
+
+ yaml = YAML.dump(preferences.transform_keys(&:to_sym))
+ conversion.write(table, row['id'], target => inside_json ? JSON.generate(yaml) : yaml)
+ end
+ end
+
+ private
+
+ attr_reader :connection, :conversion
+
+ def decode(raw)
+ raw.is_a?(String) ? JSON.parse(raw) : raw
+ end
+
+ # A value written before encryption was enabled is plain JSON and comes
+ # back as it is. An encrypted one that cannot be decrypted stops the
+ # rollback: writing its ciphertext into `preferences` would replace a
+ # gateway's keys with garbage.
+ def decrypt(raw)
+ return raw if raw.nil? || !encrypted?(raw)
+
+ unless ActiveRecord::Encryption.config.has_primary_key?
+ raise ActiveRecord::IrreversibleMigration, 'Encrypted secret_preferences cannot be rolled back without the Active Record encryption keys. Configure them and run the rollback again.'
+ end
+
+ ActiveRecord::Encryption.encryptor.decrypt(raw)
+ rescue ActiveRecord::Encryption::Errors::Decryption
+ raise ActiveRecord::IrreversibleMigration, 'An encrypted secret_preferences value could not be decrypted with the configured keys. Restore the keys it was encrypted with and run the rollback again.'
+ end
+
+ def encrypted?(raw)
+ parsed = JSON.parse(raw)
+ parsed.is_a?(Hash) && parsed.key?('p') && parsed.key?('h')
+ rescue JSON::ParserError
+ false
+ end
+
+ def restore_tiers(preferences, model)
+ tiers = preferences['tiers']
+ return unless tiers.is_a?(Array) && Spree::Preferences::JsonConversion.tiered_calculator?(model)
+
+ # A value that is not a number is kept as written, so two such tiers
+ # cannot collapse into one nil key.
+ preferences['tiers'] = tiers.to_h do |tier|
+ [Spree::Calculator::Tiers.decimal(tier['threshold']) || tier['threshold'],
+ Spree::Calculator::Tiers.decimal(tier['value']) || tier['value']]
+ end
+ end
+
+ def restore_typed_values(preferences, model)
+ return unless model.respond_to?(:declared_preference_types)
+
+ model.declared_preference_types.each do |name, type|
+ preferences[name.to_s] = Spree::Preferences::Preferable.restore_value(preferences[name.to_s], type) if preferences.key?(name.to_s)
+ end
+ end
+ end
+end
diff --git a/spree/core/lib/generators/spree/dummy/dummy_generator.rb b/spree/core/lib/generators/spree/dummy/dummy_generator.rb
index 19bbcbf8a6..4b1a1740f6 100644
--- a/spree/core/lib/generators/spree/dummy/dummy_generator.rb
+++ b/spree/core/lib/generators/spree/dummy/dummy_generator.rb
@@ -63,7 +63,6 @@ module Spree
puts "Rails app generator args: #{args.inspect}"
Rails::Generators.invoke('app', args)
- inject_yaml_permitted_classes
end
def test_dummy_config
@@ -96,14 +95,6 @@ module Spree
protected
- def inject_yaml_permitted_classes
- inside dummy_path do
- inject_into_file 'config/application.rb', %Q[
- config.active_record.yaml_column_permitted_classes = [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone, Time]
- ], after: /config\.load_defaults.*$/, verbose: true
- end
- end
-
def dummy_path
ENV['DUMMY_PATH'] || 'spec/dummy'
end
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index 3208eacd58..70cc26c4f2 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -221,20 +221,12 @@ module Spree
->(locale) { locale.name == Spree::Current.content_locale }
end
- # Stable anonymous identifier for this Spree installation. Generated once,
- # persisted in the preferences store and reused afterwards. It identifies the
- # installation only
+ # Stable anonymous identifier for this Spree installation, kept on the
+ # default store, which generates it when it is first saved.
#
- # Deliberately not memoized: the persisted preference is the single source
- # of truth, so processes that race to generate the first value converge on
- # the winning row at the next read instead of each holding a different id
- # for their lifetime.
- #
- # @return [String] UUID
+ # @return [String, nil] UUID, or nil before the default store exists
def self.install_id
- store = Spree::Preferences::Store.instance
- store.get('spree/install_id') { nil }.presence ||
- SecureRandom.uuid.tap { |id| store.set('spree/install_id', id) }
+ Spree::Store.default&.preferred_install_id
end
# Used to configure Spree.
@@ -639,9 +631,9 @@ require 'spree/checkout/requirements'
require 'spree/core/controller_helpers/store'
-require 'spree/core/preferences/store'
require 'spree/core/preferences/runtime_configuration'
require 'spree/core/preferences/masking'
+require 'spree/core/preferences/json_conversion'
require 'spree/core/permission_configuration'
require 'spree/core/ransack_configuration'
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index b13a9be2e5..0cbb0dbdb6 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -83,8 +83,6 @@ module Spree
app.config.spree.promotions = PromoEnvironment.new([])
app.config.spree.pricing = PricingEnvironment.new([])
- app.config.active_record.yaml_column_permitted_classes ||= []
- app.config.active_record.yaml_column_permitted_classes.concat([Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone, Time])
Spree::Config = app.config.spree.preferences
Spree::Dependencies = app.config.spree.dependencies
Spree::Deprecation = ActiveSupport::Deprecation.new('6.0', 'Spree')
diff --git a/spree/core/lib/spree/core/preferences/json_conversion.rb b/spree/core/lib/spree/core/preferences/json_conversion.rb
new file mode 100644
index 0000000000..a344a20575
--- /dev/null
+++ b/spree/core/lib/spree/core/preferences/json_conversion.rb
@@ -0,0 +1,182 @@
+
+module Spree
+ module Preferences
+ # Converts preferences stored as YAML to JSON, for the 6.0 migration and the
+ # `spree:upgrade:preferences_json` safety net.
+ #
+ # Rows are read and written through the connection, never through models:
+ # a model would load the column through whichever coder the current code
+ # declares, which is the thing being converted. The only thing asked of a
+ # model class is which of its preferences are secrets, from its
+ # declarations alone.
+ class JsonConversion
+ PERMITTED_CLASSES = [
+ Symbol, BigDecimal, Date, Time,
+ ActiveSupport::TimeWithZone, ActiveSupport::TimeZone, ActiveSupport::HashWithIndifferentAccess
+ ].freeze
+
+ class UnreadableRowError < StandardError; end
+
+ # Whether a row's class keeps a tier ladder, which moves from a hash keyed
+ # by threshold to a list, because a JSON object key is always a string.
+ #
+ # @param model [Class, nil] the class the row's `type` names
+ # @return [Boolean]
+ def self.tiered_calculator?(model)
+ model.is_a?(Class) && model.include?(Spree::Calculator::Tiers)
+ end
+
+ # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
+ # @param log [#call, nil] receives a line for each row left as it was
+ def initialize(connection, log: nil)
+ @connection = connection
+ @log = log || ->(_message) {}
+ @models = Hash.new { |models, type| models[type] = type.presence&.safe_constantize }
+ end
+
+ # Resolves a row's `type`, once per type for the life of the conversion.
+ #
+ # @param type [String, nil]
+ # @return [Class, nil]
+ def model_for(type)
+ @models[type]
+ end
+
+ # @param table [String]
+ # @param id [Object] the row's primary key
+ # @param assignments [Hash{String => String}] column names and the values to write
+ def write(table, id, assignments)
+ arel_table = Arel::Table.new(table)
+ update = Arel::UpdateManager.new(arel_table).
+ set(assignments.map { |column, value| [arel_table[column], value] }).
+ where(arel_table[:id].eq(id))
+ connection.update(update)
+ end
+
+ # Tables with a `preferences` column that a model storing Spree
+ # preferences reads — Spree's, an extension's, or the application's own.
+ # A table another library owns is left alone.
+ #
+ # @return [Array<String>]
+ def preference_tables
+ Rails.application.eager_load! if defined?(Rails.application) && Rails.application
+ owned = ActiveRecord::Base.descendants.filter_map do |model|
+ model.table_name if !model.abstract_class? && model.include?(Spree::Preferences::Preferable) && model.table_name
+ end
+
+ connection.tables.select { |table| owned.include?(table) && connection.column_exists?(table, :preferences) }.sort
+ end
+
+ # Whether the table's `preferences` column is still `text` and needs a
+ # JSON column in its place.
+ #
+ # @param table [String]
+ # @return [Boolean]
+ def text_column?(table)
+ connection.columns(table).find { |column| column.name == 'preferences' }&.type == :text
+ end
+
+ # Replaces a `text` preferences column with a JSON one (`jsonb` where the
+ # adapter has it), converting every row on the way.
+ #
+ # @param table [String]
+ # @return [Integer] the number of rows written
+ def convert_text_table(table)
+ type = connection.native_database_types.key?(:jsonb) ? :jsonb : :json
+ # Left behind by an earlier run that stopped on an unreadable row, on a
+ # database that cannot roll back schema changes.
+ connection.add_column(table, :preferences_json, type) unless connection.column_exists?(table, :preferences_json)
+ converted = convert_table(table, target: 'preferences_json')
+ connection.remove_column(table, :preferences)
+ connection.rename_column(table, :preferences_json, :preferences)
+ converted
+ end
+
+ # Converts every row of `table` whose `source` column holds YAML (as text,
+ # or wrapped in a JSON string) and writes the JSON to `target`. Secrets
+ # move to `secret_preferences` when the table has that column, and tier
+ # ladders change shape.
+ #
+ # @param table [String]
+ # @param source [String] the column to read
+ # @param target [String] the column to write, the same one for an in-place conversion
+ # @return [Integer] the number of rows written
+ def convert_table(table, source: 'preferences', target: 'preferences')
+ typed = connection.column_exists?(table, :type)
+ secrets_column = connection.column_exists?(table, :secret_preferences)
+ rows = rows_with(table, source, ['id', source, ('type' if typed)].compact)
+
+ rows.count do |row|
+ preferences, already_json = read(row[source], table, row['id'])
+ type = row['type'] if typed
+ # A tiered calculator whose class was not loaded when the migration
+ # ran keeps its hash, so a JSON row is still reshaped when it has one.
+ reshaped = reshape_tiers(preferences, model_for(type))
+ next false if already_json && source == target && !reshaped
+
+ secrets = secrets_column ? extract_secrets(preferences, type, table, row['id']) : {}
+
+ assignments = { target => JSON.generate(preferences) }
+ assignments['secret_preferences'] = JSON.generate(secrets) if secrets.any?
+ write(table, row['id'], assignments)
+ true
+ end
+ end
+
+ private
+
+ attr_reader :connection, :log
+
+ # @return [Array(Hash, Boolean)] the preferences, and whether they were already JSON
+ def read(raw, table, id)
+ decoded = raw.is_a?(String) ? decode_json(raw) : raw
+ return [decoded.to_h, true] if decoded.is_a?(Hash)
+
+ yaml = decoded.is_a?(String) ? decoded : raw
+ value = parse_yaml(yaml, table, id) || {}
+ raise UnreadableRowError, "#{table} row #{id}: preferences are not a hash" unless value.is_a?(Hash)
+
+ [value.as_json, false]
+ end
+
+ def decode_json(raw)
+ JSON.parse(raw)
+ rescue JSON::ParserError
+ nil
+ end
+
+ def parse_yaml(text, table, id)
+ YAML.safe_load(text.to_s, permitted_classes: PERMITTED_CLASSES, aliases: true)
+ rescue Psych::Exception => e
+ raise UnreadableRowError, "#{table} row #{id}: #{e.message}. Fix or clear this row, then run the migration again."
+ end
+
+ def reshape_tiers(preferences, model)
+ tiers = preferences['tiers']
+ return unless tiers.is_a?(Hash) && self.class.tiered_calculator?(model)
+
+ preferences['tiers'] = tiers.map { |threshold, value| { 'threshold' => threshold.to_s, 'value' => value.to_s } }.
+ sort_by { |tier| Spree::Calculator::Tiers.decimal(tier['threshold']) || 0 }
+ end
+
+ def extract_secrets(preferences, type, table, id)
+ model = model_for(type)
+ unless model.respond_to?(:secret_preference_names)
+ log.call("#{table} row #{id}: class #{type.inspect} is not loaded, so its secrets stay in preferences until it is")
+ return {}
+ end
+
+ names = model.secret_preference_names.map(&:to_s)
+ secrets = preferences.slice(*names)
+ names.each { |name| preferences.delete(name) }
+ secrets
+ end
+
+ # The given columns of every row whose `present` column is not NULL.
+ def rows_with(table, present, columns)
+ arel_table = Arel::Table.new(table)
+ connection.select_all(arel_table.project(*columns.map { |column| arel_table[column] }).where(arel_table[present].not_eq(nil)))
+ end
+ end
+ end
+end
diff --git a/spree/core/lib/spree/core/preferences/masking.rb b/spree/core/lib/spree/core/preferences/masking.rb
index e05092ceda..0c48d14dc6 100644
--- a/spree/core/lib/spree/core/preferences/masking.rb
+++ b/spree/core/lib/spree/core/preferences/masking.rb
@@ -60,7 +60,10 @@ module Spree
return {} if preferable.nil?
preferable.preference_schema.each_with_object({}) do |field, hash|
- value = preferable.preferences[field[:key]]
+ # The stored value only, never the default — an unset secret must not
+ # reveal what it would fall back to — read through the record,
+ # because a secret lives in its own column.
+ value = preferable.stored_preference(field[:key]) { nil }
hash[field[:key_string] || field[:key].to_s] = field[:type] == :password ? mask(value) : value
end
end
diff --git a/spree/core/lib/spree/core/preferences/preferable.rb b/spree/core/lib/spree/core/preferences/preferable.rb
index d80f3e7ec5..e7870a77fd 100644
--- a/spree/core/lib/spree/core/preferences/preferable.rb
+++ b/spree/core/lib/spree/core/preferences/preferable.rb
@@ -1,36 +1,17 @@
-# Preferable allows defining preference accessor methods.
+# Typed model preferences, each a Rails store accessor on the `preferences`
+# JSON column — `secret_preferences` for a `:password` one (see
+# {Spree::SecretPreferences}). Included by `Spree::Base`.
#
-# A class including Preferable must implement #preferences which should return
-# an object responding to .fetch(key), []=(key, val), and .delete(key).
-#
-# The generated writer method performs typecasting before assignment into the
-# preferences object.
-#
-# Examples:
-#
-# # Spree::Base includes Preferable and defines preferences as a serialized
-# # column.
# class Settings < Spree::Base
# preference :color, :string, default: 'red'
# preference :temperature, :integer, default: 21
# end
#
# s = Settings.new
-# s.preferred_color # => 'red'
-# s.preferred_temperature # => 21
-#
-# s.preferred_color = 'blue'
-# s.preferred_color # => 'blue'
-#
-# # Typecasting is performed on assignment
-# s.preferred_temperature = '24'
-# s.preferred_temperature # => 24
-#
-# # Modifications have been made to the .preferences hash
-# s.preferences #=> {color: 'blue', temperature: 24}
-#
-# # Save the changes. All handled by activerecord
-# s.save!
+# s.preferred_color # => 'red'
+# s.preferred_temperature = '24' # cast on assignment
+# s.preferred_temperature # => 24
+# s.preferences # => { 'color' => 'red', 'temperature' => 24 }
require 'spree/core/preferences/preferable_class_methods'
@@ -38,47 +19,56 @@ module Spree::Preferences::Preferable
extend ActiveSupport::Concern
included do
- serialize :preferences, type: Hash, coder: YAML if defined?(serialize)
+ serialize :preferences, coder: Spree::Metadata::HashSerializer
extend Spree::Preferences::PreferableClassMethods
end
+ # JSON keeps decimals and times as strings; the declared type turns them
+ # back into a BigDecimal and a time. Anything else, or a string that does
+ # not parse, comes back as stored.
+ #
+ # @param value [Object] a value as JSON stores it
+ # @param type [Symbol] the preference's declared type
+ # @return [Object]
+ def self.restore_value(value, type)
+ return value unless value.is_a?(String)
+
+ case type
+ when :decimal then BigDecimal(value, exception: false) || value
+ when :datetime then Time.zone.iso8601(value)
+ else value
+ end
+ rescue ArgumentError
+ value
+ end
+
def get_preference(name)
has_preference! name
- send self.class.preference_getter_method(name)
+ public_send(:"preferred_#{name}")
end
def set_preference(name, value)
has_preference! name
- send self.class.preference_setter_method(name), value
+ public_send(:"preferred_#{name}=", value)
end
def preference_type(name)
- has_preference! name
- send self.class.preference_type_getter_method(name)
+ preference_definition(name)[:type]
end
def preference_default(name)
- has_preference! name
- send self.class.preference_default_getter_method(name)
+ instance_exec(&preference_definition(name)[:default])
end
def preference_deprecated(name)
- has_preference! name
- send(self.class.preference_deprecated_getter_method(name))
+ preference_definition(name)[:deprecated]
end
# Whether this preference is written by Spree rather than supplied by the
# operator — a webhook secret a provider issues, say. Kept out of the
# schema, so nothing offers it as a field to fill in.
def preference_internal(name)
- has_preference! name
- getter = self.class.preference_internal_getter_method(name)
- # A preference declared before this option existed has no such reader.
- # Treated as not internal rather than raising, so one old declaration
- # cannot take a whole class's preference schema down with it.
- return false unless respond_to?(getter)
-
- send(getter)
+ preference_definition(name)[:internal]
end
# The fixed set this preference's value must come from, when it has one.
@@ -87,14 +77,8 @@ module Spree::Preferences::Preferable
#
# @return [Array, nil]
def preference_choices(name)
- has_preference! name
- getter = self.class.preference_choices_getter_method(name)
- # A preference declared before this option existed has no such reader.
- # Treated as unconstrained rather than raising, so one old declaration
- # cannot take a whole class's preference schema down with it.
- return unless respond_to?(getter)
-
- send(getter)
+ choices = preference_definition(name)[:choices]
+ choices.respond_to?(:call) ? choices.call : choices
end
def has_preference!(name)
@@ -102,57 +86,125 @@ module Spree::Preferences::Preferable
end
def has_preference?(name)
- respond_to? self.class.preference_getter_method(name)
+ self.class.preference_definitions.key?(name.to_sym)
end
def defined_preferences
- # Only real `preference`-macro definitions carry a `_default` getter —
- # the name grep alone would also catch `preferred_*` association writers
- # (e.g. Order#preferred_stock_location=).
- methods.grep(/\Apreferred_.*=\Z/).filter_map do |pref_method|
- name = pref_method.to_s.gsub(/\Apreferred_|=\Z/, '').to_sym
- name if respond_to?(self.class.preference_default_getter_method(name))
- end
+ self.class.preference_definitions.keys
+ end
+
+ def default_preferences
+ defined_preferences.index_with { |name| preference_default(name) }
+ end
+
+ # Every preference value, secrets included — what `preferences` alone held
+ # before secrets moved to their own column. Declared preferences read
+ # through their `preferred_*` reader, so a default applies and decimals and
+ # times come back typed rather than in the string form JSON stores.
+ #
+ # @return [ActiveSupport::HashWithIndifferentAccess]
+ def preference_values
+ (preferences || {}).to_h.with_indifferent_access.merge(defined_preferences.index_with { |name| get_preference(name) })
end
- def deprecated_preferences
- defined_preferences.each_with_object([]) do |pref_name, array|
- deprecated_message = preference_deprecated(pref_name)
- array << { name: pref_name, message: deprecated_message } unless deprecated_message.nil?
+ # The stored value of a preference, before its default applies. A secret
+ # found in `preferences` wins over the encrypted column: it is either a
+ # newer value assigned as part of a whole hash, waiting for the save that
+ # moves it across, or one the upgrade has not moved yet — the writer always
+ # removes that copy, so it is never stale.
+ #
+ # @param name [Symbol, String]
+ # @return [Object] the stored value, or the block's result when nothing is stored
+ def stored_preference(name)
+ if secret_preference?(name)
+ (preferences || {}).fetch(name) { (secret_preferences || {}).fetch(name) { return yield } }
+ else
+ (preferences || {}).fetch(name) { yield }
end
end
- def default_preferences
- Hash[
- defined_preferences.map do |preference|
- [preference, preference_default(preference)]
- end
- ]
+ # Fills in the declared default of every preference this record has no
+ # stored value for, so a preference added to the class after the row was
+ # saved never reads as missing and the raw `preferences` hash always holds
+ # every value. Secrets are left out: their defaults are read through the
+ # `preferred_*` reader, never copied into storage.
+ #
+ # Filling in a default is not a change to a stored record: a loaded record
+ # stays clean, so `with_lock` still accepts it, and the defaults are written
+ # with its next real save.
+ def backfill_default_preferences
+ secrets = self.class.secret_preference_names
+ missing = defined_preferences.reject { |name| preferences.key?(name) || secrets.include?(name) }
+ return if missing.empty?
+
+ already_changed = attribute_changed?(:preferences)
+ self.preferences = preferences.merge(missing.index_with { |name| stored_default(name) })
+ clear_attribute_change(:preferences) if persisted? && !already_changed
end
- def preferences_of_type(type)
- defined_preferences.find_all { |preference| preference_type(preference) == type.to_sym }
+ # Names of the preferences the last save changed, secrets included.
+ #
+ # @return [Array<Symbol>]
+ def previously_changed_preference_names
+ defined_preferences.select { |name| public_send(:"saved_change_to_preferred_#{name}?") }
end
- def clear_preferences
- preferences.keys.each { |pref| preferences.delete pref }
+ private
+
+ def preference_definition(name)
+ has_preference! name
+ self.class.preference_definitions[name.to_sym]
end
- def restore_preferences_for(preference_keys)
- preference_keys.each { |pref| preferences[pref] = preference_default(pref) }
+ # A declared default in the form JSON stores, as the writer would store it,
+ # so a decimal default reads back as a BigDecimal and setting the same
+ # amount is not a change.
+ def stored_default(name)
+ default = preference_default(name)
+ type = preference_type(name)
+ return default if default.nil? || %i[decimal datetime].exclude?(type)
+
+ convert_preference_value(default, type, nullable: true).as_json
end
- def preference_change(name, changes_or_previous_changes)
- preference_changes = changes_or_previous_changes.with_indifferent_access.fetch('preferences', [{}, {}])
- before_preferences = preference_changes[0] || {}
- after_preferences = preference_changes[1] || {}
+ # Only a class that stores secrets can declare one, so the names say it all.
+ def secret_preference?(name)
+ self.class.secret_preference_names.include?(name.to_sym)
+ end
- return if before_preferences[name] == after_preferences[name]
+ def cast_preference(name, value)
+ definition = preference_definition(name)
+ parse_on_set = definition[:parse_on_set]
+ if parse_on_set.is_a?(Proc)
+ # Procs that accept more than one arg opt into receiving the owning
+ # record so they can scope (e.g. `normalize_id_preference` rejecting
+ # cross-store IDs). `arity.abs > 1` covers both the `(value, owner)` and
+ # `(value, owner = nil)` shapes.
+ value = parse_on_set.arity.abs > 1 ? parse_on_set.call(value, self) : parse_on_set.call(value)
+ end
+ value = convert_preference_value(value, definition[:type], nullable: definition[:nullable])
+ # Decimals and times are kept in the string form JSON stores, so a record
+ # read back from the database and one just written compare equal and
+ # setting the same value is not a change. The reader restores them.
+ value = value.as_json if %i[decimal datetime].include?(definition[:type])
- [before_preferences[name], after_preferences[name]]
+ Spree::Deprecation.warn("`#{name}` is deprecated. #{definition[:deprecated]}") if definition[:deprecated]
+ value
end
- private
+ # Assigns a new hash rather than calling the store accessor's writer, which
+ # skips writing nil to a missing key — leaving the default in force when nil
+ # was meant. Rails still sees no change when the value is the one stored.
+ def write_preference(name, value)
+ if secret_preference?(name)
+ # A copy assigned as part of a whole hash would otherwise outlive the
+ # value written here and be moved over it on save.
+ self.preferences = preferences.except(name) if preferences&.key?(name)
+ self.secret_preferences = (secret_preferences || {}).with_indifferent_access.merge(name => value)
+ else
+ self.preferences = (preferences || {}).with_indifferent_access.merge(name => value)
+ end
+ end
def convert_preference_value(value, type, nullable: false)
case type
@@ -223,7 +275,7 @@ module Spree::Preferences::Preferable
value.to_time
end
# Kept as the `yyyy-MM-dd` string it arrived as rather than coerced to a
- # Time. Preferences are YAML-serialized, and what a date *means* depends on
+ # Time. Preferences are stored as JSON, and what a date *means* depends on
# the store's timezone, which this method has no access to: coercing here
# would freeze the value against the server's zone and move the operator's
# deadline by hours. The reader owns that (see
diff --git a/spree/core/lib/spree/core/preferences/preferable_class_methods.rb b/spree/core/lib/spree/core/preferences/preferable_class_methods.rb
index 4bf11c6f8e..1f7cdd50c7 100644
--- a/spree/core/lib/spree/core/preferences/preferable_class_methods.rb
+++ b/spree/core/lib/spree/core/preferences/preferable_class_methods.rb
@@ -1,169 +1,104 @@
module Spree::Preferences
module PreferableClassMethods
- # Declaration order, so an admin form can present preferences the way
- # their author grouped them — credentials before the optional settings
- # that depend on them. `defined_preferences` reads Ruby's own `methods`,
- # whose order is an implementation detail, so it cannot answer this.
- def declared_preference_order
- @declared_preference_order ||= begin
- inherited = superclass.respond_to?(:declared_preference_order) ? superclass.declared_preference_order : []
- inherited.dup
+ # Bumped by every declaration anywhere, so a class's cached view of its
+ # preferences is rebuilt when an ancestor gains one after the class was
+ # loaded — a decorator adding a secret to Spree::PaymentMethod, say.
+ @declarations = 0
+
+ class << self
+ attr_accessor :declarations
+ end
+
+ # Every preference the class declares, inherited ones included, in
+ # declaration order — so an admin form can present them the way their
+ # author grouped them.
+ #
+ # @return [Hash{Symbol => Hash}] name => `{ type:, default:, deprecated:, internal:, choices:, nullable:, parse_on_set: }`
+ def preference_definitions
+ preference_declarations_cache[:definitions] ||= begin
+ inherited = superclass.respond_to?(:preference_definitions) ? superclass.preference_definitions : {}
+ inherited.merge(own_preference_definitions).freeze
end
end
+ # Each declared preference's type, known from the declarations alone —
+ # without building a record.
+ #
+ # @return [Hash{Symbol => Symbol}]
+ def declared_preference_types
+ preference_declarations_cache[:types] ||= preference_definitions.transform_values { |definition| definition[:type] }.freeze
+ end
+
+ # Names of the `:password` preferences, which a model including
+ # {Spree::SecretPreferences} stores encrypted in `secret_preferences`.
+ #
+ # @return [Array<Symbol>]
+ def secret_preference_names
+ preference_declarations_cache[:secrets] ||= declared_preference_types.filter_map { |name, type| name if type == :password }.freeze
+ end
+
+ # Declares a typed preference with a `preferred_<name>` reader and writer
+ # and a `prefers_<name>?` query. On a model, the value lives in the
+ # `preferences` column (`secret_preferences` for a `:password` one) as a
+ # Rails store accessor, which brings the change methods with it:
+ # `preferred_<name>_changed?`, `_change`, `_was`,
+ # `saved_change_to_preferred_<name>?` and `preferred_<name>_before_last_save`.
def preference(name, type, *args)
- declared_preference_order << name.to_sym unless declared_preference_order.include?(name.to_sym)
+ name = name.to_sym
+ secret = type == :password
+ if secret && !include?(Spree::SecretPreferences)
+ raise ArgumentError, "#{self.name} declares the secret preference `#{name}` but cannot encrypt it. " \
+ 'Include Spree::SecretPreferences and add a `secret_preferences` text column to its table.'
+ end
+
options = args.extract_options!
options.assert_valid_keys(:default, :deprecated, :in, :internal, :nullable, :parse_on_set)
default = options[:default]
default = -> { options[:default] } unless default.is_a?(Proc)
- deprecated = options[:deprecated]
- internal = options[:internal]
- # The fixed set a value must come from. Turns a text box into a picker
- # in every admin form, and is what the inclusion validation would have
- # told the operator only after a failed save.
- choices = options[:in]
- nullable = options[:nullable]
- parse_on_set = options[:parse_on_set]
-
- # cache_key will be nil for new objects, then if we check if there
- # is a pending preference before going to default
- define_method preference_getter_method(name) do
- # Rows persisted before the model gained preferences can load with a
- # NULL column — treat that as "no stored preferences".
- (preferences || {}).fetch(name) do
- default.call
- end
- end
-
- define_method preference_setter_method(name) do |value|
- if parse_on_set.is_a?(Proc)
- # Procs that accept more than one arg opt into receiving the
- # owning record so they can scope (e.g. `normalize_id_preference`
- # rejecting cross-store IDs). `arity.abs > 1` covers both the
- # `(value, owner)` and `(value, owner = nil)` shapes.
- value = parse_on_set.arity.abs > 1 ? parse_on_set.call(value, self) : parse_on_set.call(value)
- end
- value = convert_preference_value(value, type, nullable: nullable)
- self.preferences = {} if preferences.nil?
- preferences[name] = value
-
- Spree::Deprecation.warn("`#{name}` is deprecated. #{deprecated}") if deprecated
-
- # If this is an activerecord object, we need to inform
- # ActiveRecord::Dirty that this value has changed, since this is an
- # in-place update to the preferences hash.
- preferences_will_change! if respond_to?(:preferences_will_change!)
- end
-
- define_method preference_default_getter_method(name), &default
-
- define_method preference_type_getter_method(name) do
- type
- end
-
- define_method preference_deprecated_getter_method(name) do
- deprecated
- end
-
- # Whether this preference is the system's to write rather than the
- # operator's to supply — a value a provider hands back to us, not one
- # anybody could know in advance.
- define_method preference_internal_getter_method(name) do
- internal
- end
-
- define_method preference_choices_getter_method(name) do
- choices.respond_to?(:call) ? choices.call : choices
- end
-
- define_method prefers_query_method(name) do
- preferences.fetch(name).to_b
- end
-
- define_method preference_change_method(name) do
- preference_change(name, changes) if respond_to?(:changes)
- end
-
- define_method preference_was_method(name) do
- return unless respond_to?(:changes)
-
- preference_change(name, changes)&.first || get_preference(name)
- end
-
- define_method preference_changed_method(name) do
- respond_to?(:changes) && preference_change(name, changes).present?
- end
-
- define_method preference_previous_change_method(name) do
- preference_change(name, previous_changes) if respond_to?(:previous_changes)
+ own_preference_definitions[name] = {
+ type: type,
+ default: default,
+ deprecated: options[:deprecated],
+ # Whether the system writes the value rather than the operator — a
+ # value a provider hands back, kept out of every admin form.
+ internal: options[:internal],
+ # The fixed set a value must come from; turns a text box into a picker.
+ choices: options[:in],
+ nullable: options[:nullable],
+ parse_on_set: options[:parse_on_set]
+ }
+ PreferableClassMethods.declarations += 1
+
+ store_accessor(secret ? :secret_preferences : :preferences, name, prefix: :preferred)
+
+ # Overrides the store accessor's reader and writer, which treat a missing
+ # key as nil; here a missing key means the declared default.
+ define_method(:"preferred_#{name}") do
+ Spree::Preferences::Preferable.restore_value(stored_preference(name) { stored_default(name) }, type)
end
- define_method preference_previous_was_method(name) do
- return unless respond_to?(:previous_changes)
-
- preference_change(name, previous_changes)&.first
+ define_method(:"preferred_#{name}=") do |value|
+ write_preference(name, cast_preference(name, value))
end
- define_method preference_previous_changed_method(name) do
- respond_to?(:previous_changes) && preference_change(name, previous_changes).present?
+ define_method(:"prefers_#{name}?") do
+ stored_preference(name) { raise KeyError, "key not found: #{name.inspect}" }.to_b
end
end
- def preference_getter_method(name)
- "preferred_#{name}".to_sym
- end
-
- def preference_setter_method(name)
- "preferred_#{name}=".to_sym
- end
-
- def preference_default_getter_method(name)
- "preferred_#{name}_default".to_sym
- end
-
- def preference_deprecated_getter_method(name)
- "preferred_#{name}_deprecated".to_sym
- end
-
- def preference_internal_getter_method(name)
- "preferred_#{name}_internal".to_sym
- end
-
- def preference_choices_getter_method(name)
- "preferred_#{name}_choices".to_sym
- end
-
- def preference_type_getter_method(name)
- "preferred_#{name}_type".to_sym
- end
-
- def prefers_query_method(name)
- "prefers_#{name}?".to_sym
- end
-
- def preference_change_method(name)
- "preferred_#{name}_change".to_sym
- end
+ private
- def preference_was_method(name)
- "preferred_#{name}_was".to_sym
+ def own_preference_definitions
+ @own_preference_definitions ||= {}
end
- def preference_changed_method(name)
- "preferred_#{name}_changed?".to_sym
- end
-
- def preference_previous_change_method(name)
- "preferred_#{name}_previous_change".to_sym
- end
-
- def preference_previous_was_method(name)
- "preferred_#{name}_previously_was".to_sym
- end
-
- def preference_previous_changed_method(name)
- "preferred_#{name}_previously_changed?".to_sym
+ def preference_declarations_cache
+ generation = PreferableClassMethods.declarations
+ unless @preference_declarations_generation == generation
+ @preference_declarations_generation = generation
+ @preference_declarations_cache = {}
+ end
+ @preference_declarations_cache
end
end
end
diff --git a/spree/core/lib/spree/core/preferences/store.rb b/spree/core/lib/spree/core/preferences/store.rb
deleted file mode 100644
index cd8e065fec..0000000000
--- a/spree/core/lib/spree/core/preferences/store.rb
+++ /dev/null
@@ -1,105 +0,0 @@
-# Use singleton class Spree::Preferences::Store.instance to access
-#
-# StoreInstance has a persistence flag that is on by default,
-# but we disable database persistence in testing to speed up tests
-#
-
-require 'singleton'
-
-DB_EXCEPTIONS ||= if defined? PG
- [PG::ConnectionBad, ActiveRecord::NoDatabaseError]
- elsif defined? Mysql2
- [Mysql2::Error::ConnectionError, ActiveRecord::NoDatabaseError]
- else
- [ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError]
- end
-
-module Spree::Preferences
- class StoreInstance
- attr_accessor :persistence
-
- def initialize
- @cache = Rails.cache
- @persistence = true
- end
-
- def set(key, value)
- @cache.write(key, value)
- persist(key, value)
- end
- alias []= set
-
- def exist?(key)
- @cache.exist?(key) ||
- should_persist? && Spree::Preference.where(key: key).exists?
- end
-
- def get(key)
- # return the retrieved value, if it's in the cache
- # use unless nil? in case the value is actually boolean false
- #
- unless (val = @cache.read(key)).nil?
- return val
- end
-
- if should_persist?
- # If it's not in the cache, maybe it's in the database, but
- # has been cleared from the cache
-
- # does it exist in the database?
- val = if preference = Spree::Preference.find_by(key: key)
- # it does exist
- preference.value
- else
- # use the fallback value
- yield
- end
-
- # Cache either the value from the db or the fallback value.
- # This avoids hitting the db with subsequent queries.
- @cache.write(key, val)
-
- return val
- else
- yield
- end
- end
- alias fetch get
-
- def delete(key)
- @cache.delete(key)
- destroy(key)
- end
-
- def clear_cache
- @cache.clear
- end
-
- private
-
- def persist(cache_key, value)
- return unless should_persist?
-
- preference = Spree::Preference.where(key: cache_key).first_or_initialize
- preference.value = value
- preference.save
- end
-
- def destroy(cache_key)
- return unless should_persist?
-
- preference = Spree::Preference.find_by(key: cache_key)
- preference&.destroy
- end
-
- def should_persist?
- @persistence && Spree::Preference.table_exists?
- rescue *DB_EXCEPTIONS # this is fix to make Deploy To Heroku button work
- false
- end
- end
-
- class Store < StoreInstance
- include Singleton
- end
-end
diff --git a/spree/core/lib/spree/testing_support/preferences.rb b/spree/core/lib/spree/testing_support/preferences.rb
index c428a7b07d..d29d717d0f 100644
--- a/spree/core/lib/spree/testing_support/preferences.rb
+++ b/spree/core/lib/spree/testing_support/preferences.rb
@@ -18,11 +18,6 @@ module Spree
yield(config) if block_given?
end
- def assert_preference_unset(preference)
- find("#preferences_#{preference}")['checked'].should be false
- Spree::Config[preference].should be false
- end
-
# Stubs store-scoped commerce settings for the duration of one example.
#
# stub_store_preferences(track_inventory_levels: false)
diff --git a/spree/core/lib/spree/testing_support/store.rb b/spree/core/lib/spree/testing_support/store.rb
index 9ade30cc51..f7fba9d15b 100644
--- a/spree/core/lib/spree/testing_support/store.rb
+++ b/spree/core/lib/spree/testing_support/store.rb
@@ -29,6 +29,15 @@ RSpec.configure do |config|
end
end
+ # Reloaded before each example rather than after: the after hooks run
+ # inside the example's transaction, so a reload there still sees the
+ # example's writes, and the next example would start from values the
+ # database no longer holds. Setting a preference to such a stale value is
+ # no change to Rails, so it would never be saved.
+ config.before(:each) do
+ @default_store&.reload unless self.class.metadata[:without_global_store]
+ end
+
config.after(:each) do
unless self.class.metadata[:without_global_store]
@default_store&.products = []
@@ -43,7 +52,6 @@ RSpec.configure do |config|
@default_store.association(:default_market).reset if @default_store.association_cached?(:default_market)
@default_store.association(:markets).reset if @default_store.association_cached?(:markets)
@default_store.remove_instance_variable(:@has_markets) if @default_store.instance_variable_defined?(:@has_markets)
- @default_store.reload
end
end
end
diff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
index fd369be22f..ed1d181c85 100644
--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
@@ -35,6 +35,22 @@ steps:
Idempotent, and a no-op on an already-consolidated schema.
+ - id: preferences_json
+ name: "Check for preferences still stored as YAML"
+ task: "spree:upgrade:preferences_json"
+ notes: |
+ Spree 6.0 stores preferences as JSON instead of YAML, and keeps secrets
+ (preferences declared as passwords, such as gateway API keys) in a
+ separate secret_preferences column. The migration converts every row and
+ moves every secret itself, so a normal db:migrate needs nothing from this
+ step.
+
+ This is a safety net: it converts any row still holding YAML, and moves
+ the secrets of a class that was not loaded when the migration ran, such
+ as one from an extension gem installed afterwards.
+
+ Idempotent, and a no-op once everything is converted.
+
- id: migrate_media_class_names
name: "Rewrite stored Spree::Asset and Spree::Image class names"
task: "spree:migrate_media_class_names"
@@ -729,3 +745,18 @@ steps:
predates the upgrade.
Idempotent, and safe to re-run at any time to repair drift.
+
+ - id: encrypt_secret_preferences
+ name: "Encrypt gateway and integration secrets"
+ task: "spree:upgrade:encrypt_secret_preferences"
+ notes: |
+ The 6.0 migration moves existing secrets into the secret_preferences
+ column in plain text, so it never needs encryption keys. They stay
+ readable and are encrypted the next time their record is saved. This step
+ encrypts them all now.
+
+ Skips with a note when Active Record encryption keys are not configured;
+ see the encryption section of the upgrade guide to add them, then run
+ this step again.
+
+ Idempotent.
diff --git a/spree/core/lib/tasks/preferences_json.rake b/spree/core/lib/tasks/preferences_json.rake
new file mode 100644
index 0000000000..466c31bf6a
--- /dev/null
+++ b/spree/core/lib/tasks/preferences_json.rake
@@ -0,0 +1,101 @@
+# frozen_string_literal: true
+
+# 5.6 → 6.0 follow-ups to the ConvertPreferencesToJson migration
+# (docs/plans/6.0-json-preferences.md). Both are idempotent.
+module Spree
+ module SecretPreferencesUpgrade
+ # The models keeping secrets in `secret_preferences`, one per table.
+ def self.models
+ Rails.application.eager_load!
+ Spree::Base.descendants.select do |model|
+ model.include?(Spree::SecretPreferences) && model.base_class == model && model.table_exists?
+ end
+ end
+
+ # Every row of the model, deleted ones included, except rows whose class
+ # is no longer installed — loading one of those raises, which would stop
+ # the task before it reached the rest. They are reported and left as
+ # they are; their secrets stay readable.
+ #
+ # @return [ActiveRecord::Relation]
+ def self.loadable_rows(model)
+ rows = model.unscoped
+ return rows unless model.column_names.include?(model.inheritance_column)
+
+ types = rows.distinct.pluck(model.inheritance_column)
+ missing = types.compact.reject(&:safe_constantize)
+ return rows if missing.empty?
+
+ missing.each { |type| puts " #{model.table_name}: skipped rows of #{type}, which is not installed" }
+ rows.where.not(model.inheritance_column => missing).or(rows.where(model.inheritance_column => nil))
+ end
+ end
+end
+
+namespace :spree do
+ namespace :upgrade do
+ desc <<~DESC
+ Converts any preferences row still holding YAML to JSON, and moves secrets
+ left in `preferences` to the encrypted `secret_preferences` column.
+
+ The 6.0 migration does both itself, so a normal `db:migrate` leaves nothing
+ for this task. It exists for schemas changed out of band, and for secrets of
+ a class that was not loaded when the migration ran (an extension gem added
+ afterwards).
+ DESC
+ task preferences_json: :environment do
+ conversion = Spree::Preferences::JsonConversion.new(ActiveRecord::Base.connection, log: ->(message) { puts " #{message}" })
+
+ conversion.preference_tables.each do |table|
+ converted = conversion.text_column?(table) ? conversion.convert_text_table(table) : conversion.convert_table(table)
+ puts " #{table}: #{converted} rows converted from YAML" if converted.positive?
+ end
+
+ Spree::SecretPreferencesUpgrade.models.each do |model|
+ moved = 0
+
+ Spree::SecretPreferencesUpgrade.loadable_rows(model).find_each do |record|
+ separated = record.separated_secret_preferences
+ next unless separated
+
+ # Written without callbacks: moving a secret is not a change to it, and
+ # must not look like one to a gateway that re-registers on key changes.
+ record.update_columns(separated)
+ moved += 1
+ end
+
+ puts " #{model.table_name}: moved secrets out of preferences for #{moved} rows" if moved.positive?
+ end
+
+ puts 'preferences_json done.'
+ end
+
+ desc <<~DESC
+ Encrypts every `secret_preferences` value still stored in plain text.
+
+ The 6.0 migration moves existing secrets into `secret_preferences` without
+ encrypting them, so it never depends on encryption keys. They stay readable
+ as they are and are encrypted on their next save; this task encrypts them
+ all at once. Skips with a note when Active Record encryption keys are not
+ configured.
+ DESC
+ task encrypt_secret_preferences: :environment do
+ unless ActiveRecord::Encryption.config.has_primary_key?
+ puts ' Active Record encryption keys are not configured, so secrets stay in plain text. ' \
+ 'See https://spreecommerce.org/docs/developer/deployment/environment_variables'
+ next
+ end
+
+ Spree::SecretPreferencesUpgrade.models.each do |model|
+ count = 0
+ Spree::SecretPreferencesUpgrade.loadable_rows(model).where.not(secret_preferences: nil).find_each do |record|
+ record.encrypt
+ count += 1
+ end
+ puts " #{model.table_name}: encrypted secrets for #{count} rows"
+ end
+
+ puts 'encrypt_secret_preferences done.'
+ end
+ end
+end
diff --git a/spree/core/spec/lib/spree/core_spec.rb b/spree/core/spec/lib/spree/core_spec.rb
index 566f3096ce..b0392a9a79 100644
--- a/spree/core/spec/lib/spree/core_spec.rb
+++ b/spree/core/spec/lib/spree/core_spec.rb
@@ -121,26 +121,16 @@ describe Spree do
end
describe '.install_id' do
- let(:store) { Spree::Preferences::Store.instance }
+ it "returns the default store's identifier" do
+ allow(Spree::Store).to receive(:default).and_return(build(:store, preferred_install_id: 'stored-id'))
- around do |example|
- store.delete('spree/install_id')
- example.run
- store.delete('spree/install_id')
- end
-
- it 'generates a UUID, persists it and returns the same value on subsequent calls' do
- id = described_class.install_id
-
- expect(id).to match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
- expect(store.get('spree/install_id') { nil }).to eq(id)
- expect(described_class.install_id).to eq(id)
+ expect(described_class.install_id).to eq('stored-id')
end
- it 'reuses an identifier already persisted in the preferences store' do
- store.set('spree/install_id', 'already-persisted-id')
+ it 'returns nil without a default store' do
+ allow(Spree::Store).to receive(:default).and_return(nil)
- expect(described_class.install_id).to eq('already-persisted-id')
+ expect(described_class.install_id).to be_nil
end
end
diff --git a/spree/core/spec/lib/tasks/preferences_json_spec.rb b/spree/core/spec/lib/tasks/preferences_json_spec.rb
new file mode 100644
index 0000000000..d1f1b8b148
--- /dev/null
+++ b/spree/core/spec/lib/tasks/preferences_json_spec.rb
@@ -0,0 +1,236 @@
+require 'spec_helper'
+require 'rake'
+
+describe 'preferences stored as JSON' do
+ before(:all) do
+ Rake::Task.define_task(:environment)
+ load Spree::Core::Engine.root.join('lib', 'tasks', 'preferences_json.rake')
+ end
+
+ let(:connection) { ActiveRecord::Base.connection }
+ let(:conversion) { Spree::Preferences::JsonConversion.new(connection) }
+
+ describe Spree::Preferences::JsonConversion do
+ let(:table_name) { 'spree_legacy_preference_fixtures' }
+ let(:quoted_table) { connection.quote_table_name(table_name) }
+
+ # The real tables are converted by the migration, so the conversion is
+ # exercised against a synthetic table in the pre-6.0 shape: YAML text, plus
+ # the JSON column the migration writes into. Built once, outside the
+ # example's transaction, because MySQL commits any open transaction when a
+ # table is created.
+ before(:all) do
+ ActiveRecord::Base.connection.create_table 'spree_legacy_preference_fixtures', force: true do |t|
+ t.string :type
+ t.text :preferences
+ t.text :secret_preferences
+ t.json :preferences_json
+ end
+ end
+
+ after(:all) { ActiveRecord::Base.connection.drop_table 'spree_legacy_preference_fixtures', if_exists: true }
+
+ def insert_row(preferences, type: nil)
+ connection.exec_insert(
+ "INSERT INTO #{quoted_table} (type, preferences) VALUES (#{connection.quote(type)}, #{connection.quote(preferences)})"
+ )
+ connection.select_values("SELECT id FROM #{quoted_table} ORDER BY id DESC").first
+ end
+
+ def column(id, name)
+ value = connection.select_value("SELECT #{connection.quote_column_name(name)} FROM #{quoted_table} WHERE id = #{connection.quote(id)}")
+ value.is_a?(String) ? JSON.parse(value) : value
+ end
+
+ def convert
+ conversion.convert_table(table_name, target: 'preferences_json')
+ end
+
+ it 'writes YAML preferences as JSON with string keys and exact decimals' do
+ id = insert_row(YAML.dump({ amount: BigDecimal('9.99'), currency: 'USD', ids: [1, 2], enabled: true, starts_on: Date.new(2026, 1, 1) }))
+
+ convert
+
+ expect(column(id, 'preferences_json')).to eq(
+ 'amount' => '9.99', 'currency' => 'USD', 'ids' => [1, 2], 'enabled' => true, 'starts_on' => '2026-01-01'
+ )
+ end
+
+ it 'reads a hash dumped with indifferent access' do
+ id = insert_row(YAML.dump(ActiveSupport::HashWithIndifferentAccess.new(code: 'x')))
+
+ convert
+
+ expect(column(id, 'preferences_json')).to eq('code' => 'x')
+ end
+
+ it 'moves secrets of a resolvable class into secret_preferences' do
+ id = insert_row(YAML.dump({ dummy_key: 'pk_1', dummy_secret_key: 'sk_live_1' }), type: 'Spree::Gateway::Bogus')
+
+ convert
+
+ expect(column(id, 'preferences_json')).to eq('dummy_key' => 'pk_1')
+ expect(column(id, 'secret_preferences')).to eq('dummy_secret_key' => 'sk_live_1')
+ end
+
+ it 'leaves secrets in place when the class cannot be loaded' do
+ id = insert_row(YAML.dump({ api_key: 'sk_live_1' }), type: 'Spree::Gateway::Uninstalled')
+
+ convert
+
+ expect(column(id, 'preferences_json')).to eq('api_key' => 'sk_live_1')
+ expect(column(id, 'secret_preferences')).to be_nil
+ end
+
+ it 'turns tiers keyed by threshold into a list, lowest threshold first' do
+ id = insert_row(YAML.dump({ base_percent: BigDecimal('5'), tiers: { 200.0 => 20.0, 100.0 => 15.0 } }), type: 'Spree::Calculator::TieredPercent')
+
+ convert
+
+ expect(column(id, 'preferences_json')['tiers']).to eq(
+ [{ 'threshold' => '100.0', 'value' => '15.0' }, { 'threshold' => '200.0', 'value' => '20.0' }]
+ )
+ end
+
+ it "converts an application's subclass of a tiered calculator too" do
+ stub_const('MyApp::LoyaltyTiers', Class.new(Spree::Calculator::TieredFlatRate))
+ id = insert_row(YAML.dump({ tiers: { 50.0 => 5.0 } }), type: 'MyApp::LoyaltyTiers')
+
+ convert
+
+ expect(column(id, 'preferences_json')['tiers']).to eq([{ 'threshold' => '50.0', 'value' => '5.0' }])
+ end
+
+ it 'converts only tables a model storing Spree preferences reads' do
+ expect(conversion.preference_tables).to include('spree_payment_methods', 'spree_stores')
+ expect(conversion.preference_tables).not_to include(table_name)
+ end
+
+ it 'stops on a row it cannot read, naming the table and row' do
+ id = insert_row("--- !ruby/hash-with-ivars:ActionController::Parameters\nelements: {}\n")
+
+ expect { convert }.to raise_error(Spree::Preferences::JsonConversion::UnreadableRowError, /#{table_name} row #{id}/)
+ end
+
+ context 'when the column is already JSON but holds a YAML string' do
+ it 'rewrites the row in place' do
+ id = insert_row(nil)
+ connection.exec_update("UPDATE #{quoted_table} SET preferences_json = #{connection.quote(YAML.dump({ min: 3 }).to_json)} WHERE id = #{id}")
+
+ conversion.convert_table(table_name, source: 'preferences_json', target: 'preferences_json')
+
+ expect(column(id, 'preferences_json')).to eq('min' => 3)
+ end
+
+ it 'leaves rows that are already JSON objects alone' do
+ id = insert_row(nil)
+ connection.exec_update("UPDATE #{quoted_table} SET preferences_json = #{connection.quote({ min: 3 }.to_json)} WHERE id = #{id}")
+
+ expect(conversion.convert_table(table_name, source: 'preferences_json', target: 'preferences_json')).to eq(0)
+ end
+
+ it 'still reshapes tiers left as a hash, as when the calculator was not loaded during the migration' do
+ id = insert_row(nil, type: 'Spree::Calculator::TieredPercent')
+ connection.exec_update("UPDATE #{quoted_table} SET preferences_json = #{connection.quote({ tiers: { '100.0' => '15.0' } }.to_json)} WHERE id = #{id}")
+
+ conversion.convert_table(table_name, source: 'preferences_json', target: 'preferences_json')
+
+ expect(column(id, 'preferences_json')['tiers']).to eq([{ 'threshold' => '100.0', 'value' => '15.0' }])
+ end
+ end
+ end
+
+ describe 'spree:upgrade:preferences_json' do
+ let(:task) { Rake::Task['spree:upgrade:preferences_json'] }
+ let(:payment_method) { create(:credit_card_payment_method) }
+
+ after { task.reenable }
+
+ # A table whose model the migration did not load — an extension installed
+ # after the upgrade — still has the pre-6.0 text column.
+ context 'when a preferences column is still text' do
+ let(:table_name) { 'spree_late_extension_settings' }
+
+ before do
+ connection.create_table(table_name, force: true) { |t| t.text :preferences }
+ stub_const('LateExtensionSetting', Class.new(Spree::Base) { self.table_name = 'spree_late_extension_settings' })
+ connection.exec_insert("INSERT INTO #{table_name} (preferences) VALUES (#{connection.quote(YAML.dump({ limit: 5 }))})")
+ end
+
+ after { connection.drop_table table_name, if_exists: true }
+
+ it 'replaces it with a JSON column and converts its rows' do
+ expect { task.invoke }.to output(/#{table_name}: 1 rows converted/).to_stdout
+
+ expect(connection.columns(table_name).find { |column| column.name == 'preferences' }.type).not_to eq(:text)
+ value = connection.select_value("SELECT preferences FROM #{table_name}")
+ expect(value.is_a?(String) ? JSON.parse(value) : value).to eq('limit' => 5)
+ end
+
+ it 'finishes a conversion an earlier run left half done' do
+ connection.add_column table_name, :preferences_json, :json
+
+ expect { task.invoke }.to output(/#{table_name}: 1 rows converted/).to_stdout
+ expect(connection.column_exists?(table_name, :preferences_json)).to be(false)
+ end
+ end
+
+ # A secret left in `preferences` is what a class unloaded at migration time
+ # leaves behind.
+ it 'moves secrets left in preferences without treating it as a change to them' do
+ connection.exec_update(
+ "UPDATE spree_payment_methods SET preferences = #{connection.quote({ dummy_key: 'pk_1', dummy_secret_key: 'sk_left' }.to_json)}, " \
+ "secret_preferences = NULL WHERE id = #{payment_method.id}"
+ )
+
+ expect { task.invoke }.to output(/moved secrets out of preferences for 1 rows/).to_stdout
+
+ reloaded = Spree::PaymentMethod.find(payment_method.id)
+ expect(reloaded.preferences).not_to have_key('dummy_secret_key')
+ expect(reloaded.preferred_dummy_secret_key).to eq('sk_left')
+ end
+ end
+
+ describe 'a row whose class is no longer installed' do
+ let(:task) { Rake::Task['spree:upgrade:encrypt_secret_preferences'] }
+ let!(:payment_method) { create(:credit_card_payment_method) }
+ let!(:uninstalled) { create(:credit_card_payment_method) }
+
+ after do
+ task.reenable
+ connection.exec_delete("DELETE FROM spree_payment_methods WHERE id = #{uninstalled.id}")
+ end
+
+ it 'is skipped and reported, and the other rows are still processed' do
+ connection.exec_update("UPDATE spree_payment_methods SET type = 'SpreeRemovedGem::Gateway' WHERE id = #{uninstalled.id}")
+ connection.exec_update(
+ "UPDATE spree_payment_methods SET secret_preferences = #{connection.quote({ dummy_secret_key: 'sk_plain' }.to_json)} WHERE id = #{payment_method.id}"
+ )
+
+ expect { task.invoke }.to output(/skipped rows of SpreeRemovedGem::Gateway/).to_stdout
+
+ raw = connection.select_value("SELECT secret_preferences FROM spree_payment_methods WHERE id = #{payment_method.id}")
+ expect(raw).not_to include('sk_plain')
+ end
+ end
+
+ describe 'spree:upgrade:encrypt_secret_preferences' do
+ let(:task) { Rake::Task['spree:upgrade:encrypt_secret_preferences'] }
+ let(:payment_method) { create(:credit_card_payment_method) }
+
+ after { task.reenable }
+
+ it 'encrypts a secret the migration moved in plain text, which stays readable' do
+ connection.exec_update(
+ "UPDATE spree_payment_methods SET secret_preferences = #{connection.quote({ dummy_secret_key: 'sk_plain' }.to_json)} WHERE id = #{payment_method.id}"
+ )
+ expect(Spree::PaymentMethod.find(payment_method.id).preferred_dummy_secret_key).to eq('sk_plain')
+
+ expect { task.invoke }.to output(/encrypted secrets/).to_stdout
+
+ raw = connection.select_value("SELECT secret_preferences FROM spree_payment_methods WHERE id = #{payment_method.id}")
+ expect(raw).not_to include('sk_plain')
+ expect(Spree::PaymentMethod.find(payment_method.id).preferred_dummy_secret_key).to eq('sk_plain')
+ end
+ end
+end
diff --git a/spree/core/spec/lib/tasks/store_settings_spec.rb b/spree/core/spec/lib/tasks/store_settings_spec.rb
index ceab06b5dd..297a35e7e6 100644
--- a/spree/core/spec/lib/tasks/store_settings_spec.rb
+++ b/spree/core/spec/lib/tasks/store_settings_spec.rb
@@ -47,7 +47,7 @@ describe 'spree:store_settings:backfill_from_config' do
# merchant's later change from being undone by a second run.
it 'does not re-copy after the merchant changes the value back' do
run_task
- store.update!(preferred_address_requires_phone: false)
+ store.reload.update!(preferred_address_requires_phone: false)
expect { run_task }.not_to change { store.reload.preferred_address_requires_phone }.from(false)
end
diff --git a/spree/core/spec/models/spree/base_spec.rb b/spree/core/spec/models/spree/base_spec.rb
index d263006982..30d81ead77 100644
--- a/spree/core/spec/models/spree/base_spec.rb
+++ b/spree/core/spec/models/spree/base_spec.rb
@@ -105,14 +105,16 @@ describe Spree::Base do
end
describe 'preference defaults on load' do
- let(:store) { Spree::Store.default }
+ let(:store) { create(:store) }
- it 'backfills preferences added since the record was last saved' do
- store.update_column(:preferences, store.preferences.except(:timezone))
+ it 'backfills preferences added since the record was last saved, without marking the record changed' do
+ store.update_column(:preferences, store.preferences.except(:timezone, :install_id))
loaded = Spree::Store.find(store.id)
expect(loaded.preferences[:timezone]).to eq(loaded.preference_default(:timezone))
+ expect(loaded).not_to be_changed
+ expect { loaded.with_lock { nil } }.not_to raise_error
end
# `preferences` is a YAML-serialized Hash, so the dirty check compares the
diff --git a/spree/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb b/spree/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb
index 2520ee7e3b..7781791efc 100644
--- a/spree/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb
+++ b/spree/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb
@@ -6,18 +6,40 @@ describe Spree::Calculator::TieredFlatRate, type: :model do
describe '#valid?' do
subject { calculator.valid? }
- context 'when tiers is not a hash' do
+ context 'when tiers is not a list of tiers' do
before { calculator.preferred_tiers = ['nope', 0] }
it { is_expected.to be false }
end
- context 'when tiers is a hash' do
- context 'and one of the keys is not a positive number' do
- before { calculator.preferred_tiers = { 'nope' => 20 } }
+ context 'when tiers is the pre-6.0 hash keyed by threshold' do
+ before { calculator.preferred_tiers = { 100 => 15 } }
- it { is_expected.to be false }
- end
+ it { is_expected.to be false }
+ end
+
+ context 'when a threshold is not a positive number' do
+ before { calculator.preferred_tiers = [{ threshold: 'nope', value: 20 }] }
+
+ it { is_expected.to be false }
+ end
+
+ context 'when a threshold repeats' do
+ before { calculator.preferred_tiers = [{ threshold: 100, value: 15 }, { threshold: 100, value: 20 }] }
+
+ it { is_expected.to be false }
+ end
+
+ context 'when an amount is negative' do
+ before { calculator.preferred_tiers = [{ threshold: 100, value: -1 }] }
+
+ it { is_expected.to be false }
+ end
+
+ context 'when every tier has a positive threshold and an amount' do
+ before { calculator.preferred_tiers = [{ threshold: 100, value: 15 }] }
+
+ it { is_expected.to be true }
end
end
@@ -29,10 +51,10 @@ describe Spree::Calculator::TieredFlatRate, type: :model do
before do
calculator.preferred_base_amount = 10
calculator.preferred_currency = 'USD'
- calculator.preferred_tiers = {
- 100 => 15,
- 200 => 20
- }
+ calculator.preferred_tiers = [
+ { threshold: 200, value: 20 },
+ { threshold: 100, value: 15 }
+ ]
allow(line_item).to receive_messages(currency: 'USD')
end
diff --git a/spree/core/spec/models/spree/calculator/tiered_percent_spec.rb b/spree/core/spec/models/spree/calculator/tiered_percent_spec.rb
index 2d1b5b1d6e..0ca2fe4b32 100644
--- a/spree/core/spec/models/spree/calculator/tiered_percent_spec.rb
+++ b/spree/core/spec/models/spree/calculator/tiered_percent_spec.rb
@@ -18,24 +18,22 @@ describe Spree::Calculator::TieredPercent, type: :model do
it { is_expected.to be false }
end
- context 'when tiers is not a hash' do
+ context 'when tiers is not a list of tiers' do
before { calculator.preferred_tiers = ['nope', 0] }
it { is_expected.to be false }
end
- context 'when tiers is a hash' do
- context 'and one of the keys is not a positive number' do
- before { calculator.preferred_tiers = { 'nope' => 20 } }
+ context 'when a threshold is not a positive number' do
+ before { calculator.preferred_tiers = [{ threshold: 'nope', value: 20 }] }
- it { is_expected.to be false }
- end
+ it { is_expected.to be false }
+ end
- context 'and one of the values is not a percent' do
- before { calculator.preferred_tiers = { 10 => 110 } }
+ context 'when a rate is not a percent' do
+ before { calculator.preferred_tiers = [{ threshold: 10, value: 110 }] }
- it { is_expected.to be false }
- end
+ it { is_expected.to be false }
end
end
@@ -46,10 +44,10 @@ describe Spree::Calculator::TieredPercent, type: :model do
before do
calculator.preferred_base_percent = 10
- calculator.preferred_tiers = {
- 100 => 15,
- 200 => 20
- }
+ calculator.preferred_tiers = [
+ { threshold: 100, value: 15 },
+ { threshold: 200, value: 20 }
+ ]
end
context 'when amount falls within the first tier' do
@@ -61,7 +59,33 @@ describe Spree::Calculator::TieredPercent, type: :model do
context 'when amount falls within the second tier' do
before { allow(line_item).to receive_messages(amount: 150) }
- it { is_expected.to eq 22 }
+ it { is_expected.to eq 22.5 }
+ end
+
+ context 'when an unsaved tier is not a number' do
+ before do
+ calculator.preferred_tiers += [{ threshold: 'abc', value: 30 }]
+ allow(line_item).to receive_messages(amount: 150)
+ end
+
+ it('skips it') { is_expected.to eq 22.5 }
+ end
+
+ context 'when the tiers are still a hash keyed by threshold, as before 6.0' do
+ before do
+ calculator.preferences = calculator.preferences.merge(tiers: { '100' => '15', '200' => '20' })
+ allow(line_item).to receive_messages(amount: 150)
+ end
+
+ it('still applies them') { is_expected.to eq 22.5 }
+ end
+ end
+
+ context 'when saved and loaded again' do
+ it 'keeps the tiers as a list with exact decimals' do
+ calculator.update!(preferred_tiers: [{ threshold: '100.5', value: '12.5' }])
+
+ expect(described_class.find(calculator.id).preferred_tiers).to eq([{ 'threshold' => '100.5', 'value' => '12.5' }])
end
end
end
diff --git a/spree/core/spec/models/spree/gateway_spec.rb b/spree/core/spec/models/spree/gateway_spec.rb
index cb8516f50f..459c98e8d0 100644
--- a/spree/core/spec/models/spree/gateway_spec.rb
+++ b/spree/core/spec/models/spree/gateway_spec.rb
@@ -11,6 +11,7 @@ describe Spree::Gateway, type: :model do
preference :publishable_preference1, :string
preference :publishable_preference2, :string
preference :private_preference, :string
+ preference :threshold_amount, :decimal, default: 9.99
def provider_class
Provider
@@ -23,6 +24,10 @@ describe Spree::Gateway, type: :model do
end
end
+ it 'hands the provider typed preference values' do
+ expect(TestGateway.new.options[:threshold_amount]).to eq(BigDecimal('9.99'))
+ end
+
it 'passes through all arguments on a method_missing call' do
gateway = TestGateway.new
expect(gateway.provider).to receive(:imaginary_method).with('foo')
diff --git a/spree/core/spec/models/spree/import_row_spec.rb b/spree/core/spec/models/spree/import_row_spec.rb
index 676fbca3ed..c218652e79 100644
--- a/spree/core/spec/models/spree/import_row_spec.rb
+++ b/spree/core/spec/models/spree/import_row_spec.rb
@@ -102,8 +102,10 @@ RSpec.describe Spree::ImportRow, :job, type: :model do
end
it 'memoizes the result' do
- expect(JSON).to receive(:parse).once.and_call_original
+ allow(JSON).to receive(:parse).and_call_original
2.times { import_row.data_json }
+
+ expect(JSON).to have_received(:parse).with(import_row.data).once
end
end
diff --git a/spree/core/spec/models/spree/payment_method_spec.rb b/spree/core/spec/models/spree/payment_method_spec.rb
index 95260081ab..80bd883fb2 100644
--- a/spree/core/spec/models/spree/payment_method_spec.rb
+++ b/spree/core/spec/models/spree/payment_method_spec.rb
@@ -436,19 +436,6 @@ describe Spree::PaymentMethod, type: :model do
expect(gateway.preference_internal(:issued_secret)).to be(true)
expect(gateway.preference_internal(:api_key)).to be_nil
end
-
- # A gateway loaded from a build that predates this option has no such
- # reader. Schema computation rescues everything into an empty list, so
- # without a guard one old declaration would strip every field from the
- # class — including the password ones the admin form relies on.
- it 'still describes a class whose declarations predate the option' do
- gateway_class.send(:undef_method, :preferred_issued_secret_internal)
- gateway_class.instance_variable_set(:@preference_schema, nil)
-
- keys = gateway_class.preference_schema.map { |field| field[:key] }
-
- expect(keys).to include(:api_key, :issued_secret)
- end
end
# Describing a class needs a connection, so the schema can be asked for
@@ -486,4 +473,90 @@ describe Spree::PaymentMethod, type: :model do
expect(gateway_class.serialized_preference_schema.map { |field| field[:key] }).to eq([:api_key])
end
end
+
+ describe 'secret preferences' do
+ let(:gateway) { create(:credit_card_payment_method) }
+ let(:raw_secret_column) do
+ ActiveRecord::Base.connection.select_value("SELECT secret_preferences FROM spree_payment_methods WHERE id = #{gateway.id}")
+ end
+
+ it 'stores a :password preference only in the encrypted column' do
+ gateway.update!(preferred_dummy_secret_key: 'sk_live_secret')
+
+ expect(gateway.reload.preferred_dummy_secret_key).to eq('sk_live_secret')
+ expect(gateway.preferences).not_to have_key('dummy_secret_key')
+ expect(raw_secret_column).to be_present
+ expect(raw_secret_column).not_to include('sk_live_secret')
+ end
+
+ it 'moves a secret assigned with the whole preferences hash before saving' do
+ gateway.update!(preferences: gateway.preferences.merge(dummy_secret_key: 'sk_from_hash'))
+
+ expect(gateway.reload.preferences).not_to have_key('dummy_secret_key')
+ expect(gateway.preferred_dummy_secret_key).to eq('sk_from_hash')
+ end
+
+ it 'moves a secret assigned in a plain hash with string keys, as JSON or form parameters arrive' do
+ gateway.update!(preferences: { 'dummy_secret_key' => 'sk_from_json' })
+ gateway.update!(preferred_dummy_secret_key: 'sk_rotated')
+
+ expect(gateway.reload.preferences).not_to have_key('dummy_secret_key')
+ expect(gateway.preferred_dummy_secret_key).to eq('sk_rotated')
+ end
+
+ it 'reads a secret assigned with the whole hash before the save moves it' do
+ gateway.update!(preferred_dummy_secret_key: 'sk_old')
+ gateway.preferences = gateway.preferences.merge(dummy_secret_key: 'sk_new')
+
+ expect(gateway.preferred_dummy_secret_key).to eq('sk_new')
+ end
+
+ it "reads a secret's default without copying it into storage" do
+ expect(gateway.preferred_dummy_secret_key).to eq('SECRETKEY123')
+ expect(gateway.secret_preferences.to_h).not_to have_key('dummy_secret_key')
+ end
+
+ it 'hands the secrets to the gateway connection' do
+ gateway.update!(preferred_dummy_secret_key: 'sk_live_secret')
+
+ expect(gateway.reload.options).to include(dummy_secret_key: 'sk_live_secret', dummy_key: 'PUBLICKEY123')
+ end
+
+ it 'reports a secret change through the preference change methods' do
+ gateway.update!(preferred_dummy_secret_key: 'sk_rotated')
+
+ expect(gateway.saved_change_to_preferred_dummy_secret_key?).to be(true)
+ expect(gateway.previously_changed_preference_names).to eq([:dummy_secret_key])
+ end
+
+ it 'does not mark a gateway without stored secrets as changed when one is read' do
+ gateway.update_column(:secret_preferences, nil)
+ reloaded = Spree::PaymentMethod.find(gateway.id)
+
+ reloaded.preferred_dummy_secret_key
+ reloaded.options
+
+ expect(reloaded.changed?).to be(false)
+ end
+
+ it 'treats a secret a decorator adds to the parent class later as a secret' do
+ parent = Class.new(Spree::Gateway) { def self.name = 'DecoratedGateway' }
+ child = Class.new(parent) { def self.name = 'DecoratedGatewayChild' }
+ expect(child.secret_preference_names).not_to include(:late_api_key)
+
+ parent.preference :late_api_key, :password
+
+ expect(child.secret_preference_names).to include(:late_api_key)
+ end
+
+ it 'refuses a :password preference on a model that cannot encrypt it' do
+ expect do
+ Class.new(Spree::Calculator) do
+ def self.name = 'CalculatorWithSecret'
+
+ preference :api_key, :password
+ end
+ end.to raise_error(ArgumentError, /Spree::SecretPreferences/)
+ end
+ end
end
diff --git a/spree/core/spec/models/spree/preference_spec.rb b/spree/core/spec/models/spree/preference_spec.rb
deleted file mode 100644
index 1f767c3795..0000000000
--- a/spree/core/spec/models/spree/preference_spec.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Preference, type: :model do
- it 'requires a key' do
- @preference = Spree::Preference.new
- @preference.key = :test
- @preference.value = true
- expect(@preference).to be_valid
- end
-
- describe 'type conversion for values' do
- def round_trip_preference(key, value)
- p = Spree::Preference.new
- p.value = value
- p.key = key
- p.save
-
- Spree::Preference.find_by(key: key)
- end
-
- it ':boolean' do
- value = true
- key = 'boolean_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it 'false :boolean' do
- value = false
- key = 'boolean_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':integer' do
- value = 10
- key = 'integer_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':decimal' do
- value = 1.5
- key = 'decimal_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':string' do
- value = 'This is a string'
- key = 'string_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':text' do
- value = 'This is a string stored as text'
- key = 'text_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':password' do
- value = 'This is a password'
- key = 'password_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
-
- it ':any' do
- value = [1, 2]
- key = 'any_key'
- pref = round_trip_preference(key, value)
- expect(pref.value).to eq value
- end
- end
-end
diff --git a/spree/core/spec/models/spree/preferences/masking_spec.rb b/spree/core/spec/models/spree/preferences/masking_spec.rb
index d3fa6f8aef..09680c4a66 100644
--- a/spree/core/spec/models/spree/preferences/masking_spec.rb
+++ b/spree/core/spec/models/spree/preferences/masking_spec.rb
@@ -53,25 +53,16 @@ RSpec.describe Spree::Preferences::Masking do
end
describe '.serialize' do
- # A throwaway Preferable — keeps the test focused on the masking
- # helper instead of coupling to whichever production model happens
- # to expose a :password preference today. Provides its own
- # `preferences` hash since Preferable is normally mixed into an
- # ActiveRecord model that supplies the serialized column.
+ # A throwaway payment method — keeps the test focused on the masking
+ # helper instead of coupling to whichever production gateway happens to
+ # expose a :password preference today. A payment method can store secrets.
let(:preferable_class) do
- Class.new do
- include Spree::Preferences::Preferable
- include Spree::PreferenceSchema
-
+ Class.new(Spree::PaymentMethod) do
preference :api_key, :string, default: 'PUBLIC123'
preference :api_secret, :password, default: 'SECRET456'
preference :ratio, :decimal, default: 0.5
preference :enabled, :boolean, default: true
- def preferences
- @preferences ||= {}
- end
-
def self.name
'TestPreferable'
end
@@ -95,7 +86,7 @@ RSpec.describe Spree::Preferences::Masking do
result = described_class.serialize(preferable)
expect(result['api_key']).to eq('pk_live_visible')
- expect(result['ratio']).to eq(0.25)
+ expect(result['ratio']).to eq('0.25')
expect(result['enabled']).to be false
end
diff --git a/spree/core/spec/models/spree/preferences/preferable_spec.rb b/spree/core/spec/models/spree/preferences/preferable_spec.rb
index 107066400d..424a74f988 100644
--- a/spree/core/spec/models/spree/preferences/preferable_spec.rb
+++ b/spree/core/spec/models/spree/preferences/preferable_spec.rb
@@ -2,18 +2,12 @@ require 'spec_helper'
describe Spree::Preferences::Preferable, type: :model do
before :all do
- class A
- include ActiveModel::Model
- include Spree::Preferences::Preferable
- attr_reader :id
-
- def initialize
- @id = rand(999)
- end
+ ActiveRecord::Migration.suppress_messages do
+ ActiveRecord::Migration.create_table(:preferable_spec_records, force: true) { |t| t.json :preferences }
+ end
- def preferences
- @preferences ||= default_preferences
- end
+ class A < Spree::Base
+ self.table_name = 'preferable_spec_records'
preference :color, :string, default: 'green', deprecated: 'Please use colour instead'
end
@@ -23,16 +17,13 @@ describe Spree::Preferences::Preferable, type: :model do
end
end
+ after :all do
+ ActiveRecord::Migration.suppress_messages { ActiveRecord::Migration.drop_table(:preferable_spec_records) }
+ end
+
before do
@a = A.new
- allow(@a).to receive_messages(persisted?: true)
@b = B.new
- allow(@b).to receive_messages(persisted?: true)
-
- # ensure we're persisting as that is the default
- #
- store = Spree::Preferences::Store.instance
- store.persistence = true
end
describe 'preference definitions' do
@@ -64,17 +55,14 @@ describe Spree::Preferences::Preferable, type: :model do
end
it 'has a type' do
- expect(@a.preferred_color_type).to eq :string
expect(@a.preference_type(:color)).to eq :string
end
it 'has a default' do
- expect(@a.preferred_color_default).to eq 'green'
expect(@a.preference_default(:color)).to eq 'green'
end
it 'can have a deprecation message' do
- expect(@a.preferred_color_deprecated).to eq 'Please use colour instead'
expect(@a.preference_deprecated(:color)).to eq 'Please use colour instead'
end
@@ -116,11 +104,6 @@ describe Spree::Preferences::Preferable, type: :model do
color: 'green')
end
- it 'builds an array of deprecated preferences' do
- expect(@b.deprecated_preferences).to eq([{ name: :color,
- message: 'Please use colour instead' }])
- end
-
context 'converts integer preferences to integer values' do
before do
A.preference :is_integer, :integer
@@ -166,17 +149,30 @@ describe Spree::Preferences::Preferable, type: :model do
A.preference :if_decimal, :decimal
end
- it 'returns a BigDecimal' do
+ it 'returns a BigDecimal, stored as its exact string' do
@a.set_preference(:if_decimal, 3.3)
- expect(@a.preferences[:if_decimal].class).to eq(BigDecimal)
+ expect(@a.get_preference(:if_decimal)).to eq(BigDecimal('3.3'))
+ expect(@a.preferences[:if_decimal]).to eq('3.3')
end
it 'with strings' do
@a.set_preference(:if_decimal, '3.3')
- expect(@a.preferences[:if_decimal]).to eq(3.3)
+ expect(@a.get_preference(:if_decimal)).to eq(3.3)
@a.set_preference(:if_decimal, '')
- expect(@a.preferences[:if_decimal]).to eq(0.0)
+ expect(@a.get_preference(:if_decimal)).to eq(0.0)
+ end
+
+ it 'stores and returns a decimal default as a BigDecimal, so setting the same amount is not a change' do
+ A.preference :decimal_with_default, :decimal, default: 0.0
+ record = A.create!
+ loaded = A.find(record.id)
+
+ expect(loaded.preferences[:decimal_with_default]).to eq('0.0')
+ expect(loaded.preferred_decimal_with_default).to be_a(BigDecimal)
+
+ loaded.preferred_decimal_with_default = 0
+ expect(loaded).not_to be_changed
end
end
@@ -197,13 +193,13 @@ describe Spree::Preferences::Preferable, type: :model do
it 'converts string to BigDecimal when present' do
@a.set_preference(:nullable_decimal, '3.14')
- expect(@a.preferences[:nullable_decimal]).to eq(BigDecimal('3.14'))
- expect(@a.preferences[:nullable_decimal].class).to eq(BigDecimal)
+ expect(@a.get_preference(:nullable_decimal)).to eq(BigDecimal('3.14'))
+ expect(@a.get_preference(:nullable_decimal).class).to eq(BigDecimal)
end
it 'preserves decimal values' do
@a.set_preference(:nullable_decimal, 9.99)
- expect(@a.preferences[:nullable_decimal]).to eq(BigDecimal('9.99'))
+ expect(@a.get_preference(:nullable_decimal)).to eq(BigDecimal('9.99'))
end
end
@@ -298,9 +294,9 @@ describe Spree::Preferences::Preferable, type: :model do
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
end
- it 'with hash and keys are integers' do
+ it 'with hash and keys are integers, which JSON keeps as strings' do
@a.set_preference(:is_hash, 1 => 2, 3 => 4)
- expect(@a.preferences[:is_hash]).to eql(1 => 2, 3 => 4)
+ expect(@a.preferences[:is_hash]).to eql('1' => 2, '3' => 4)
end
it 'with string' do
@@ -383,6 +379,7 @@ describe Spree::Preferences::Preferable, type: :model do
before do
A.preference :product_ids, :any, default: []
A.preference :product_attributes, :any, default: {}
+ @a = A.new
end
it 'with array' do
@@ -394,7 +391,7 @@ describe Spree::Preferences::Preferable, type: :model do
it 'with hash' do
expect(@a.preferences[:product_attributes]).to eq({})
@a.set_preference(:product_attributes, id: 1, name: 2)
- expect(@a.preferences[:product_attributes]).to eq(id: 1, name: 2)
+ expect(@a.preferences[:product_attributes]).to eq('id' => 1, 'name' => 2)
end
end
end
@@ -405,7 +402,7 @@ describe Spree::Preferences::Preferable, type: :model do
def self.up
create_table :pref_tests do |t|
t.string :col
- t.text :preferences
+ t.json :preferences
end
end
@@ -421,6 +418,8 @@ describe Spree::Preferences::Preferable, type: :model do
class PrefTest < Spree::Base
preference :pref_test_pref, :string, default: 'abc'
preference :pref_test_any, :any, default: []
+ preference :pref_test_decimal, :decimal, default: 0
+ preference :pref_test_datetime, :datetime
end
end
@@ -453,13 +452,6 @@ describe Spree::Preferences::Preferable, type: :model do
end
end
- it 'clear preferences' do
- @pt.set_preference(:pref_test_pref, 'xyz')
- expect(@pt.preferred_pref_test_pref).to eq('xyz')
- @pt.clear_preferences
- expect(@pt.preferred_pref_test_pref).to eq('abc')
- end
-
it 'clear preferences when record is deleted' do
@pt.save!
@pt.preferred_pref_test_pref = 'lmn'
@@ -471,6 +463,44 @@ describe Spree::Preferences::Preferable, type: :model do
expect(@pt1.get_preference(:pref_test_pref)).to eq('abc')
end
+ describe 'JSON storage' do
+ it 'restores a decimal exactly after a round trip' do
+ @pt.update!(preferred_pref_test_decimal: '19.99')
+
+ expect(PrefTest.find(@pt.id).preferred_pref_test_decimal).to eq(BigDecimal('19.99'))
+ end
+
+ it 'restores a time after a round trip' do
+ time = Time.zone.parse('2026-03-01 09:30:00')
+ @pt.update!(preferred_pref_test_datetime: time)
+ reloaded = PrefTest.find(@pt.id)
+
+ expect(reloaded.preferred_pref_test_datetime).to eq(time)
+ reloaded.preferred_pref_test_datetime = time
+ expect(reloaded.preferred_pref_test_datetime_changed?).to be(false)
+ end
+
+ it 'reads stored keys with indifferent access' do
+ @pt.update!(preferred_pref_test_pref: 'xyz')
+ reloaded = PrefTest.find(@pt.id)
+
+ expect(reloaded.preferences[:pref_test_pref]).to eq('xyz')
+ expect(reloaded.preferences['pref_test_pref']).to eq('xyz')
+ end
+
+ it 'does not mark a loaded record as changed' do
+ expect(PrefTest.find(@pt.id).changed?).to be(false)
+ end
+
+ it 'does not report a change when a decimal is set to the value it holds' do
+ @pt.update!(preferred_pref_test_decimal: '19.99')
+ reloaded = PrefTest.find(@pt.id)
+ reloaded.preferred_pref_test_decimal = BigDecimal('19.99')
+
+ expect(reloaded.preferred_pref_test_decimal_changed?).to be(false)
+ end
+ end
+
describe 'preference change tracking methods' do
it 'tracks changes to preferences' do
@pt.preferred_pref_test_pref = 'xyz'
@@ -483,9 +513,9 @@ describe Spree::Preferences::Preferable, type: :model do
@pt.preferred_pref_test_pref = 'xyz'
@pt.save!
- expect(@pt.preferred_pref_test_pref_previously_changed?).to be true
- expect(@pt.preferred_pref_test_pref_previous_change).to eq(['abc', 'xyz'])
- expect(@pt.preferred_pref_test_pref_previously_was).to eq('abc')
+ expect(@pt.saved_change_to_preferred_pref_test_pref?).to be true
+ expect(@pt.saved_change_to_preferred_pref_test_pref).to eq(['abc', 'xyz'])
+ expect(@pt.preferred_pref_test_pref_before_last_save).to eq('abc')
end
it 'reports no changes when preference is set to same value' do
diff --git a/spree/core/spec/models/spree/preferences/store_spec.rb b/spree/core/spec/models/spree/preferences/store_spec.rb
deleted file mode 100644
index 3d2ffe3c75..0000000000
--- a/spree/core/spec/models/spree/preferences/store_spec.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Preferences::Store, type: :model do
- before do
- @store = Spree::Preferences::StoreInstance.new
- end
-
- it 'sets and gets a key' do
- @store.set :test, 1
- expect(@store.exist?(:test)).to be true
- expect(@store.get(:test)).to eq 1
- end
-
- it 'can set and get false values when cache return nil' do
- @store.set :test, false
- expect(@store.get(:test)).to be false
- end
-
- xit 'will return db value when cache is empty and cache the db value' do
- preference = Spree::Preference.where(key: 'test').first_or_initialize
- preference.value = '123'
- preference.save
-
- Rails.cache.clear
- expect(@store.get(:test)).to eq '123'
- expect(Rails.cache.read(:test)).to eq '123'
- end
-
- xit 'returns and cache fallback value when supplied' do
- Rails.cache.clear
- expect(@store.get(:test) { false }).to be false
- expect(Rails.cache.read(:test)).to be false
- end
-
- xit 'returns but not cache fallback value when persistence is disabled' do
- Rails.cache.clear
- allow(@store).to receive_messages(should_persist?: false)
- expect(@store.get(:test) { true }).to be true
- expect(Rails.cache.exist?(:test)).to be false
- end
-
- it "returns nil when key can't be found and fallback value is not supplied" do
- expect(@store.get(:random_key) { nil }).to be_nil
- end
-end
diff --git a/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb b/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
index 0c0b4b973b..039562b86d 100644
--- a/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
+++ b/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
@@ -26,8 +26,8 @@ describe Spree::Promotion::Actions::CreateAdjustment, type: :model do
{
'a flat percent of the item total' => -> { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) },
- 'a tiered percent' => -> { Spree::Calculator::TieredPercent.new(preferred_base_percent: 10, preferred_tiers: { 100 => 50 }) },
- 'a tiered flat rate' => -> { Spree::Calculator::TieredFlatRate.new(preferred_base_amount: 2, preferred_currency: 'USD', preferred_tiers: { 100 => 50 }) }
+ 'a tiered percent' => -> { Spree::Calculator::TieredPercent.new(preferred_base_percent: 10, preferred_tiers: [{ threshold: 100, value: 50 }]) },
+ 'a tiered flat rate' => -> { Spree::Calculator::TieredFlatRate.new(preferred_base_amount: 2, preferred_currency: 'USD', preferred_tiers: [{ threshold: 100, value: 50 }]) }
}.each do |calculator_description, build_calculator|
it "discounts it by #{calculator_description}" do
action = described_class.create!(promotion: promotion, calculator: build_calculator.call)
diff --git a/spree/core/spec/models/spree/promotion_spec.rb b/spree/core/spec/models/spree/promotion_spec.rb
index 0c66c30bd1..edfb6e47f4 100644
--- a/spree/core/spec/models/spree/promotion_spec.rb
+++ b/spree/core/spec/models/spree/promotion_spec.rb
@@ -1155,5 +1155,17 @@ describe Spree::Promotion, type: :model do
expect(promotion.actions.size).to eq(1)
end
+
+ it 'reports an invalid action on the new promotion it was assigned to' do
+ promotion = build(:promotion)
+ promotion.actions = [{
+ type: 'create_adjustment',
+ calculator: { type: 'tiered_percent', preferences: { tiers: [{ threshold: '100', value: '150' }] } }
+ }]
+
+ expect(promotion.save).to be(false)
+ expect(promotion.errors[:base].join).to include('between 0% and 100%')
+ expect(promotion).not_to be_persisted
+ end
end
end
diff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rb
index f4a73977f5..8a7b3abf33 100644
--- a/spree/core/spec/models/spree/store_spec.rb
+++ b/spree/core/spec/models/spree/store_spec.rb
@@ -136,6 +136,20 @@ describe Spree::Store, type: :model, without_global_store: true do
end
context 'Callbacks' do
+ describe '#set_install_id' do
+ it 'generates a UUID when the store is saved without one' do
+ store = create(:store)
+
+ expect(store.preferred_install_id).to match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/)
+ end
+
+ it 'keeps an identifier the store already has' do
+ store = create(:store, preferred_install_id: 'already-persisted-id')
+
+ expect(store.reload.preferred_install_id).to eq('already-persisted-id')
+ end
+ end
+
# Pinned: store provisioning (first-run setup, enterprise multi-tenant)
# leans on the single-default invariant this callback maintains.
describe '#ensure_default_exists_and_is_unique' do
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway.rb b/spree/providers/stripe/app/models/spree_stripe/gateway.rb
index c7a182611d..2fa4392c69 100644
--- a/spree/providers/stripe/app/models/spree_stripe/gateway.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/gateway.rb
@@ -58,12 +58,10 @@ module SpreeStripe
#
# @return [Boolean]
def only_webhook_registration_changed?
- before, after = saved_change_to_preferences
- return false unless before.is_a?(Hash) && after.is_a?(Hash)
-
- changed = (before.keys | after.keys).reject { |key| before[key] == after[key] }
+ return false if previously_new_record?
- changed.any? && changed.all? { |key| WEBHOOK_REGISTRATION_PREFERENCES.include?(key.to_sym) }
+ changed = previously_changed_preference_names
+ changed.any? && changed.all? { |key| WEBHOOK_REGISTRATION_PREFERENCES.include?(key) }
end
# Endpoints are registered per Stripe account, so a rotated key leaves the
@@ -75,10 +73,7 @@ module SpreeStripe
# read as every key having changed.
return false if previously_new_record?
- # The preference macro's own reader rather than a hand-rolled diff: it
- # reads the change through indifferent access, so it answers the same
- # whether the serialized hash came back with symbol or string keys.
- preferred_secret_key_previously_changed?
+ saved_change_to_preferred_secret_key?
end
def gateway_dashboard_payment_url(payment)
diff --git a/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake b/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
index 2a326bd2b3..8544e3ec41 100644
--- a/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
+++ b/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
@@ -40,11 +40,14 @@ module SpreeStripe
next
end
+ # Through the preference writers, so the signing secret lands in the
+ # encrypted column; saved without callbacks, which would re-register
+ # the endpoint the secret belongs to.
+ gateway.preferred_webhook_endpoint_id = key.stripe_id
+ gateway.preferred_webhook_signing_secret = key.signing_secret
gateway.update_columns(
- preferences: gateway.preferences.merge(
- webhook_endpoint_id: key.stripe_id,
- webhook_signing_secret: key.signing_secret
- ),
+ preferences: gateway.preferences,
+ secret_preferences: gateway.secret_preferences,
updated_at: Time.current
)
migrated += 1
diff --git a/spree/providers/stripe/spec/lib/tasks/migrate_webhook_keys_spec.rb b/spree/providers/stripe/spec/lib/tasks/migrate_webhook_keys_spec.rb
new file mode 100644
index 0000000000..56753610ef
--- /dev/null
+++ b/spree/providers/stripe/spec/lib/tasks/migrate_webhook_keys_spec.rb
@@ -0,0 +1,52 @@
+require 'spec_helper'
+require 'rake'
+
+Rake::Task.define_task(:environment) unless Rake::Task.task_defined?(:environment)
+load SpreeStripe::Engine.root.join('lib', 'tasks', 'migrate_webhook_keys.rake')
+
+describe SpreeStripe::WebhookKeysMigrator do
+ let(:connection) { ActiveRecord::Base.connection }
+ let(:gateway) { create(:stripe_gateway) }
+
+ # The legacy tables are gone from 6.0 schemas, so they are built in the
+ # shape a 5.x install left behind — once, outside the example's transaction,
+ # because MySQL commits any open transaction when a table is created.
+ before(:all) do
+ connection = ActiveRecord::Base.connection
+ connection.create_table(described_class::LEGACY_KEYS_TABLE, force: true) do |t|
+ t.string :stripe_id
+ t.string :signing_secret
+ t.timestamps
+ end
+ connection.create_table(described_class::LEGACY_JOIN_TABLE, force: true) do |t|
+ t.bigint :payment_method_id
+ t.bigint :webhook_key_id
+ t.timestamps
+ end
+ end
+
+ after(:all) do
+ ActiveRecord::Base.connection.drop_table described_class::LEGACY_KEYS_TABLE, if_exists: true
+ ActiveRecord::Base.connection.drop_table described_class::LEGACY_JOIN_TABLE, if_exists: true
+ end
+
+ before do
+ key = described_class.new.send(:key_model).create!(stripe_id: 'we_legacy', signing_secret: 'whsec_legacy')
+ connection.insert(
+ "INSERT INTO #{described_class::LEGACY_JOIN_TABLE} (payment_method_id, webhook_key_id, created_at, updated_at) " \
+ "VALUES (#{gateway.id}, #{key.id}, #{connection.quote(Time.current)}, #{connection.quote(Time.current)})"
+ )
+ end
+
+ it 'moves the signing secret into the encrypted column, never into preferences' do
+ expect(described_class.new.call).to include(migrated: 1)
+
+ reloaded = SpreeStripe::Gateway.find(gateway.id)
+ expect(reloaded.preferred_webhook_signing_secret).to eq('whsec_legacy')
+ expect(reloaded.preferred_webhook_endpoint_id).to eq('we_legacy')
+ expect(reloaded.preferences).not_to have_key('webhook_signing_secret')
+
+ raw = connection.select_value("SELECT secret_preferences FROM spree_payment_methods WHERE id = #{gateway.id}")
+ expect(raw).not_to include('whsec_legacy')
+ end
+end