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.

Convert PickupPointOption from Struct to an ActiveModel class

Damian Legawieca506ca4b8b63
diff --git a/CLAUDE.md b/CLAUDE.md
index 230ce1d4dd..f9f0c6d5dc 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -157,6 +157,7 @@ Per-request context available in models, controllers, jobs, and services:
- Include `Spree::Metafields` for custom fields support (see docs/plans/5.4-6.0-custom-fields-rename.md)
- Include `Spree::Metadata` for JSON metadata support
- ALWAYS Use string columns instead of enums
+- NEVER use `Struct` for domain value objects — use a plain Ruby class with `ActiveModel::Model` + `ActiveModel::Attributes` (typed attributes, validations) so it behaves like an ActiveRecord object (e.g. `Spree::PickupPointOption`)
- State machines: use `state_machines-activerecord` gem, default column `status` (legacy uses `state`, see docs/plans/6.0-normalize-state-to-status.md)
- NEVER cast IDs to integer — always treat as strings (UUID support)
- Uniqueness validations: ALWAYS use `scope: spree_base_uniqueness_scope`, should be also enforced by database index
diff --git a/spree/core/app/models/spree/pickup_point_option.rb b/spree/core/app/models/spree/pickup_point_option.rb
index cf949f0c09..f890960dd8 100644
--- a/spree/core/app/models/spree/pickup_point_option.rb
+++ b/spree/core/app/models/spree/pickup_point_option.rb
@@ -2,22 +2,27 @@ module Spree
# Ephemeral value object returned by PickupPointProvider queries. Never
# persisted — the selected option is frozen into
# +fulfillment.pickup_point_data+ as plain JSON.
- PickupPointOption = Struct.new(
- :external_id,
- :name,
- :address1,
- :address2,
- :city,
- :zipcode,
- :country_iso,
- :latitude,
- :longitude,
- :provider,
- :metadata,
- keyword_init: true
- ) do
+ class PickupPointOption
+ include ActiveModel::Model
+ include ActiveModel::Attributes
+
+ attribute :external_id, :string
+ attribute :name, :string
+ attribute :address1, :string
+ attribute :address2, :string
+ attribute :city, :string
+ attribute :zipcode, :string
+ attribute :country_iso, :string
+ attribute :latitude, :float
+ attribute :longitude, :float
+ attribute :provider, :string
+ attribute :metadata
+
+ validates :external_id, :name, presence: true
+
+ # @return [Hash] the JSON shape persisted into fulfillment.pickup_point_data
def to_pickup_point_data
- to_h.compact.stringify_keys
+ attributes.compact.stringify_keys
end
end
end
diff --git a/spree/core/spec/models/spree/pickup_point_option_spec.rb b/spree/core/spec/models/spree/pickup_point_option_spec.rb
new file mode 100644
index 0000000000..105e77a91a
--- /dev/null
+++ b/spree/core/spec/models/spree/pickup_point_option_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+RSpec.describe Spree::PickupPointOption, type: :model do
+ it 'validates the identifying attributes' do
+ expect(described_class.new(external_id: 'L1', name: 'Locker')).to be_valid
+ expect(described_class.new(name: 'Locker')).not_to be_valid
+ end
+
+ it 'serializes compacted string-keyed pickup point data' do
+ option = described_class.new(external_id: 'L1', name: 'Locker', latitude: 52.1, provider: 'test')
+
+ expect(option.to_pickup_point_data).to eq(
+ 'external_id' => 'L1', 'name' => 'Locker', 'latitude' => 52.1, 'provider' => 'test'
+ )
+ end
+end