signum 0.9.1 → 0.10.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: 14ea139d54cddb7019a86b2e85650910a6c5c4c2b370734c44297740ce0240e4
4
- data.tar.gz: f41b1ad624844998f7cc949fe50690736495a1a0d0d8c0cfa345323e93b8bcfd
3
+ metadata.gz: 168e5eb648c14976253f8dd27f28fbb58db9d34314634429a345276fbcbb819e
4
+ data.tar.gz: 67d09a0cbf834adb9f0ac8d0c38bd615227b3a39d021e214241778c1ae3a0403
5
5
  SHA512:
6
- metadata.gz: 12df075d4b815a708eac3dd12469e8581c57e74907d790ffb9a4ae3a335fcefcf72d25ae144333a1e853ed24a682f629dd23e91c941d9947eebebd1182c2f1eb
7
- data.tar.gz: c2fd943c340af257bb7b25d4d2a28c7dd89e75b6d19e76bff067cf050c25ba2e22bc83ada890b3403073c5532faf4c1fe9be3dc2d3f97fe6f193683f773b8145
6
+ metadata.gz: 9505524f16669be863221503c544925d9c6675ac1b87b678858354b4e60c8d2347e7c06ea1c37b8500b1d30637ee361b246fed0f4947a192bcd91741039c2d05
7
+ data.tar.gz: 40ee6d6d62829e106edc4d38e6e238d388dce749a641444628c5cda90ec6cda6f37f991c445b15973044860331a222147d661733c115586d0ea68b7ead940478
data/.shipkit.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  ---
2
- previous_version: 0.9.0
2
+ previous_version: 0.9.1
data/Gemfile.lock CHANGED
@@ -13,7 +13,7 @@ GIT
13
13
  PATH
14
14
  remote: .
15
15
  specs:
16
- signum (0.9.0)
16
+ signum (0.9.1)
17
17
  importmap-rails
18
18
  pg
19
19
  rails (> 6.0)
@@ -288,15 +288,15 @@ GEM
288
288
  tzinfo (2.0.6)
289
289
  concurrent-ruby (~> 1.0)
290
290
  unicode-display_width (2.5.0)
291
- view_component (4.24.0)
291
+ view_component (4.25.0)
292
292
  actionview (>= 7.1.0)
293
293
  activesupport (>= 7.1.0)
294
294
  prism (>= 1.0)
295
295
  webrick (1.8.1)
296
- websocket-driver (0.8.2)
296
+ websocket-driver (0.9.1)
297
297
  base64
298
- websocket-extensions (>= 0.1.0)
299
- websocket-extensions (0.1.5)
298
+ websocket-extensions (>= 0.2.0)
299
+ websocket-extensions (0.3.0)
300
300
  zeitwerk (2.6.13)
301
301
 
302
302
  GEM
data/README.md CHANGED
@@ -4,7 +4,35 @@ Short description and motivation.
4
4
 
5
5
  ## Usage
6
6
 
7
- How to use my plugin.
7
+ Send a notification to:
8
+
9
+ ```ruby
10
+ Signum.info(user, text: "Your export is ready") # a user: theirs alone
11
+ Signum.info(account, text: "Maintenance tonight") # a group: the users whose pages have it among config.groups
12
+ Signum.info(:all, text: "New version") # every page
13
+ Signum.error(request.session.id, text: "Not found") # a key: the pages that pass it to the component
14
+ ```
15
+
16
+ Also `success`, `warning` and `signal`. Each goes to the Turbo stream of whom it is for, `[user, :signals]`,
17
+ `[account, :signals]` or `[:signals, key]`, and a page subscribes only to its own: its user's, every page's, its user's
18
+ groups' and the keys it passes:
19
+
20
+ ```erb
21
+ <%= render Signum::Notifications::Component.new %> <%# signed in %>
22
+ <%= render Signum::Notifications::Component.new([request.session.id]) %> <%# nobody signed in yet %>
23
+ ```
24
+
25
+ ```ruby
26
+ Signum.setup do |config|
27
+ config.current_user = -> { Current.user }
28
+ config.groups = -> { [Current.account].compact } # none without a user
29
+ end
30
+ ```
31
+
32
+ Only the current user's own notification is theirs to show or close (`Signum::SignalController`).
33
+
34
+ Before 0.10, every notification was broadcast to one stream, `:signals`, that every page subscribed to, so each
35
+ browser received every user's (and only didn't show them).
8
36
 
9
37
  ## Installation
10
38
 
@@ -1,8 +1,7 @@
1
- = helpers.turbo_stream_from :signals
1
+ / Only the page's own streams: its user's, every page's, its groups' and its keys' (Signum.streams_for)
2
+ - streams.each do |stream|
3
+ = helpers.turbo_stream_from(*stream)
2
4
 
