diff --git a/.circleci/config.yml b/.circleci/config.yml index 57b3117a284..815405f0ca5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -264,9 +264,6 @@ jobs: paperclip: type: boolean default: true - legacy_events: - type: boolean - default: false executor: name: << parameters.database >> ruby: << parameters.ruby >> @@ -274,7 +271,6 @@ jobs: environment: DISABLE_ACTIVE_STORAGE: << parameters.paperclip >> RAILS_VERSION: "~> << parameters.rails >>" - USE_LEGACY_EVENTS: << parameters.legacy_events >> BUILDKITE_ANALYTICS_EXECUTION_NAME_PREFIX: "(<< parameters.ruby >>:<< parameters.rails >>:<< parameters.database >>:<< parameters.paperclip >>)" steps: - setup @@ -328,12 +324,12 @@ workflows: # Based on supported versions for the current Solidus release and recommended versions from # https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html. - test_solidus: - name: &name "test-rails-<>-ruby-<>-<>-<<#matrix.paperclip>>paperclip<><<^matrix.paperclip>>activestorage<><<#matrix.legacy_events>>-legacy_events<>" - matrix: { parameters: { rails: ['7.0'], ruby: ['3.1', '3.2'], database: ['mysql', 'sqlite', 'postgres'], paperclip: [true, false], legacy_events: [false] } } + name: &name "test-rails-<>-ruby-<>-<>-<<#matrix.paperclip>>paperclip<><<^matrix.paperclip>>activestorage<>" + matrix: { parameters: { rails: ['7.0'], ruby: ['3.1', '3.2'], database: ['mysql', 'sqlite', 'postgres'], paperclip: [true, false] } } - test_solidus: name: *name - matrix: { parameters: { rails: ['6.1'], ruby: ['2.7', '3.0', '3.1'], database: ['sqlite'], paperclip: [false], legacy_events: [false] } } + matrix: { parameters: { rails: ['6.1'], ruby: ['2.7', '3.0', '3.1'], database: ['sqlite'], paperclip: [false] } } - test_solidus: name: *name - matrix: { parameters: { rails: ['6.0'], ruby: ['2.7'], database: ['sqlite'], paperclip: [true], legacy_events: [true] } } + matrix: { parameters: { rails: ['6.0'], ruby: ['2.7'], database: ['sqlite'], paperclip: [true] } } - dev_tools diff --git a/core/app/subscribers/spree/mailer_subscriber.rb b/core/app/subscribers/spree/mailer_subscriber.rb deleted file mode 100644 index 083dacd863b..00000000000 --- a/core/app/subscribers/spree/mailer_subscriber.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require 'spree/event/subscriber' - -module Spree - # Legacy subscriber - # - # This subscriber module is used by the legacy pub/sub system (see - # {Spree::Event}). - module MailerSubscriber - include Spree::Event::Subscriber - - event_action :order_finalized - event_action :send_reimbursement_email, event_name: :reimbursement_reimbursed - - def order_finalized(event) - order = event.payload[:order] - unless order.confirmation_delivered? - Spree::Config.order_mailer_class.confirm_email(order).deliver_later - order.update_column(:confirmation_delivered, true) - end - end - - def send_reimbursement_email(event) - reimbursement = event.payload[:reimbursement] - Spree::Config.reimbursement_mailer_class.reimbursement_email(reimbursement.id).deliver_later - end - end -end diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 4a62d78b9ea..7fb600dc95d 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -280,14 +280,6 @@ class AppConfiguration < Preferences::Configuration # @return [] Track on_hand values for variants / products. (default: true) preference :track_inventory_levels, :boolean, default: true - # @!attribute [rw] use_legacy_events - # Before v3.2, Solidus used a custom pub/sub implementation based on - # ActiveSupport::Notifications. Now, we internally use and recommend - # [Omnes](https://github.com/nebulab/omnes). This preference allows falling - # back to the old system. - # @return [Boolean] - versioned_preference :use_legacy_events, :boolean, initial_value: true, boundaries: { "3.2.0.alpha" => false } - # Other configurations # Allows restricting what currencies will be available. @@ -609,10 +601,6 @@ def roles end end - def events - @events_configuration ||= Spree::Event::Configuration.new - end - def user_last_url_storer_rules @user_last_url_storer_rules ||= ::Spree::Core::ClassConstantizer::Set.new.tap do |set| set << 'Spree::UserLastUrlStorer::Rules::AuthenticationRule' diff --git a/core/lib/spree/bus.rb b/core/lib/spree/bus.rb index e386e411617..a92a8fd2922 100644 --- a/core/lib/spree/bus.rb +++ b/core/lib/spree/bus.rb @@ -5,16 +5,5 @@ module Spree # # This is used for internal events, while host applications are also able to # use it. - # - # It has some modifications to support internal usage of the legacy event - # system {see Spree::AppConfiguration#use_legacy_events}. Bus = Omnes::Bus.new - def Bus.publish(event, **kwargs) - if Spree::Config.use_legacy_events - Spree::Event.fire(event, **kwargs) - else - # Override caller_location to point to the actual event publisher - super(event, **kwargs, caller_location: caller_locations(1)[0]) - end - end end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index f69671a4b9b..62dda99c6cc 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -94,7 +94,6 @@ class GatewayError < RuntimeError; end require 'spree/core/environment' require 'spree/migrations' require 'spree/migration_helpers' -require 'spree/event' require 'spree/bus' require 'spree/core/engine' diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index a3375ceaf31..807f05cfe83 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -52,27 +52,17 @@ class Engine < ::Rails::Engine # Setup pub/sub initializer 'spree.core.pub_sub' do |app| - if Spree::Config.use_legacy_events - app.reloader.to_prepare do - Spree::Event.activate_autoloadable_subscribers - end - - app.reloader.before_class_unload do - Spree::Event.deactivate_all_subscribers - end - else - app.reloader.to_prepare do - Spree::Bus.clear - - %i[ - order_finalized - order_recalculated - reimbursement_reimbursed - reimbursement_errored - ].each { |event_name| Spree::Bus.register(event_name) } - - Spree::OrderMailerSubscriber.new.subscribe_to(Spree::Bus) - end + app.reloader.to_prepare do + Spree::Bus.clear + + %i[ + order_finalized + order_recalculated + reimbursement_reimbursed + reimbursement_errored + ].each { |event_name| Spree::Bus.register(event_name) } + + Spree::OrderMailerSubscriber.new.subscribe_to(Spree::Bus) end end @@ -97,16 +87,6 @@ class Engine < ::Rails::Engine Spree::UsersController.protect_from_forgery with: :exception end end - - config.after_initialize do - if Spree::Config.use_legacy_events && !ENV['CI'] - Spree::Deprecation.warn <<~MSG - Your Solidus store is using the legacy event system. You're - encouraged to switch to the new event bus. After you're done, you - can remove the `use_legacy_events` setting from `spree.rb`. - MSG - end - end end end end diff --git a/core/lib/spree/event.rb b/core/lib/spree/event.rb deleted file mode 100644 index 6a4f3ffe7df..00000000000 --- a/core/lib/spree/event.rb +++ /dev/null @@ -1,119 +0,0 @@ -# frozen_string_literal: true - -require_relative 'event/adapters/active_support_notifications' -require_relative 'event/subscriber_registry' -require_relative 'event/configuration' -require_relative 'event/subscriber' - -module Spree - module Event - extend self - - delegate :activate_autoloadable_subscribers, :activate_all_subscribers, :deactivate_all_subscribers, to: :subscriber_registry - - # Allows to trigger events that can be subscribed using #subscribe. An - # optional block can be passed that will be executed immediately. The - # actual code implementation is delegated to the adapter. - # - # @param [String] event_name the name of the event. The suffix ".spree" - # will be added automatically if not present - # @param [Hash] opts a list of options to be passed to the triggered event - # - # @example Trigger an event named 'order_finalized' - # Spree::Event.fire 'order_finalized', order: @order do - # @order.complete! - # end - def fire(event_name, opts = {}) - adapter.fire normalize_name(event_name), opts do - yield opts if block_given? - end - end - - # Subscribe to an event with the given name. The provided block is executed - # every time the subscribed event is fired. - # - # @param [String, Regexp] event_name the name of the event. - # When String, the suffix ".spree" will be added automatically if not present, - # when using the default adapter for ActiveSupportNotifications. - # When Regexp, due to the unpredictability of all possible regexp combinations, - # adding the suffix is developer's responsibility (if you don't, you will - # subscribe to all notifications, including internal Rails notifications - # as well). - # - # @see Spree::Event::Adapters::ActiveSupportNotifications#normalize_name - # - # @return a subscription object that can be used as reference in order - # to remove the subscription - # - # @example Subscribe to the `order_finalized` event - # Spree::Event.subscribe 'order_finalized' do |event| - # order = event.payload[:order] - # Spree::Mailer.order_finalized(order).deliver_later - # end - # - # @see Spree::Event#unsubscribe - def subscribe(event_name, &block) - name = normalize_name(event_name) - listener_names << name - adapter.subscribe(name, &block) - end - - # Unsubscribes a whole event or a specific subscription object - # - # @param [String, Object] subscriber the event name as a string (with - # or without the ".spree" suffix) or the subscription object - # - # @example Unsubscribe a single subscription - # subscription = Spree::Event.fire 'order_finalized' - # Spree::Event.unsubscribe(subscription) - # @example Unsubscribe all `order_finalized` event subscriptions - # Spree::Event.unsubscribe('order_finalized') - # @example Unsubscribe an event by name with explicit prefix - # Spree::Event.unsubscribe('order_finalized.spree') - def unsubscribe(subscriber) - name_or_subscriber = subscriber.is_a?(String) ? normalize_name(subscriber) : subscriber - adapter.unsubscribe(name_or_subscriber) - end - - # Lists all subscriptions currently registered under the ".spree" - # namespace. Actual implementation is delegated to the adapter - # - # @return [Hash] an hash with event names as keys and arrays of subscriptions - # as values - # - # @example Current subscriptions - # Spree::Event.listeners - # # => {"order_finalized.spree"=> [#], - # "reimbursement_reimbursed.spree"=> [#]} - def listeners - adapter.listeners_for(listener_names) - end - - # The adapter used by Spree::Event, defaults to - # Spree::Event::Adapters::ActiveSupportNotifications - # - # @example Change the adapter - # Spree::Config.events.adapter = "Spree::EventBus.new" - # - # @see Spree::AppConfiguration - def adapter - Spree::Config.events.adapter - end - - # @!attribute [r] subscribers - # @return The registry for supporting class reloading for Spree::Event::Subscriber instances - def subscriber_registry - Spree::Config.events.subscriber_registry - end - - private - - def normalize_name(name) - adapter.normalize_name(name) - end - - def listener_names - @listeners_names ||= Set.new - end - end -end diff --git a/core/lib/spree/event/adapters/active_support_notifications.rb b/core/lib/spree/event/adapters/active_support_notifications.rb deleted file mode 100644 index cc3a9fd86ee..00000000000 --- a/core/lib/spree/event/adapters/active_support_notifications.rb +++ /dev/null @@ -1,67 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Event - module Adapters - module ActiveSupportNotifications - class InvalidEventNameType < StandardError; end - - extend self - - def fire(event_name, opts) - ActiveSupport::Notifications.instrument event_name, opts do - yield opts if block_given? - end - end - - def subscribe(event_name) - ActiveSupport::Notifications.subscribe event_name do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - yield event - end - end - - def unsubscribe(subscriber_or_name) - ActiveSupport::Notifications.unsubscribe(subscriber_or_name) - end - - def listeners_for(names) - names.each_with_object({}) do |name, memo| - listeners = ActiveSupport::Notifications.notifier.listeners_for(name) - memo[name] = listeners if listeners.present? - end - end - - # Normalizes the event name according to this specific adapter rules. - # When the event name is a string or a symbol, if the suffix is missing, then - # it is added automatically. - # When the event name is a regexp, due to the huge variability of regexps, adding - # or not the suffix is developer's responsibility (if you don't, you will subscribe - # to all internal rails events as well). - # When the event type is not supported, an error is raised. - # - # @param [String, Symbol, Regexp] event_name the event name, with or without the - # suffix (Spree::Config.events.suffix defaults to `.spree`). - def normalize_name(event_name) - case event_name - when Regexp - event_name - when String, Symbol - name = event_name.to_s - name.end_with?(suffix) ? name : [name, suffix].join - else - raise InvalidEventNameType, "Invalid event name type: #{event_name.class}" - end - end - - # The suffix used for namespacing event names, defaults to - # `.spree` - # - # @see Spree::Event::Configuration#suffix - def suffix - Spree::Config.events.suffix - end - end - end - end -end diff --git a/core/lib/spree/event/configuration.rb b/core/lib/spree/event/configuration.rb deleted file mode 100644 index 658dc37cea2..00000000000 --- a/core/lib/spree/event/configuration.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Event - class Configuration - def subscriber_registry - @subscriber_registry ||= Spree::Event::SubscriberRegistry.new - end - - attr_writer :adapter, :suffix, :autoload_subscribers - - def autoload_subscribers - @autoload_subscribers.nil? ? true : !!@autoload_subscribers - end - - def adapter - @adapter ||= Spree::Event::Adapters::ActiveSupportNotifications - end - - def suffix - @suffix ||= '.spree' - end - end - end -end diff --git a/core/lib/spree/event/subscriber.rb b/core/lib/spree/event/subscriber.rb deleted file mode 100644 index 78773ae3939..00000000000 --- a/core/lib/spree/event/subscriber.rb +++ /dev/null @@ -1,86 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Event - # This module simplifies adding and removing subscriptions to {Spree::Event} events. - # Here's a complete example: - # module EmailSender - # include Spree::Event::Subscriber - # - # event_action :order_finalized - # event_action :confirm_reimbursement, event_name: :reimbursement_reimbursed - # - # def order_finalized(event) - # Mailer.send_email(event.payload[:order]) - # end - # - # def confirm_reimbursement(event) - # Mailer.send_email(event.payload[:reimbursement]) - # end - # end - # - # # Optional, required only when the subscriber needs to be loaded manually. - # # - # # If Spree::Config.events.autoload_subscribers is set to `true` and the module - # # file matches the pattern `app/subscribers/**/*_subscriber.rb` then it will - # # be loaded automatically at boot and this line can be removed: - # EmailSender.activate - module Subscriber - def self.included(base) - base.extend base - - base.mattr_accessor :event_actions - base.event_actions = {} - - Spree::Event.subscriber_registry.register(base) - end - - # Declares a method name in the including module that can be subscribed/unsubscribed - # to an event. - # - # @param method_name [String, Symbol] the method that will be called when the subscribed event is fired - # @param event_name [String, Symbol] the name of the event to be subscribed - # - # @example Declares 'send_email' as an event action that can subscribe the event 'order_finalized' - # module EmailSender - # event_action :send_email, event_name: :order_finalized - # - # def send_email(event) - # Mailer.send_email(event.payload[:order]) - # end - # end - # - # @example Same as above, but the method name is same as the event name: - # module EmailSender - # event_action :order_completed - # - # def order_completed(event) - # Mailer.send_email(event.payload[:order]) - # end - # end - def event_action(method_name, event_name: nil) - event_actions[method_name] = (event_name || method_name).to_s - end - - # Activates all declared event actions to their events. Only actions that are activated - # will be called when their event fires. - # - # @example activate all event actions for module 'EmailSender' - # EmailSender.activate - def activate - Spree::Event.subscriber_registry.activate_subscriber(self) - end - - # Deactivates all declared event actions (or a single specific one) from their events. - # This means that when an event fires then none of its unsubscribed event actions will - # be called. - # @example deactivate all event actions for module 'EmailSender' - # EmailSender.deactivate - # @example deactivate only order_finalized for module 'EmailSender' - # EmailSender.deactivate(:order_finalized) - def deactivate(event_action_name = nil) - Spree::Event.subscriber_registry.deactivate_subscriber(self, event_action_name) - end - end - end -end diff --git a/core/lib/spree/event/subscriber_registry.rb b/core/lib/spree/event/subscriber_registry.rb deleted file mode 100644 index 4e2610468fb..00000000000 --- a/core/lib/spree/event/subscriber_registry.rb +++ /dev/null @@ -1,92 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Event - class SubscriberRegistry - def initialize - @registry = {} - @semaphore = Mutex.new - end - - def register(subscriber) - registry[subscriber.name] ||= {} - end - - def activate_autoloadable_subscribers - require_subscriber_files - activate_all_subscribers - end - - def activate_all_subscribers - registry.each_key { |subscriber_name| activate_subscriber(subscriber_name.constantize) } - end - - def deactivate_all_subscribers - registry.each_key { |subscriber_name| deactivate_subscriber(subscriber_name.constantize) } - end - - def activate_subscriber(subscriber) - return unless registry[subscriber.name] - - subscriber.event_actions.each do |event_action, event_name| - @semaphore.synchronize do - unsafe_deactivate_subscriber(subscriber, event_action) - - subscription = Spree::Event.subscribe(event_name) { |event| subscriber.send(event_action, event) } - - # deprecated mappings, to be removed when Solidus 2.10 is not supported anymore: - if subscriber.respond_to?("#{event_action}_handler=") - subscriber.send("#{event_action}_handler=", subscription) - end - - registry[subscriber.name][event_action] = subscription - end - end - end - - def deactivate_subscriber(subscriber, event_action_name = nil) - @semaphore.synchronize do - unsafe_deactivate_subscriber(subscriber, event_action_name) - end - end - - private - - attr_reader :registry - - # Loads all Solidus' core and application's event subscribers files. - # The latter are loaded automatically only when the preference - # Spree::Config.events.autoload_subscribers is set to a truthy value. - # - # Files must be placed under the directory `app/subscribers` and their - # name must end with `_subscriber.rb`. - # - # Loading the files has the side effect of adding their module to the - # list in Spree::Event.subscribers. - def require_subscriber_files - require_dependency( - Spree::Core::Engine.root.join('app', 'subscribers', 'spree', 'mailer_subscriber.rb') - ) - - pattern = "app/subscribers/**/*_subscriber.rb" - - # Load application subscribers, only when the flag is set to true: - if Spree::Config.events.autoload_subscribers - Rails.root.glob(pattern) { |c| require_dependency(c.to_s) } - end - end - - def unsafe_deactivate_subscriber(subscriber, event_action_name = nil) - to_unsubscribe = Array.wrap(event_action_name || subscriber.event_actions.keys) - - to_unsubscribe.each do |event_action| - if (subscription = registry.dig(subscriber.name, event_action)) - Spree::Event.unsubscribe(subscription) - - registry[subscriber.name].delete(event_action) - end - end - end - end - end -end diff --git a/core/lib/spree/testing_support/dummy_app.rb b/core/lib/spree/testing_support/dummy_app.rb index efce592b48b..9bf66f77017 100644 --- a/core/lib/spree/testing_support/dummy_app.rb +++ b/core/lib/spree/testing_support/dummy_app.rb @@ -138,8 +138,6 @@ class Application < ::Rails::Application Spree.user_class = 'Spree::LegacyUser' Spree.load_defaults(Spree.solidus_version) Spree.config do |config| - config.use_legacy_events = (ENV['USE_LEGACY_EVENTS'] == 'true') - if (ENV['DISABLE_ACTIVE_STORAGE'] == 'true') config.image_attachment_module = 'Spree::Image::PaperclipAttachment' config.taxon_attachment_module = 'Spree::Taxon::PaperclipAttachment' diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index 722435e4bf0..46f83de4b69 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -157,8 +157,4 @@ class DummyClass; end; expect(prefs.admin_vat_location.state_id).to eq(nil) expect(prefs.admin_vat_location.country_id).to eq(nil) end - - it 'has default Event adapter' do - expect(prefs.events.adapter).to eq Spree::Event::Adapters::ActiveSupportNotifications - end end diff --git a/core/spec/lib/spree/bus_spec.rb b/core/spec/lib/spree/bus_spec.rb deleted file mode 100644 index 6d754129951..00000000000 --- a/core/spec/lib/spree/bus_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Spree::Bus do - describe '.publish' do - it "doesn't coerce events responding to #to_hash", - if: (Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")) && !Spree::Config.use_legacy_events do - described_class.register(:foo) - event = Class.new do - def omnes_event_name - :foo - end - - def to_hash - { foo: 'bar' } - end - end.new - - expect { described_class.publish(event) }.not_to raise_error - ensure - described_class.registry.unregister(:foo) - end - end -end diff --git a/core/spec/lib/spree/event/adapters/active_support_notifications_spec.rb b/core/spec/lib/spree/event/adapters/active_support_notifications_spec.rb deleted file mode 100644 index b8d73a2e8bf..00000000000 --- a/core/spec/lib/spree/event/adapters/active_support_notifications_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'spree/event' - -module Spree - module Event - module Adapters - RSpec.describe ActiveSupportNotifications do - describe "#normalize_name" do - subject { described_class.normalize_name(event_name) } - - context "when event name is a string" do - let(:event_name) { "foo" } - - it "adds the suffix to the event name" do - expect(subject).to eql "foo.spree" - end - end - - context "when event name is a regexp" do - let(:event_name) { /.*/ } - - it "never changes the regexp" do - expect(subject).to eq event_name - end - end - - context "when event name is a class not handled" do - let(:event_name) { Object.new } - - it "raises a InvalidEventNameType error" do - expect { subject }.to raise_error(described_class::InvalidEventNameType) - end - end - end - end - end - end -end diff --git a/core/spec/lib/spree/event/subscriber_registry_spec.rb b/core/spec/lib/spree/event/subscriber_registry_spec.rb deleted file mode 100644 index 33fd0ed88ed..00000000000 --- a/core/spec/lib/spree/event/subscriber_registry_spec.rb +++ /dev/null @@ -1,115 +0,0 @@ -# frozen_string_literal: true -require 'active_support/all' -require 'spec_helper' -require 'spree/event' - -RSpec.describe Spree::Event::SubscriberRegistry do - module N - include Spree::Event::Subscriber - - event_action :event_name - event_action :other_event - - def event_name(event) - # code that handles the event - end - - def other_event(event) - # code that handles the event - end - end - - describe "#activate_all_subscribers" do - before { subject.register(N) } - - it "delegates to #activate_subscriber for each registered subscriber" do - expect(subject).to receive(:activate_subscriber).with(N) - - subject.activate_all_subscribers - end - end - - describe "#deactivate_all_subscribers" do - before { subject.register(N) } - - it "delegates to #deactivate_subscriber for each registered subscriber" do - expect(subject).to receive(:deactivate_subscriber).with(N) - - subject.deactivate_all_subscribers - end - end - - describe "#activate_subscriber" do - before do - N.deactivate - Spree::Event.subscriber_registry.send(:registry).delete("N") - end - - context "with an unregistered subscriber" do - it "does not activate the subscriber" do - expect(N).not_to receive(:event_name) - - subject.activate_subscriber(N) - Spree::Event.fire(:event_name) - end - end - - context "with a registered subscriber" do - before { subject.register(N) } - - it "activates the subscriber" do - expect(N).to receive(:event_name) - - subject.activate_subscriber(N) - Spree::Event.fire(:event_name) - end - - after { subject.deactivate_subscriber(N) } - end - end - - describe "#deactivate_subscriber" do - context "with a unregistered subscriber" do - before do - N.deactivate - Spree::Event.subscriber_registry.send(:registry).delete("N") - end - - it { expect { subject.deactivate_subscriber(N) }.not_to raise_error } - end - - context "with a registered subscriber" do - before { subject.register(N) } - - after { subject.deactivate_subscriber(N) } - - context "with the module was activated" do - before { subject.activate_subscriber(N) } - - context "when deactivating the whole module" do - it "removes all the module event callbacks" do - expect(N).not_to receive(:event_name) - expect(N).not_to receive(:other_event) - - subject.deactivate_subscriber(N) - Spree::Event.fire(:event_name) - Spree::Event.fire(:other_event) - end - end - - context "when unsubscribing a single event action" do - context "when deactivating a single event action" do - it "removes the single event action callbacks" do - expect(N).not_to receive(:event_name) - expect(N).to receive(:other_event) - - subject.deactivate_subscriber(N, :event_name) - Spree::Event.fire(:event_name) - Spree::Event.fire(:other_event) - end - end - end - end - end - end -end diff --git a/core/spec/lib/spree/event/subscriber_spec.rb b/core/spec/lib/spree/event/subscriber_spec.rb deleted file mode 100644 index 06b006815b3..00000000000 --- a/core/spec/lib/spree/event/subscriber_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -# frozen_string_literal: true - -require 'active_support/all' -require 'spec_helper' -require 'spree/event' - -RSpec.describe Spree::Event::Subscriber do - module M - include Spree::Event::Subscriber - - event_action :event_name - - def event_name(event) - # code that handles the event - end - - def other_event(event) - # not registered via event_action - end - end - - describe '::activate' do - before { M.deactivate } - - it 'adds new listeners to Spree::Event' do - expect { M.activate }.to change { Spree::Event.listeners } - end - - context 'when subscriptions are not registered' do - it 'does not trigger the event callback' do - expect(M).not_to receive(:event_name) - Spree::Event.fire 'event_name' - end - end - - it 'subscribes event actions' do - M.activate - expect(M).to receive(:event_name) - Spree::Event.fire 'event_name' - end - - it 'does not subscribe event actions more than once' do - 2.times { M.activate } - expect(M).to receive(:event_name).once - Spree::Event.fire 'event_name' - end - end - - describe '::deactivate' do - before { M.activate } - - it 'removes the subscription' do - expect(M).not_to receive(:event_name) - M.deactivate - Spree::Event.fire 'event_name' - end - end - - describe '::event_action' do - context 'when the action has not been declared' do - before { M.activate } - - it 'does not subscribe the action' do - expect(M).not_to receive(:other_event) - Spree::Event.fire 'other_event' - end - end - - context 'when the action is declared' do - before do - M.event_action :other_event - M.activate - end - - after do - M.deactivate - M.event_actions.delete(:other_event) - end - - it 'subscribe the action' do - expect(M).to receive(:other_event) - Spree::Event.fire 'other_event' - end - end - end -end diff --git a/core/spec/lib/spree/event_spec.rb b/core/spec/lib/spree/event_spec.rb deleted file mode 100644 index 16f2ada45a2..00000000000 --- a/core/spec/lib/spree/event_spec.rb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Spree::Event do - let(:subscription_name) { 'foo_bar' } - let(:item) { spy('object') } - let(:notifier) { ActiveSupport::Notifications.notifier } - - subject { described_class } - - it 'has default adapter' do - expect(subject.adapter).to eql Spree::Event::Adapters::ActiveSupportNotifications - end - - context 'with the default adapter' do - before do - # ActiveSupport::Notifications does not provide an interface to clean all - # subscribers at once, so some low level brittle code is required - if Rails.gem_version >= Gem::Version.new('6.0.0') - @old_string_subscribers = notifier.instance_variable_get('@string_subscribers').dup - @old_other_subscribers = notifier.instance_variable_get('@other_subscribers').dup - notifier.instance_variable_get('@string_subscribers').clear - notifier.instance_variable_get('@other_subscribers').clear - else - @old_subscribers = notifier.instance_variable_get('@subscribers').dup - notifier.instance_variable_get('@subscribers').clear - end - @old_listeners = notifier.instance_variable_get('@listeners_for').dup - notifier.instance_variable_get('@listeners_for').clear - end - - after do - if Rails.gem_version >= Gem::Version.new('6.0.0') - notifier.instance_variable_set '@string_subscribers', @old_string_subscribers - notifier.instance_variable_set '@other_subscribers', @old_other_subscribers - else - notifier.instance_variable_set '@subscribers', @old_subscribers - end - notifier.instance_variable_set '@listeners_for', @old_listeners - end - - describe '#listeners' do - context 'when there is no subscription' do - it { expect(subject.listeners).to be_empty } - - context 'after adding a subscription' do - before do - Spree::Event.subscribe(subscription_name) { item.do_something } - end - - it 'includes the new subscription with custom suffix' do - expect(subject.listeners).to be_present - subscription_listeners = subject.listeners["#{subscription_name}.spree"] - expect(subscription_listeners).to be_a Array - expect(subscription_listeners.first).to be_a ActiveSupport::Notifications::Fanout::Subscribers::Timed - end - end - end - end - - context 'subscriptions' do - describe '#subscribe' do - it 'can subscribe to events' do - Spree::Event.subscribe(subscription_name) { item.do_something } - Spree::Event.fire subscription_name - expect(item).to have_received :do_something - end - - it 'can subscribe to multiple events using a regexp' do - Spree::Event.subscribe(/.*\.spree$/) { item.do_something_else } - Spree::Event.fire subscription_name - Spree::Event.fire 'another_event' - expect(item).to have_received(:do_something_else).twice - end - end - - describe '#unsubscribe' do - context 'when unsubscribing using a subscription object as reference' do - let!(:subscription) { Spree::Event.subscribe(subscription_name) { item.do_something } } - - before do - Spree::Event.subscribe(subscription_name) { item.do_something_else } - end - - it 'can unsubscribe from single event by object' do - subject.unsubscribe subscription - Spree::Event.fire subscription_name - expect(item).not_to have_received :do_something - expect(item).to have_received :do_something_else - end - end - - context 'when unsubscribing using a string as reference' do - before do - Spree::Event.subscribe(subscription_name) { item.do_something } - Spree::Event.subscribe(subscription_name) { item.do_something_else } - end - - it 'can unsubscribe from multiple events with the same name' do - subject.unsubscribe subscription_name - Spree::Event.fire subscription_name - expect(item).not_to have_received :do_something - expect(item).not_to have_received :do_something_else - end - end - end - end - end -end diff --git a/core/spec/models/spree/order/finalizing_spec.rb b/core/spec/models/spree/order/finalizing_spec.rb index 99747889c60..b140e50dad2 100644 --- a/core/spec/models/spree/order/finalizing_spec.rb +++ b/core/spec/models/spree/order/finalizing_spec.rb @@ -86,42 +86,6 @@ order.complete! end - - if Spree::Config.use_legacy_events - # These specs show how notifications can be removed, one at a time or - # all the ones set by MailerSubscriber module - context 'when removing the default email notification subscription' do - before do - Spree::MailerSubscriber.deactivate(:order_finalized) - end - - after do - Spree::MailerSubscriber.activate - end - - it 'does not send the email' do - expect(Spree::Config.order_mailer_class).not_to receive(:confirm_email) - - order.complete! - end - end - - context 'when removing all the email notification subscriptions' do - before do - Spree::MailerSubscriber.deactivate - end - - after do - Spree::MailerSubscriber.activate - end - - it 'does not send the email' do - expect(Spree::Config.order_mailer_class).not_to receive(:confirm_email) - - order.complete! - end - end - end end end diff --git a/core/spec/models/spree/order_updater_spec.rb b/core/spec/models/spree/order_updater_spec.rb index 88be0e2716e..5bf12615802 100644 --- a/core/spec/models/spree/order_updater_spec.rb +++ b/core/spec/models/spree/order_updater_spec.rb @@ -391,7 +391,7 @@ module Spree context "with 'order_recalculated' event subscription" do let(:item) { spy('object') } - let(:bus) { Spree::Config.use_legacy_events ? Spree::Event : Spree::Bus } + let(:bus) { Spree::Bus } let!(:subscription) do bus.subscribe :order_recalculated do