communique 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 83a7e5be37e23918ac8477417e6bb769d3e0d524
4
- data.tar.gz: 9e754661e47632b80ab8e1076941141ad0b37e8a
3
+ metadata.gz: 774bcaa4e9e2bec498fa64ed09171d390e84aa19
4
+ data.tar.gz: 300cb9f363f5a590e50b4252c62dc7e14d6159d1
5
5
  SHA512:
6
- metadata.gz: 627f1c1c594e0484808169074b956f1cd80fc26856192087db062d891696d97223c8d270998405d3aa123b855f136a3f45a8792b494fb0c9602a4bcecfc9e7d7
7
- data.tar.gz: 16278c48625816efe4083323d06e782b1392a4834aac19589502ceafc6cc59aaa0c0e830ed96aaaabd233993cf57686afb027da51dfa4c19ab71df5a6352410a
6
+ metadata.gz: 5b88e73ec989d41c2e7afd8cf388c1384a53b151027a13d57e9faac9c16d7ceab96b04391ae93ac3ae160f1620c6d570190230471626666715f702bdfda55615
7
+ data.tar.gz: b6dc54ce9f0ccee9012b25ede6bc86ceb35e989d78d03d8ee262dd36725027e703ca98839cee64aed3ecef6c5b5306b254b96fe6e9bb4629cf85278d97b39119
@@ -13,6 +13,7 @@ module Communique
13
13
  class Configuration
14
14
  attr_accessor :user_klass
15
15
  attr_accessor :notification_handler
16
+ attr_accessor :prevent_unseen_duplicates
16
17
 
17
18
  def set_notification_handler(&block)
18
19
  @notification_handler = block
@@ -1,4 +1,4 @@
1
1
  # gem version
2
2
  module Communique
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
data/lib/communique.rb CHANGED
@@ -10,13 +10,14 @@ module Communique
10
10
  def self.notify(notifiable, action_key, context_info = nil)
11
11
  action = Action.find_or_create_by(key: action_key)
12
12
 
13
- notification = Notification.create(
14
- notifiable: notifiable,
15
- context_info: context_info,
16
- action_key: action_key,
17
- action: action
18
- )
19
-
13
+ if prevent_unseen_duplicates?
14
+ existing_notification = find_existing_notifications(notifiable, action_key)
15
+ if existing_notification
16
+ existing_notification.update_attributes(context_info: context_info)
17
+ return existing_notification.id.to_s
18
+ end
19
+ end
20
+ notification = create_notification(notifiable, context_info, action_key, action)
20
21
  Handler.external_services notification
21
22
 
22
23
  notification.id.to_s
@@ -37,4 +38,31 @@ module Communique
37
38
  def self.count_unseen(notifiable)
38
39
  Notification.count_unseen notifiable
39
40
  end
41
+
42
+ def self.prevent_unseen_duplicates?
43
+ !Communique.config.nil? && Communique.config.prevent_unseen_duplicates
44
+ end
45
+
46
+ def self.find_existing_notifications(notifiable, action_key)
47
+ Notification.where(
48
+ notifiable: notifiable,
49
+ action_key: action_key,
50
+ seen: false
51
+ ).order_by(updated_at: :desc).first
52
+ end
53
+
54
+ def self.create_notification( notifiable, context_info, action_key, action)
55
+ Notification.create(
56
+ notifiable: notifiable,
57
+ context_info: context_info,
58
+ action_key: action_key,
59
+ action: action
60
+ )
61
+ end
62
+
63
+ private_class_method(
64
+ :prevent_unseen_duplicates?,
65
+ :find_existing_notifications,
66
+ :create_notification
67
+ )
40
68
  end
@@ -21,4 +21,37 @@ describe Communique do
21
21
  )
22
22
  end
23
23
  end
24
+ describe 'config.prevent_unseen_duplicates' do
25
+ it 'creates notifications when prevent_unseen_duplicates if false' do
26
+ Communique.configure { |config| config.prevent_unseen_duplicates = false }
27
+
28
+ dummy = NotifiableDummy.create
29
+ 2.times do
30
+ Communique.notify(
31
+ dummy,
32
+ '3CPO_fell_down_and_hit_his_head',
33
+ name: 'broken robot',
34
+ location: 'nginx caching issue'
35
+ )
36
+ end
37
+ expect(dummy.notifications.count).to be 2
38
+
39
+ end
40
+
41
+ it 'updates notifications when prevent_unseen_duplicates if true' do
42
+ Communique.configure { |config| config.prevent_unseen_duplicates = true }
43
+
44
+ dummy = NotifiableDummy.create
45
+ 2.times do
46
+ Communique.notify(
47
+ dummy,
48
+ '3CPO_fell_down_and_hit_his_head',
49
+ name: 'broken robot',
50
+ location: 'nginx caching issue'
51
+ )
52
+ end
53
+ expect(dummy.notifications.count).to be 1
54
+ end
55
+
56
+ end
24
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: communique
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Polycarpou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -172,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  version: '0'
173
173
  requirements: []
174
174
  rubyforge_project:
175
- rubygems_version: 2.4.3
175
+ rubygems_version: 2.4.5.1
176
176
  signing_key:
177
177
  specification_version: 4
178
178
  summary: Simple way to store warnings and errors affecting users as messages. Stored