3
5
  .pointer-events-none.fixed.inset-0.items-end.px-2.py-6.sm:items-start.sm:p-6.z-10 data-turbo-permanent="true" id="signum_notifications"
4
- / #notifications_all.flex.w-full.flex-col.items-center.space-y-2.sm:items-end
5
- div.flex.w-full.flex-col.items-center.space-y-2.sm:items-end[id="#{Signum.config.balloon_notifications_container_id.call}"]
6
- div.flex.w-full.flex-col.items-center.space-y-2.sm:items-end[id="notifications_all"]
7
- - keys&.each do |key|
8
- div.flex.w-full.flex-col.items-center.space-y-2.sm:items-end[id="notifications_#{key}"]
6
+ - containers.each do |id|
7
+ div.flex.w-full.flex-col.items-center.space-y-2.sm:items-end[id=id]
@@ -2,8 +2,23 @@ module Signum
2
2
  module Notifications
3
3
  class Component < ViewComponent::Base
4
4
  attr_reader :keys
5
+
6
+ # keys: those whose signals the page gets besides its user's, every page's and its groups' (Signum.streams_for),
7
+ # e.g. [request.session.id] where nobody is signed in
5
8
  def initialize(keys = nil)
6
- @keys = keys
9
+ @keys = Array(keys).compact_blank.map(&:to_s)
10
+ end
11
+
12
+ def user = Signum.config.current_user.call
13
+
14
+ def streams = Signum.streams_for(user, keys)
15
+
16
+ # A container per receiver, which its signals are prepended to
17
+ def containers
18
+ receivers = [Signum::ALL, *Signum.groups_for(user), *keys]
19
+ ids = receivers.map { |receiver| Signum.config.balloon_notifications_container_id.call(receiver) }
20
+ ids.unshift(Signum.config.balloon_notifications_container_id.call(user)) if user
21
+ ids.uniq
7
22
  end
8
23
  end
9
24
  end
@@ -3,13 +3,11 @@ require_dependency 'signum/application_controller'
3
3
  module Signum
4
4
  class SignalController < ApplicationController
5
5
  def show
6
- signal = Signum::Signal.find(signal_params[:id])
7
6
  signal.show! if signal.broadcasted?
8
7
  head :ok
9
8
  end
10
9
 
11
10
  def close
12
- signal = Signum::Signal.find(signal_params[:id])
13
11
  signal.close!
14
12
  head :ok
15
13
  end
@@ -22,6 +20,13 @@ module Signum
22
20
 
23
21
  private
24
22
 
23
+ # Only the current user's own, or one for a group or key
24
+ def signal
25
+ @signal ||= Signum::Signal.find(signal_params[:id]).tap do |signal|
26
+ raise ActiveRecord::RecordNotFound unless signal.for?(Signum.config.current_user.call)
27
+ end
28
+ end
29
+
25
30
  def signal_params
26
31
  params.require(:signal).permit(:id)
27
32
  end
@@ -1,4 +1,11 @@
1
1
  module Signum
2
+ # A notification, broadcast to the stream of whom it is for, never to everyone's (Signum::Notifications::Component
3
+ # subscribes a page to its streams):
4
+ #
5
+ # - a user's (signalable a user): [user, :signals], theirs alone
6
+ # - a group's (signalable another record, e.g. an account): [record, :signals], of the users whose page has it among
7
+ # Signum.config.groups
8
+ # - a key's: [:signals, key], of the pages that pass it (the component's keys), or every page's for "all"
2
9
  class Signal < ApplicationRecord
3
10
  belongs_to :signalable, polymorphic: true, optional: true
4
11
  belongs_to :subjectable, polymorphic: true, optional: true
@@ -10,13 +17,23 @@ module Signum
10
17
  after_create_commit :broadcast_create
11
18
  after_update_commit :broadcast_update
12
19
 
20
+ # The stream it is broadcast to
21
+ def stream = Signum.stream_for(key.presence || signalable)
22
+
23
+ # Whether it is the user's to show or close: their own, or one for a group or key, which isn't anyone's
24
+ def for?(user)
25
+ return signalable == user if signalable_type == Signum.config.user_model_name
26
+
27
+ true
28
+ end
29
+
13
30
  def broadcast_create
14
31
  broadcast! if can_broadcast?
15
32
 
