nuntius 1.4.1 → 1.4.3

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: be4761123ee4a2d8aaca7f70592b66c3f0f2c1a352596e10233f9e1f7fae1d82
4
- data.tar.gz: 4a725d5bfa7f330ac1a7b89955b7df2aef3a56ee1ee36a8ea1a85074f26a6d77
3
+ metadata.gz: 503ba69f7497033d55a16b1ec419a8c7b9083c31e55ac8c80bfc93bbb3581f7c
4
+ data.tar.gz: c690591944454ff27e64faa46d0fad4240cc5f2124f5ee2c21182e991c268b20
5
5
  SHA512:
6
- metadata.gz: 108cc9efe6c26d6cc223fe9c58f671bbfc95ac4992c1859feca5d9f4d718eab4811b60d045b04cec06595bfcdc77ecf18e391dce0de87a8cce6388c52cb98b94
7
- data.tar.gz: afffc5c56327a134603c24520727ee9d1b76b527a1e378de6c350d71f459e488e8238c8dda9bf4ea562ce1b12107aa4d6bda3349109bafbc92f23815cb5abc8a
6
+ metadata.gz: 33ebca7868482ad0f3682a719a160ce53c98ae232eb5416952cc676ad2c568a2eefa1c67073e87990ac99e0409315b9100c4be79b67434dd1dfeb2cda4fb2f94
7
+ data.tar.gz: 4b2aa8d8a6f238001c2f0610b3fd797d80b68860e1b1335453daf382988610d3f01a5760ec37ef5c287967ab0feb5140fe4772fc520dd67856dcbad3a9c5736f
@@ -3,8 +3,11 @@
3
3
  # Initializes the appropriate Messenger class and calls the event method
4
4
  module Nuntius
5
5
  class MessengerJob < ApplicationJob
6
+ after_perform :cleanup_nuntius_events
7
+
6
8
  def perform(obj, event, params = {})
7
9
  return unless obj
10
+
8
11
  ActiveRecord::Base.transaction do
9
12
  messenger = Nuntius::BaseMessenger.messenger_for_obj(obj).new(obj, event, params)
10
13
  return unless messenger.is_a?(Nuntius::CustomMessenger) || messenger.respond_to?(event.to_sym)
@@ -13,19 +16,15 @@ module Nuntius
13
16
  return if result == false
14
17
 
15
18
  templates = messenger.templates
16
- messenger.dispatch(templates) if templates.present?
19
+ messenger.dispatch(templates) if templates.exists?
17
20
  end
18
- cleanup_nuntius_events(obj, event)
19
21
  end
20
22
 
21
- def cleanup_nuntius_events(obj, event)
22
-
23
- nuntius_events = Nuntius::Event.where(
24
- transitionable_id: obj["id"],
25
- transitionable_type: obj["type"],
26
- transition_event: event.to_s
27
- )
28
- nuntius_events.destroy_all
23
+ def cleanup_nuntius_events
24
+ Nuntius::Event.where(
25
+ transitionable: arguments.first,
26
+ transition_event: arguments.second.to_s
27
+ ).delete_all
29
28
  end
30
29
  end
31
30
  end
@@ -4,26 +4,38 @@ module Nuntius
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
+ state_machine.events.map(&:name)
8
+ .reject { |event_name| messenger.method_defined?(event_name) }
9
+ .each do |event_name|
10
+ messenger.send(:define_method, event_name) { |object, options = {}| }
11
+ end
12
+
7
13
  state_machine do
8
- after_transition any => any do |record, transition|
9
- event = Nuntius::Event.find_or_initialize_by(
10
- transitionable_id: record.id,
11
- transitionable_type: record.class.to_s,
12
- transition_event: transition.event.to_s,
13
- transition_attribute: transition.attribute.to_s
14
- )
15
- event.update!(
16
- transition_from: transition.from.to_s,
17
- transition_to: transition.to.to_s
18
- )
14
+ after_transition any => any do |object, transition|
15
+ if object.persisted? && Nuntius.templates?(object, transition.event)
16
+ Nuntius::Event.find_or_create_by!(
17
+ transitionable_type: object.class.name,
18
+ transitionable_id: object.id,
19
+ # transitionable: object,
20
+ transition_event: transition.event.to_s,
21
+ transition_attribute: transition.attribute.to_s
22
+ ) do |event|
23
+ event.transition_from = transition.from.to_s
24
+ event.transition_to = transition.to.to_s
25
+ end
26
+ end
19
27
  end
20
28
  end
21
29
 
22
- after_commit do
23
- Nuntius::Event.all.each do |transition|
24
- transitionable = transition.transitionable_type.constantize.find(transition.transitionable_id)
25
- Nuntius.event(transition.transition_event.to_sym, transitionable)
26
- end
30
+ after_commit :dispatch_nuntius_events
31
+ end
32
+
33
+ def dispatch_nuntius_events
34
+ Nuntius::Event
35
+ .where(transitionable: self)
36
+ .includes(:transitionable)
37
+ .select(:transition_event, :transitionable_type, :transitionable_id).distinct.each do |event|
38
+ Nuntius.event(event.transition_event.to_sym, event.transitionable)
27
39
  end
28
40
  end
29
41
  end
@@ -3,4 +3,4 @@ module Nuntius
3
3
  belongs_to :transitionable, polymorphic: true
4
4
  validates :transition_event, :transition_attribute, :transition_from, :transition_to, presence: true
5
5
  end
6
- end
6
+ end
@@ -33,8 +33,10 @@ module Nuntius
33
33
 
