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.

Move `Spree::Events` code from models to lib

Damian Legawiec477a4daa428b
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index dc26a807f6..87d97c6cfb 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -426,6 +426,7 @@ require 'spree/permitted_attributes'
require 'spree/service_module'
require 'spree/database_type_utilities'
require 'spree/analytics'
+require 'spree/events'
require 'spree/core/partials'
require 'spree/core/importer'
diff --git a/core/app/models/spree/events.rb b/core/lib/spree/events.rb
similarity index 97%
rename from core/app/models/spree/events.rb
rename to core/lib/spree/events.rb
index 13d27d046b..bf38eecdae 100644
--- a/core/app/models/spree/events.rb
+++ b/core/lib/spree/events.rb
@@ -1,5 +1,9 @@
# frozen_string_literal: true
+require_relative 'events/registry'
+require_relative 'events/adapters/base'
+require_relative 'events/adapters/active_support_notifications'
+
module Spree
# Main entry point for the Spree event system.
#
diff --git a/core/app/models/spree/events/adapters/active_support_notifications.rb b/core/lib/spree/events/adapters/active_support_notifications.rb
similarity index 100%
rename from core/app/models/spree/events/adapters/active_support_notifications.rb
rename to core/lib/spree/events/adapters/active_support_notifications.rb
diff --git a/core/app/models/spree/events/adapters/base.rb b/core/lib/spree/events/adapters/base.rb
similarity index 100%
rename from core/app/models/spree/events/adapters/base.rb
rename to core/lib/spree/events/adapters/base.rb
diff --git a/core/app/models/spree/events/registry.rb b/core/lib/spree/events/registry.rb
similarity index 100%
rename from core/app/models/spree/events/registry.rb
rename to core/lib/spree/events/registry.rb
diff --git a/core/spec/models/spree/events/registry_spec.rb b/core/spec/lib/spree/events/registry_spec.rb
similarity index 100%
rename from core/spec/models/spree/events/registry_spec.rb
rename to core/spec/lib/spree/events/registry_spec.rb
diff --git a/core/spec/models/spree/events_spec.rb b/core/spec/lib/spree/events_spec.rb
similarity index 100%
rename from core/spec/models/spree/events_spec.rb
rename to core/spec/lib/spree/events_spec.rb
diff --git a/core/spec/models/spree/event_spec.rb b/core/spec/models/spree/event_spec.rb
index b0e5aac99c..966d23272b 100644
--- a/core/spec/models/spree/event_spec.rb
+++ b/core/spec/models/spree/event_spec.rb
@@ -14,26 +14,25 @@ RSpec.describe Spree::Event do
expect(event.payload).to eq({ 'id' => 1, 'number' => 'R123' })
end
- it 'freezes the name, payload, and metadata' do
+ it 'freezes the payload and metadata' do
event = described_class.new(
name: 'order.complete',
payload: { id: 1 }
)
- expect(event.name).to be_frozen
expect(event.payload).to be_frozen
expect(event.metadata).to be_frozen
end
- it 'sets a timestamp' do
+ it 'sets created_at' do
event = described_class.new(name: 'test', payload: {})
- expect(event.timestamp).to be_within(1.second).of(Time.current)
+ expect(event.created_at).to be_within(1.second).of(Time.current)
end
- it 'generates an event_id in metadata' do
+ it 'generates an id' do
event = described_class.new(name: 'test', payload: {})
- expect(event.metadata['event_id']).to be_present
- expect(event.metadata['event_id']).to match(/\A[0-9a-f-]{36}\z/)
+ expect(event.id).to be_present
+ expect(event.id).to match(/\A[0-9a-f-]{36}\z/)
end
it 'includes spree_version in metadata' do
@@ -126,27 +125,27 @@ RSpec.describe Spree::Event do
end
end
- describe '#to_h' do
- it 'returns a hash representation with IDs as strings' do
+ describe '#attributes' do
+ it 'returns a hash representation with string keys' do
event = described_class.new(
name: 'order.complete',
payload: { id: 1 },
metadata: { user_id: 5 }
)
- hash = event.to_h
+ hash = event.attributes
- expect(hash[:name]).to eq('order.complete')
- expect(hash[:payload]).to eq({ 'id' => 1 })
- expect(hash[:metadata]['user_id']).to eq('5')
- expect(hash[:timestamp]).to eq(event.timestamp)
+ expect(hash['name']).to eq('order.complete')
+ expect(hash['payload']).to eq({ 'id' => 1 })
+ expect(hash['metadata']['user_id']).to eq('5')
+ expect(hash['created_at']).to eq(event.created_at)
end
end
describe '#inspect' do
it 'returns a readable string representation' do
event = described_class.new(name: 'order.complete', payload: {})
- expect(event.inspect).to match(/Spree::Event name="order.complete"/)
+ expect(event.inspect).to match(/Spree::Event id="[0-9a-f-]{36}" name="order.complete" created_at=/)
end
end
end