solidus_support 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88870615f95d8fe0794ee093b13f2875de6c84390a137352c196c7a6aa97d469
4
- data.tar.gz: 2aa7188af2b42c60e90201d4b366673ed4f1b8333cc31d11ce9da7fde534e659
3
+ metadata.gz: b29adfe3ba04b222f4644fb12d8e26d4b77f1f438d75ed4fe72873b2c29b1301
4
+ data.tar.gz: b5e87fb3449a317d500bc0a87d8e45f0efafbac61c825363dc2bf0dcf8374ed9
5
5
  SHA512:
6
- metadata.gz: 2f090251b1f4511057d6ec13781d63af4e951d3875818c157b7382e55c1d654a05ff464569af85fdafaa021a0ac3c998af29d29b3a70acb776c976e1becbb943
7
- data.tar.gz: dfa6ba3b3ac590ac623f1c707edfb1d09ce369435bb8508feba37feed3908dcacc59c35b9b01ecf3774c3378d42d68c038472c318596e689d30190b43d002b92
6
+ metadata.gz: b0303b76dab3ad82ff50fe85cc4508c5750241a06a2d179a2feacf005c402017c1f15488e3eb1082147449d96ea040e5a3fd2df09bd74c675d9ceef7c87906f6
7
+ data.tar.gz: '068a4e6077b32e4e3a78577912fbcfbad23fede909822cb9c6a26d5d1f5346bc753d07ed38dd17cadf1e4787431b1032fc2551ea023c44a0974d75f7d7890680'
@@ -27,6 +27,25 @@ module SolidusSupport
27
27
  module ClassMethods
28
28
  def activate
29
29
  load_solidus_decorators_from(solidus_decorators_root)
30
+ load_solidus_subscribers_from(solidus_subscribers_root)
31
+ end
32
+
33
+ # Loads Solidus event subscriber files.
34
+ #
35
+ # This allows to add event subscribers to extensions without explicitly subscribing them,
36
+ # similarly to what happens in Solidus core.
37
+ def load_solidus_subscribers_from(path)
38
+ if SolidusSupport::LegacyEventCompat.using_legacy?
39
+ path.glob("**/*_subscriber.rb") do |subscriber_path|
40
+ require_dependency(subscriber_path)
41
+ end
42
+
43
+ if Spree::Event.respond_to?(:activate_all_subscribers)
44
+ Spree::Event.activate_all_subscribers
45
+ else
46
+ Spree::Event.subscribers.each(&:subscribe!)
47
+ end
48
+ end
30
49
  end
31
50
 
32
51
  # Loads decorator files.
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusSupport
4
+ module LegacyEventCompat
5
+ # Compatibility for some event-driven operations
6
+ module Bus
7
+ # Publication of an event
8
+ #
9
+ # If extensions want to support the legacy sytem, they need to use a
10
+ # compatible API. That means it's not possible to publish an instance as
11
+ # event, which is something supported by Omnes but not the legacy adapter.
12
+ # Instead, a payload can be given. E.g.:
13
+ #
14
+ # ```
15
+ # SolidusSupport::LegacyEventCompat::Bus.publish(:foo, bar: :baz)
16
+ # ```
17
+ #
18
+ # Legacy subscribers will receive an
19
+ # `ActiveSupport::Notifications::Fanout`, while omnes subscribers will get
20
+ # an `Omnes::UnstructuredEvent`. Both instances are compatible as they
21
+ # implement a `#payload` method.
22
+ #
23
+ # @param event_name [Symbol]
24
+ # @param payload [Hash<Symbol, Any>]
25
+ def self.publish(event_name, **payload)
26
+ if SolidusSupport::LegacyEventCompat.using_legacy?
27
+ Spree::Event.fire(event_name, payload)
28
+ else
29
+ Spree::Bus.publish(event_name, **payload, caller_location: caller_locations(1)[0])
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "omnes"
5
+ rescue LoadError
6
+ end
7
+
8
+ module SolidusSupport
9
+ module LegacyEventCompat
10
+ # Compatibility for subscriber modules
11
+ #
12
+ # Thanks to this module, extensions can create legacy subscriber modules
13
+ # (see {Spree::Event::Subscriber}) and translate them automatically to an
14
+ # {Omnes::Subscriber}). E.g.:
15
+ #
16
+ # ```
17
+ # module MyExtension
18
+ # module MySubscriber
19
+ # include Spree::Event::Subscriber
20
+ # include SolidusSupport::LegacyEventCompat::Subscriber
21
+ #
22
+ # event_action :order_finalized
23
+ #
24
+ # def order_finalized(event)
25
+ # event.payload[:order].do_something
26
+ # end
27
+ # end
28
+ # end
29
+ #
30
+ # MyExtension::MySubscriber.omnes_subscriber.subscribe_to(Spree::Bus)
31
+ # ```
32
+ #
33
+ # The generated omnes subscriptions will call the corresponding legacy
34
+ # subscriber method with the omnes event. It'll compatible as long as the
35
+ # omnes event responds to the `#payload` method (see
36
+ # {Omnes::UnstructuredEvent}).
37
+ module Subscriber
38
+ # @api private
39
+ ADAPTER = lambda do |legacy_subscriber, legacy_subscriber_method, _omnes_subscriber, omnes_event|
40
+ legacy_subscriber.send(legacy_subscriber_method, omnes_event)
41
+ end
42
+
43
+ def self.included(legacy_subscriber)
44
+ legacy_subscriber.define_singleton_method(:omnes_subscriber) do
45
+ @omnes_subscriber ||= Class.new.include(::Omnes::Subscriber).tap do |subscriber|
46
+ legacy_subscriber.event_actions.each do |(legacy_subscriber_method, event_name)|
47
+ subscriber.handle(event_name.to_sym, with: ADAPTER.curry[legacy_subscriber, legacy_subscriber_method])
48
+ end
49
+ end.new
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_support/legacy_event_compat/bus'
4
+ require 'solidus_support/legacy_event_compat/subscriber'
5
+
6
+ module SolidusSupport
7
+ # Compatibility middleman for {Spree::Event} and {Spree::Bus}
8
+ #
9
+ # Solidus v3.2 changed to use [Omnes](https://github.com/nebulab/omnes) as the
10
+ # backbone for event-driven behavior (see {Spree::Bus}) by default. Before
11
+ # that, a custom adapter based on {ActiveSupport::Notifications} was used (see
12
+ # {Spree::Event}. Both systems are still supported on v3.2.
13
+ #
14
+ # This module provides compatibility support so that extensions can easily
15
+ # target both systems regardless of the underlying circumstances:
16
+ #
17
+ # - Solidus v3.2 with the new system.
18
+ # - Solidus v3.2 with the legacy system.
19
+ # - Solidus v2.9 to v3.1, when only {Spree::Event} existed.
20
+ # - Possible future versions of Solidus, whether the legacy system is
21
+ # eventually removed or not.
22
+ module LegacyEventCompat
23
+ # Returns whether the application is using the legacy event system
24
+ #
25
+ # @return [Boolean]
26
+ def self.using_legacy?
27
+ legacy_present? &&
28
+ (legacy_alone? ||
29
+ legacy_chosen?)
30
+ end
31
+
32
+ def self.legacy_present?
33
+ defined?(Spree::Event)
34
+ end
35
+ private_class_method :legacy_present?
36
+
37
+ def self.legacy_alone?
38
+ !Spree::Config.respond_to?(:use_legacy_events)
39
+ end
40
+ private_class_method :legacy_alone?
41
+
42
+ def self.legacy_chosen?
43
+ Spree::Config.use_legacy_events
44
+ end
45
+ private_class_method :legacy_chosen?
46
+ end
47
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusSupport
4
- VERSION = '0.12.0'
4
+ VERSION = '0.13.0'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'solidus_support/version'
4
4
  require 'solidus_support/deprecator'
5
5
  require 'solidus_support/migration'
6
+ require 'solidus_support/legacy_event_compat'
6
7
  require 'solidus_support/engine_extensions'
7
8
  require 'solidus_core'
8
9
 
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe SolidusSupport::LegacyEventCompat::Bus do
4
+ describe '#publish' do
5
+ if SolidusSupport::LegacyEventCompat.using_legacy?
6
+ it 'forwards to Spree::Event' do
7
+ box = nil
8
+ subscription = Spree::Event.subscribe(:foo) { |event| box = event.payload[:bar] }
9
+
10
+ described_class.publish(:foo, bar: :baz)
11
+
12
+ expect(box).to be(:baz)
13
+ ensure
14
+ Spree::Event.unsubscribe(subscription)
15
+ end
16
+ else
17
+ it 'forwards to Spree::Bus' do
18
+ box = nil
19
+ Spree::Bus.register(:foo)
20
+ subscription = Spree::Bus.subscribe(:foo) { |event| box = event.payload[:bar] }
21
+
22
+ described_class.publish(:foo, bar: :baz)
23
+
24
+ expect(box).to be(:baz)
25
+ ensure
26
+ Spree::Bus.unsubscribe(subscription)
27
+ Spree::Bus.registry.unregister(:foo)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omnes'
4
+
5
+ RSpec.describe SolidusSupport::LegacyEventCompat::Subscriber, if: Spree.solidus_version < Gem::Version.new("4.0.0.dev") do
6
+ subject { Module.new.include(Spree::Event::Subscriber).include(described_class) }
7
+
8
+ describe '#omnes_subscriber' do
9
+ it 'returns an Omnes::Subscriber' do
10
+ subject.module_eval do
11
+ event_action :foo
12
+
13
+ def foo(_event); end
14
+ end
15
+
16
+ expect(subject.omnes_subscriber.is_a?(Omnes::Subscriber)).to be(true)
17
+ end
18
+
19
+ it 'adds single-event definitions matching legacy event actions' do
20
+ subject.module_eval do
21
+ event_action :foo
22
+
23
+ def foo(_event); end
24
+ end
25
+ bus = Omnes::Bus.new
26
+ bus.register(:foo)
27
+
28
+ subscriptions = subject.omnes_subscriber.subscribe_to(bus)
29
+
30
+ event = Struct.new(:omnes_event_name).new(:foo)
31
+ expect(subscriptions.first.matches?(event)).to be(true)
32
+ end
33
+
34
+ it 'coerces event names given as Strings' do
35
+ subject.module_eval do
36
+ event_action 'foo'
37
+
38
+ def foo(_event); end
39
+ end
40
+ bus = Omnes::Bus.new
41
+ bus.register(:foo)
42
+
43
+ subscriptions = subject.omnes_subscriber.subscribe_to(bus)
44
+
45
+ event = Struct.new(:omnes_event_name).new(:foo)
46
+ expect(subscriptions.first.matches?(event)).to be(true)
47
+ end
48
+
49
+ it 'executes legacy event action methods as handlers with the omnes event' do
50
+ subject.module_eval do
51
+ event_action :foo
52
+
53
+ def foo(event)
54
+ event[:bar]
55
+ end
56
+ end
57
+ bus = Omnes::Bus.new
58
+ bus.register(:foo)
59
+
60
+ subscriptions = subject.omnes_subscriber.subscribe_to(bus)
61
+
62
+ expect(
63
+ bus.publish(:foo, bar: :baz).executions.first.result
64
+ ).to be(:baz)
65
+ end
66
+
67
+ it 'distingish when event name is given explicitly' do
68
+ subject.module_eval do
69
+ event_action :foo, event_name: :bar
70
+
71
+ def foo(_event)
72
+ :bar
73
+ end
74
+ end
75
+ bus = Omnes::Bus.new
76
+ bus.register(:bar)
77
+
78
+ subscriptions = subject.omnes_subscriber.subscribe_to(bus)
79
+
80
+ expect(
81
+ bus.publish(:bar).executions.first.result
82
+ ).to be(:bar)
83
+ end
84
+
85
+ it "returns the same omnes subscriber instance if called again" do
86
+ expect(subject.omnes_subscriber).to be(subject.omnes_subscriber)
87
+ end
88
+
89
+ it "doesn't fail when no event action has been defined" do
90
+ expect { subject.omnes_subscriber }.not_to raise_error
91
+ end
92
+ end
93
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-12-17 00:00:00.000000000 Z
12
+ date: 2025-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -146,9 +146,14 @@ files:
146
146
  - lib/solidus_support.rb
147
147
  - lib/solidus_support/deprecator.rb
148
148
  - lib/solidus_support/engine_extensions.rb
149
+ - lib/solidus_support/legacy_event_compat.rb
150
+ - lib/solidus_support/legacy_event_compat/bus.rb
151
+ - lib/solidus_support/legacy_event_compat/subscriber.rb
149
152
  - lib/solidus_support/migration.rb
150
153
  - lib/solidus_support/version.rb
151
154
  - solidus_support.gemspec
155
+ - spec/solidus_support/legacy_event_compat/bus_spec.rb
156
+ - spec/solidus_support/legacy_event_compat/legacy_event_compat_spec.rb
152
157
  - spec/solidus_support_spec.rb
153
158
  - spec/spec_helper.rb
154
159
  - spec/support/dummy_app.rb
@@ -175,11 +180,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
180
  - !ruby/object:Gem::Version
176
181
  version: '0'
177
182
  requirements: []
178
- rubygems_version: 3.5.22
183
+ rubygems_version: 3.5.3
179
184
  signing_key:
180
185
  specification_version: 4
181
186
  summary: Common runtime helpers for Solidus extensions.
182
187
  test_files:
188
+ - spec/solidus_support/legacy_event_compat/bus_spec.rb
189
+ - spec/solidus_support/legacy_event_compat/legacy_event_compat_spec.rb
183
190
  - spec/solidus_support_spec.rb
184
191
  - spec/spec_helper.rb
185
192
  - spec/support/dummy_app.rb