34
34
  options = {registers: {"template" => self, "message" => message}}
35
35
 
36
- message.to = render(:to, assigns, locale, options).split(",").reject(&:empty?).join(",")
37
- message.from = render(:from, assigns, locale, options).split(",").reject(&:empty?).join(",")
36
+ message.to = render(:to, assigns, locale, options).split(',').map(&:strip).reject{ |to| to == 'Liquid error internal' }.reject(&:empty?).join(',')
37
+ return unless message.to.present?
38
+
39
+ message.from = render(:from, assigns, locale, options).split(',').reject(&:empty?).join(',')
38
40
  message.subject = render(:subject, assigns, locale, options)
39
41
  message.html = render(:html, assigns, locale, options.merge(layout: layout&.data))
40
42
  message.text = render(:text, assigns, locale, options)
@@ -0,0 +1,9 @@
1
+ class UpdateNuntiusEventsIndex < ActiveRecord::Migration[7.0]
2
+ def change
3
+ remove_index :nuntius_events, name: :index_nuntius_events_on_transitionable
4
+
5
+ add_index :nuntius_events,
6
+ [:transitionable_type, :transitionable_id, :transition_event],
7
+ name: :index_nuntius_events_on_type_id_event
8
+ end
9
+ end
@@ -2,7 +2,6 @@
2
2
 
3
3
  require_relative "nuntiable"
4
4
  require_relative "devise"
5
- require_relative "state_machine"
6
5
  require_relative "life_cycle"
7
6
  # require_relative "transactio"
8
7
 
@@ -17,8 +16,7 @@ module Nuntius::ActiveRecordHelpers
17
16
  def nuntiable(options = {})
18
17
  @_nuntius_nuntiable_options = options
19
18
  include Nuntius::Nuntiable
20
- include Nuntius::StateMachine if options[:use_state_machine]
21
- include Nuntius::Concerns::EventsTransaction if options[:use_state_machine]
19
+ include Nuntius::Concerns::EventsTransaction if options[:use_state_machine] || options[:state_machine]
22
20
  include Nuntius::Devise if options[:override_devise]
23
21
  include Nuntius::LifeCycle if options[:life_cycle]
24
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nuntius
4
- VERSION = "1.4.1"
4
+ VERSION = "1.4.3"
5
5
  end
data/lib/nuntius.rb CHANGED
@@ -50,10 +50,13 @@ module Nuntius
50
50
  end
51
51
 
52
52
  def templates?(obj, event)
53
- Nuntius::Template.where(klass: Nuntius::BaseMessenger.class_names_for(obj),
54
- event: Nuntius::BaseMessenger.event_name_for(
55
- obj, event
56
- )).where(enabled: true).count.positive?
53
+ messenger = Nuntius::BaseMessenger.messenger_for_obj(obj).new(obj, event)
54
+ return false unless messenger.is_a?(Nuntius::CustomMessenger) || messenger.respond_to?(event.to_sym)
55
+
56
+ result = messenger.call
57
+ return false if result == false
58
+
59
+ messenger.templates.exists?
57
60
  end
58
61
  end
59
62
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nuntius
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-20 00:00:00.000000000 Z
11
+ date: 2025-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: apnotic
@@ -572,6 +572,7 @@ files:
572
572
  - db/migrate/20240205204719_add_description_to_list.rb
573
573
  - db/migrate/20240206203019_add_slug_to_nuntius_list.rb
574
574
  - db/migrate/20250520091916_create_nuntius_events_table.rb
575
+ - db/migrate/20250521132554_update_nuntius_events_index.rb
575
576
  - lib/generators/nuntius/install_generator.rb
576
577
  - lib/generators/nuntius/tailwind_config_generator.rb
577
578
  - lib/generators/nuntius/templates/config/initializers/nuntius.rb
@@ -587,7 +588,6 @@ files:
587
588
  - lib/nuntius/liquid/tags/attach_tag.rb
588
589
  - lib/nuntius/mail_allow_list.rb
589
590
  - lib/nuntius/nuntiable.rb
590
- - lib/nuntius/state_machine.rb
591
591
  - lib/nuntius/version.rb
592
592
  - lib/preamble.rb
593
593
  - lib/tasks/nuntius_tasks.rake
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nuntius
4
- module StateMachine
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- raise "#{name} must be nuntiable" unless nuntiable?
9
-
10
- state_machine.events.map(&:name)
11
- .reject { |event_name| messenger.method_defined?(event_name) }
12
- .each do |event_name|
13
- messenger.send(:define_method, event_name) { |object, options = {}| }
14
- end
15
-
16
- after_commit do
17
- Thread.current["___nuntius_state_machine_events"]&.each do |event|
18
- Nuntius.event(event[:event], event[:object])
19
- end
20
- # After events are fired we can clear the events
21
- Thread.current["___nuntius_state_machine_events"] = []
22
- end
23
-
24
- state_machine do
25
- # This records events within the same thread, and clears them in the same thread.
26
- # A different thread is a different transaction.
27
- after_transition any => any do |record, transition|
28
- ___record__nuntius_state_machine_event(transition.event, record)
29
- ___record__nuntius_state_machine_event(:update, record)
30
- ___record__nuntius_state_machine_event(:save, record)
31
- end
32
-
33
- def ___record__nuntius_state_machine_event(event, object)
34
- Thread.current["___nuntius_state_machine_events"] ||= []
35
- Thread.current["___nuntius_state_machine_events"] << {event: event, object: object}
36
- end
37
- end
38
- end
39
- end
40
- end