solidus_support 0.11.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 791e44bf714cfc82303b07994a80c9fa72c94e2ad92bf0e50cbf6d7fd67a6124
4
- data.tar.gz: a0b7e7d7d9e551d99bbc93f12e1e16bfce0ffb4b63ec5c908a52bbde8fe78565
3
+ metadata.gz: b29adfe3ba04b222f4644fb12d8e26d4b77f1f438d75ed4fe72873b2c29b1301
4
+ data.tar.gz: b5e87fb3449a317d500bc0a87d8e45f0efafbac61c825363dc2bf0dcf8374ed9
5
5
  SHA512:
6
- metadata.gz: 53f1fef22c5003effc4611881f89a74da814fa354b68a23b70f96e00888dd0625f925cd41b60ec841cfb41ee2e91717bfd9e61e721c17e82aa30f2fe78a5bef5
7
- data.tar.gz: 1f034253895ade97162295ef67797f0145b6c6ba153b3351f9a9fc83996dee4e926cd1f9ed9474478642d51760269aa2cf19185a43a10151d6d3a0cba89d7f85
6
+ metadata.gz: b0303b76dab3ad82ff50fe85cc4508c5750241a06a2d179a2feacf005c402017c1f15488e3eb1082147449d96ea040e5a3fd2df09bd74c675d9ceef7c87906f6
7
+ data.tar.gz: '068a4e6077b32e4e3a78577912fbcfbad23fede909822cb9c6a26d5d1f5346bc753d07ed38dd17cadf1e4787431b1032fc2551ea023c44a0974d75f7d7890680'
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/deprecation"
4
- require "flickwerk"
5
4
 
6
5
  module SolidusSupport
7
6
  module EngineExtensions
@@ -10,7 +9,6 @@ module SolidusSupport
10
9
 
11
10
  def self.included(engine)
12
11
  engine.extend ClassMethods
13
- engine.include Flickwerk
14
12
 
15
13
  engine.class_eval do
16
14
  solidus_decorators_root.glob('*') do |decorators_folder|
@@ -29,6 +27,25 @@ module SolidusSupport
29
27
  module ClassMethods
30
28
  def activate
31
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
32
49
  end
33
50
 
34
51
  # Loads decorator files.
@@ -107,17 +124,6 @@ module SolidusSupport
107
124
  end
108
125
  end
109
126
  end
110
-
111
- initializer "#{name}_#{engine}_patch_paths", before: "flickwerk.add_paths" do
112
- patch_paths = root.join("lib/patches/#{engine}").glob("*")
113
- Flickwerk.patch_paths += patch_paths
114
- end
115
-
116
- initializer "#{name}_#{engine}_user_patches", before: "flickwerk.add_patches" do
117
- Flickwerk.patches.transform_keys! do |key|
118
- key.gsub("Spree.user_class", Spree.user_class_name)
119
- end
120
- end
121
127
  end
122
128
  end
123
129
  end
@@ -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.11.1'
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
 
@@ -28,9 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
30
 
31
- spec.add_dependency 'flickwerk', '~> 0.2.0'
32
- spec.add_dependency 'solidus_core', '~> 4.1'
33
-
34
31
  spec.add_development_dependency 'rails'
35
32
  spec.add_development_dependency 'bundler'
36
33
  spec.add_development_dependency 'rake'
@@ -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.11.1
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
@@ -9,36 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-12-16 00:00:00.000000000 Z
12
+ date: 2025-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: flickwerk
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: 0.2.0
21
- type: :runtime
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: 0.2.0
28
- - !ruby/object:Gem::Dependency
29
- name: solidus_core
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - "~>"
33
- - !ruby/object:Gem::Version
34
- version: '4.1'
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '4.1'
42
14
  - !ruby/object:Gem::Dependency
43
15
  name: rails
44
16
  requirement: !ruby/object:Gem::Requirement
@@ -174,9 +146,14 @@ files:
174
146
  - lib/solidus_support.rb
175
147
  - lib/solidus_support/deprecator.rb
176
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
177
152
  - lib/solidus_support/migration.rb
178
153
  - lib/solidus_support/version.rb
179
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
180
157
  - spec/solidus_support_spec.rb
181
158
  - spec/spec_helper.rb
182
159
  - spec/support/dummy_app.rb
@@ -203,11 +180,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
180
  - !ruby/object:Gem::Version
204
181
  version: '0'
205
182
  requirements: []
206
- rubygems_version: 3.5.22
183
+ rubygems_version: 3.5.3
207
184
  signing_key:
208
185
  specification_version: 4
209
186
  summary: Common runtime helpers for Solidus extensions.
210
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
211
190
  - spec/solidus_support_spec.rb
212
191
  - spec/spec_helper.rb
213
192
  - spec/support/dummy_app.rb