16
- broadcast_prepend_to(:signals, target: Signum.config.balloon_notifications_container_id.call(key || signalable),
33
+ broadcast_prepend_to(*stream, target: Signum.config.balloon_notifications_container_id.call(key.presence || signalable),
17
34
  html: ActionController::Base.render(Signum::Notification::Component.new(self)))
18
35
 
19
- broadcast_prepend_to(:signals, target: Signum.config.drawer_notifications_container_id.call(key || signalable),
36
+ broadcast_prepend_to(*stream, target: Signum.config.drawer_notifications_container_id.call(key.presence || signalable),
20
37
  html: ActionController::Base.render(Signum::NotificationDrawerItem::Component.new(signal: self)))
21
38
  end
22
39
 
@@ -24,13 +41,13 @@ module Signum
24
41
  return if saved_change_to_state? && (broadcasted? || shown?)
25
42
 
26
43
  if saved_change_to_state? && closed?
27
- broadcast_remove_to(:signals, target: "notification_drawer_item_parent_#{signalable_id}_#{id}")
44
+ broadcast_remove_to(*stream, target: "notification_drawer_item_parent_#{signalable_id}_#{id}")
28
45
  return
29
46
  end
30
47
 
31
- broadcast_replace_to(:signals, target: Signum.config.notification_body_id.call(:balloon, self),
48
+ broadcast_replace_to(*stream, target: Signum.config.notification_body_id.call(:balloon, self),
32
49
  html: ActionController::Base.render(Signum::NotificationBody::Component.new(self, {type: :balloon, timeout: 5})))
33
- broadcast_replace_to(:signals, target: Signum.config.notification_body_id.call(:drawer_item, self),
50
+ broadcast_replace_to(*stream, target: Signum.config.notification_body_id.call(:drawer_item, self),
34
51
  html: ActionController::Base.render(Signum::NotificationBody::Component.new(self, {type: :drawer_item, timeout: 5})))
35
52
  end
36
53
 
@@ -33,6 +33,9 @@ module Signum
33
33
  option :hide_after, default: 3000
34
34
  option :base_controller, default: "::ApplicationController"
35
35
  option :current_user, default: lambda {}
36
+ # The records whose signals the current user gets besides their own, e.g. -> { [Current.account] }; a page without
37
+ # a user gets none of them
38
+ option :groups, default: -> { [] }
36
39
  option :drawer_notifications_container_id, default: ->(signalable = nil) {
37
40
  signalable.is_a?(String) ? "drawer_notifications_#{signalable}" : "drawer_notifications_#{(signalable || Signum.config.current_user.call)&.class&.name}_#{(signalable || Signum.config.current_user.call)&.id}"
38
41
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Signum
4
- VERSION = "0.9.1"
4
+ VERSION = "0.10.0"
5
5
  end
data/lib/signum.rb CHANGED
@@ -6,6 +6,9 @@ module Signum
6
6
  class Error < StandardError
7
7
  end
8
8
 
9
+ # The key of the signals for every page
10
+ ALL = "all"
11
+
9
12
  class << self
10
13
  def config
11
14
  @config ||= Configuration.new
@@ -20,13 +23,20 @@ module Signum
20
23
  # @i18n_store ||= Nuntius::I18nStore.new
21
24
  # end
22
25
 
23
- # Generic notice
26
+ # Generic notice, for:
27
+ #
28
+ # - a user: theirs alone
29
+ # - another record (an account, say): the users whose pages have it among config.groups
30
+ # - several of these (an array or a relation): each of them
31
+ # - a key (a string or symbol): the pages that pass it to Signum::Notifications::Component (a session's id, say),
32
+ # or every page for :all
24
33
  def signal(signalable_receiver, options)
25
34
  return unless signalable_receiver
26
35
 
27
36
  if signalable_receiver.is_a?(Signum.config.user_model_name.constantize)
28
37
  signalable_receiver.signals.create!(options)
29
-
38
+ elsif signalable_receiver.is_a?(ActiveRecord::Base)
39
+ Signal.create!(options.merge(signalable: signalable_receiver))
30
40
  elsif signalable_receiver.respond_to?(:each)
31
41
  signalable_receiver.each { |signalable| signal(signalable, options) }
32
42
  nil
@@ -35,6 +45,20 @@ module Signum
35
45
  end
36
46
  end
37
47
 
48
+ # The Turbo stream of a user, group (a record) or key, which its signals are broadcast to
49
+ def stream_for(receiver)
50
+ receiver.is_a?(ActiveRecord::Base) ? [receiver, :signals] : [:signals, receiver.to_s]
51
+ end
52
+
53
+ # The streams a page subscribes to: its user's, every page's (:all), its user's groups' (config.groups) and its
54
+ # keys'. Without a user, no groups: they're for the users of a group, not whoever visits its pages.
55
+ def streams_for(user, keys = [])
56
+ ([user, ALL] + groups_for(user) + Array(keys)).compact_blank.uniq.map { |receiver| stream_for(receiver) }
57
+ end
58
+
59
+ # The groups whose signals the user gets (config.groups), none without one
60
+ def groups_for(user) = user ? Array(config.groups.call).compact : []
61
+
38
62
  # Signal about something that happened
39
63
  def info(signalable, options)
40
64
  return unless signalable
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt