notifi 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +123 -0
- data/Rakefile +1 -0
- data/lib/notifi/base.rb +16 -0
- data/lib/notifi/notification.rb +30 -0
- data/lib/notifi/subscribable.rb +19 -0
- data/lib/notifi/subscriber.rb +40 -0
- data/lib/notifi/subscription.rb +27 -0
- data/lib/notifi/version.rb +3 -0
- data/lib/notifi.rb +14 -0
- data/notifi.gemspec +27 -0
- data/spec/mongoid.yml +7 -0
- data/spec/notification_spec.rb +15 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/subscribable_spec.rb +115 -0
- data/spec/subscriber_spec.rb +96 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8345a4a8b58f6a503421a1e19d73bf07f4f78fc2
|
4
|
+
data.tar.gz: bc4d59ffcd2f022c8da86950e93255eceac95446
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b88901322aca4e2540c0cbb823f62ce541f4191301b75436d73f5984442603b6f1e428a2aa1e79dcb0d51104e724e9c937a0f07dd558721d1769cfd2e0bf5a0
|
7
|
+
data.tar.gz: fc026677730bb98cd890fd51dc89f207807f9840a8f31f804df0861fb99b6c4ddcfb72f6d86705f818c9ca0133f2cf01f944bfd14569cc01274e4dd09d9a08b0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Hunter Haydel
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
[](https://codeclimate.com/github/wedgex/notifi)
|
2
|
+
|
3
|
+
# Notifi
|
4
|
+
|
5
|
+
TODO: Write a gem description
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'notifi'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install notifi
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic subscription & notification
|
24
|
+
Setup subscribers and subscribable models:
|
25
|
+
~~~~~ Ruby
|
26
|
+
class User
|
27
|
+
include Mongoid::Document
|
28
|
+
|
29
|
+
# Include notifi base
|
30
|
+
include Notifi
|
31
|
+
|
32
|
+
# setup user as a subscriber
|
33
|
+
acts_as_subscriber
|
34
|
+
end
|
35
|
+
|
36
|
+
class Post
|
37
|
+
include Mongoid::Document
|
38
|
+
|
39
|
+
# Include notifi base
|
40
|
+
include Notifi
|
41
|
+
|
42
|
+
# setup posts as subscribable
|
43
|
+
acts_as_subscribable
|
44
|
+
end
|
45
|
+
~~~~
|
46
|
+
|
47
|
+
You now have the ability to subscribe users to posts and notify users through the subscriptions.
|
48
|
+
~~~~ Ruby
|
49
|
+
post = Post.create
|
50
|
+
user = User.create
|
51
|
+
|
52
|
+
user.subscribe_to post
|
53
|
+
|
54
|
+
user.notifications.count # => 0
|
55
|
+
|
56
|
+
post.notify
|
57
|
+
|
58
|
+
user.notifications.count # => 1
|
59
|
+
~~~~
|
60
|
+
|
61
|
+
### On notification events
|
62
|
+
|
63
|
+
~~~ Ruby
|
64
|
+
class User
|
65
|
+
include Mongoid::Document
|
66
|
+
include Notifi
|
67
|
+
|
68
|
+
acts_as_subscriber
|
69
|
+
|
70
|
+
on_notification do |notification|
|
71
|
+
# Do some sweet notification stuff here. Like maybe queue up an email
|
72
|
+
# or print a smiley face to the log.
|
73
|
+
end
|
74
|
+
end
|
75
|
+
~~~
|
76
|
+
|
77
|
+
The block provided to on_notification in your subscriber class will be called after a notification for the subscriber is created.
|
78
|
+
|
79
|
+
|
80
|
+
### Custom notification classes.
|
81
|
+
|
82
|
+
You can customize notifications by extending the Notification class and configuring the subscribable to use the custom class for notifications.
|
83
|
+
|
84
|
+
~~~ Ruby
|
85
|
+
class CommentNotification < Notifi::Notification
|
86
|
+
field :message, type: String
|
87
|
+
end
|
88
|
+
|
89
|
+
class Post
|
90
|
+
include Mongoid::Document
|
91
|
+
include Notifi
|
92
|
+
|
93
|
+
acts_as_subscribable default: CommentNotification
|
94
|
+
end
|
95
|
+
|
96
|
+
post = Post.create
|
97
|
+
user.subscribe_to post
|
98
|
+
post.notify(set:{message: 'Someone commented on your post!'})
|
99
|
+
|
100
|
+
user.notifications.first.message # => 'Someone commented on your post!'
|
101
|
+
~~~
|
102
|
+
|
103
|
+
### Notifying specific events
|
104
|
+
|
105
|
+
~~~ Ruby
|
106
|
+
|
107
|
+
# ...
|
108
|
+
|
109
|
+
acts_as_subscribable default: CommentNotification, test: TestNotification
|
110
|
+
|
111
|
+
# ...
|
112
|
+
|
113
|
+
thing.notifiy(:test)
|
114
|
+
~~~
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
1. Fork it
|
119
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
120
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
121
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
122
|
+
5. Create new Pull Request
|
123
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/notifi/base.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Notifi
|
2
|
+
module Base
|
3
|
+
def acts_as_subscriber
|
4
|
+
class_attribute :notification_event
|
5
|
+
|
6
|
+
include Subscriber
|
7
|
+
end
|
8
|
+
|
9
|
+
def acts_as_subscribable(subscribable_options={})
|
10
|
+
class_attribute :subscribable_options
|
11
|
+
self.subscribable_options = subscribable_options
|
12
|
+
|
13
|
+
include Subscribable
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Notifi
|
2
|
+
class Notification
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
|
6
|
+
belongs_to :subscription
|
7
|
+
belongs_to :subscriber, polymorphic: true
|
8
|
+
belongs_to :notifier, polymorphic: true
|
9
|
+
belongs_to :subscribable, polymorphic: true
|
10
|
+
|
11
|
+
field :read, type: Boolean, default: false
|
12
|
+
field :message, type: String
|
13
|
+
|
14
|
+
after_create do |n|
|
15
|
+
n.fire_notification_event
|
16
|
+
end
|
17
|
+
|
18
|
+
def notification_event?
|
19
|
+
self.subscriber && self.subscriber.notification_event
|
20
|
+
end
|
21
|
+
|
22
|
+
def fire_notification_event
|
23
|
+
self.subscriber.notification_event.call(self) if self.notification_event?
|
24
|
+
end
|
25
|
+
|
26
|
+
def mark_as_read
|
27
|
+
self.update_attribute :read, true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Notifi
|
2
|
+
module Subscribable
|
3
|
+
def self.included(base)
|
4
|
+
base.has_many :subscriptions, as: :subscribable,
|
5
|
+
class_name: Subscription.name,
|
6
|
+
dependent: :destroy,
|
7
|
+
inverse_of: :subscribable
|
8
|
+
|
9
|
+
base.has_many :notifications, as: :subscribable,
|
10
|
+
class_name: Notification.name,
|
11
|
+
dependent: :destroy,
|
12
|
+
inverse_of: :subscribable
|
13
|
+
end
|
14
|
+
|
15
|
+
def notify(event=:default, notifier: nil, set: {})
|
16
|
+
self.subscriptions.each { |s| s.notify(event, notifier, set: set) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Notifi
|
2
|
+
module Subscriber
|
3
|
+
def self.included(base)
|
4
|
+
base.has_many :subscriptions, class_name: Subscription.name, dependent: :destroy, inverse_of: :subscriber
|
5
|
+
base.has_many :triggered_notifications, class_name: Notification.name, dependent: :destroy, inverse_of: :notifier
|
6
|
+
base.has_many :notifications, class_name: Notification.name, dependent: :destroy, inverse_of: :subscriber do
|
7
|
+
def mark_as_read
|
8
|
+
self.each(&:mark_as_read)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
def subscribe_to(subscribable)
|
16
|
+
reject_non_subscribable! subscribable
|
17
|
+
|
18
|
+
self.subscriptions.find_or_create_by(subscriber: self, subscribable: subscribable)
|
19
|
+
end
|
20
|
+
|
21
|
+
def unsubscribe_from(subscribable)
|
22
|
+
reject_non_subscribable! subscribable
|
23
|
+
|
24
|
+
self.subscriptions.destroy_all(subscribable: subscribable)
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
def on_notification(&block)
|
29
|
+
self.notification_event = block
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def reject_non_subscribable!(target)
|
35
|
+
unless target.kind_of? Subscribable
|
36
|
+
raise ArgumentError, "#{target.class} does not include Notifi::Subscribable"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Notifi
|
2
|
+
class Subscription
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
|
6
|
+
belongs_to :subscriber, polymorphic: true
|
7
|
+
belongs_to :subscribable, polymorphic: true
|
8
|
+
has_many :notifications, dependent: :destroy, inverse_of: :subscription
|
9
|
+
|
10
|
+
def notify(event=:default, notifier=nil, set: {})
|
11
|
+
set[:subscription] = self
|
12
|
+
set[:notifier] = notifier
|
13
|
+
set[:subscriber] = self.subscriber
|
14
|
+
set[:subscribable] = self.subscribable
|
15
|
+
|
16
|
+
self.notification_class(event).create(set)
|
17
|
+
end
|
18
|
+
|
19
|
+
def subscribable_options
|
20
|
+
self.subscribable.subscribable_options
|
21
|
+
end
|
22
|
+
|
23
|
+
def notification_class(event)
|
24
|
+
subscribable_options[event] || subscribable_options[:default] || Notification
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/notifi.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
require 'notifi/version'
|
4
|
+
require 'notifi/base'
|
5
|
+
require 'notifi/subscription'
|
6
|
+
require 'notifi/notification'
|
7
|
+
require 'notifi/subscribable'
|
8
|
+
require 'notifi/subscriber'
|
9
|
+
|
10
|
+
module Notifi
|
11
|
+
def self.included(base)
|
12
|
+
base.extend Base
|
13
|
+
end
|
14
|
+
end
|
data/notifi.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'notifi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "notifi"
|
8
|
+
spec.version = Notifi::VERSION
|
9
|
+
spec.authors = ["Hunter Haydel"]
|
10
|
+
spec.email = ["haydh530@gmail.com"]
|
11
|
+
spec.description = "Simple frame for creating subsciption and notificaton records"
|
12
|
+
spec.summary = "Simple frame for creating subsciption and notificaton records"
|
13
|
+
spec.homepage = "http://www.github.com/wedgex/notifi"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "mongoid", "~> 3.1.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "database_cleaner"
|
27
|
+
end
|
data/spec/mongoid.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'notification' do
|
4
|
+
let(:notification) { Notifi::Notification.create }
|
5
|
+
|
6
|
+
it 'should default to unread' do
|
7
|
+
notification.read?.should be false
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be able to mark_as_read' do
|
11
|
+
notification.mark_as_read
|
12
|
+
|
13
|
+
notification.read?.should be true
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'notifi'
|
2
|
+
require 'database_cleaner'
|
3
|
+
|
4
|
+
ENV["MONGOID_ENV"] = 'test'
|
5
|
+
Mongoid.load!(File.dirname(__FILE__) + '/mongoid.yml')
|
6
|
+
|
7
|
+
class User
|
8
|
+
include Mongoid::Document
|
9
|
+
include Notifi
|
10
|
+
|
11
|
+
acts_as_subscriber
|
12
|
+
end
|
13
|
+
|
14
|
+
class Post
|
15
|
+
include Mongoid::Document
|
16
|
+
include Notifi
|
17
|
+
|
18
|
+
acts_as_subscribable
|
19
|
+
end
|
20
|
+
|
21
|
+
class CommentNotification < Notifi::Notification
|
22
|
+
field :comment, type: String
|
23
|
+
end
|
24
|
+
|
25
|
+
class Comment
|
26
|
+
include Mongoid::Document
|
27
|
+
include Notifi
|
28
|
+
|
29
|
+
acts_as_subscribable default: CommentNotification
|
30
|
+
end
|
31
|
+
|
32
|
+
RSpec.configure do |config|
|
33
|
+
config.before(:suite) do
|
34
|
+
DatabaseCleaner.strategy = :truncation
|
35
|
+
DatabaseCleaner.clean_with(:truncation)
|
36
|
+
end
|
37
|
+
|
38
|
+
config.before(:each) do
|
39
|
+
DatabaseCleaner.start
|
40
|
+
end
|
41
|
+
|
42
|
+
config.after(:each) do
|
43
|
+
DatabaseCleaner.clean
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'subscribable' do
|
4
|
+
let(:subscribable) { Post.create }
|
5
|
+
|
6
|
+
it 'should be able to notify all subscribers' do
|
7
|
+
subscribers = [User.create, User.create]
|
8
|
+
|
9
|
+
subscribers.each { |s| s.subscribe_to subscribable }
|
10
|
+
|
11
|
+
subscribable.notify
|
12
|
+
|
13
|
+
subscribers.each do |s|
|
14
|
+
s.notifications.length.should eq 1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be able to provide a notifier' do
|
19
|
+
user = User.create
|
20
|
+
comment = Comment.create
|
21
|
+
|
22
|
+
user.subscribe_to comment
|
23
|
+
|
24
|
+
comment.notify(notifier: user)
|
25
|
+
|
26
|
+
user.notifications.first.notifier.should eq user
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'custom notifications' do
|
30
|
+
it 'should be able to define notification type' do
|
31
|
+
user = User.create
|
32
|
+
comment = Comment.create
|
33
|
+
|
34
|
+
user.subscribe_to comment
|
35
|
+
|
36
|
+
comment.notify
|
37
|
+
|
38
|
+
user.notifications.first.should be_a CommentNotification
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should be able to set fields on notification' do
|
42
|
+
user = User.create
|
43
|
+
comment = Comment.create
|
44
|
+
|
45
|
+
user.subscribe_to comment
|
46
|
+
|
47
|
+
message = 'HELLO'
|
48
|
+
|
49
|
+
comment.notify(set: {comment: message})
|
50
|
+
|
51
|
+
user.notifications.first.comment.should eq message
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should be able to define notifications for different events' do
|
55
|
+
user = User.create
|
56
|
+
class Thing
|
57
|
+
include Mongoid::Document
|
58
|
+
include Notifi
|
59
|
+
|
60
|
+
acts_as_subscribable default: Notifi::Notification, test: CommentNotification
|
61
|
+
end
|
62
|
+
thing = Thing.create
|
63
|
+
user.subscribe_to thing
|
64
|
+
thing.notify :test
|
65
|
+
user.notifications.first.should be_a CommentNotification
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should fall back to default notification type if event not provided' do
|
69
|
+
user = User.create
|
70
|
+
class Thing
|
71
|
+
include Mongoid::Document
|
72
|
+
include Notifi
|
73
|
+
|
74
|
+
acts_as_subscribable default: CommentNotification
|
75
|
+
end
|
76
|
+
thing = Thing.create
|
77
|
+
user.subscribe_to thing
|
78
|
+
thing.notify
|
79
|
+
user.notifications.first.should be_a CommentNotification
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should fall back to default notification type if event not found' do
|
83
|
+
user = User.create
|
84
|
+
class Thing
|
85
|
+
include Mongoid::Document
|
86
|
+
include Notifi
|
87
|
+
|
88
|
+
acts_as_subscribable default: CommentNotification
|
89
|
+
end
|
90
|
+
thing = Thing.create
|
91
|
+
user.subscribe_to thing
|
92
|
+
thing.notify :test
|
93
|
+
user.notifications.first.should be_a CommentNotification
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should be able to provide a namespaced notification' do
|
98
|
+
module Notification
|
99
|
+
class Comment < Notifi::Notification; end
|
100
|
+
end
|
101
|
+
|
102
|
+
class Thing
|
103
|
+
include Mongoid::Document
|
104
|
+
include Notifi
|
105
|
+
|
106
|
+
acts_as_subscribable default: Notification::Comment
|
107
|
+
end
|
108
|
+
|
109
|
+
user = User.create
|
110
|
+
thing = Thing.create
|
111
|
+
user.subscribe_to thing
|
112
|
+
thing.notify
|
113
|
+
user.notifications.first.should be_a Notification::Comment
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'subscriber' do
|
4
|
+
let(:subscriber) { User.create }
|
5
|
+
let(:subscribable) { Post.create }
|
6
|
+
|
7
|
+
it 'should have subscriptions' do
|
8
|
+
# TODO figure out why shoulda matchers are blowing up and do this right
|
9
|
+
subscriber.should respond_to(:subscriptions)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should have notifications' do
|
13
|
+
subscriber.should respond_to(:notifications)
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'subscribe_to' do
|
17
|
+
it 'should create a subscription' do
|
18
|
+
subscriber.subscribe_to(subscribable)
|
19
|
+
|
20
|
+
subscriber.subscriptions.count.should eq 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should throw exception if not given a subscribeble' do
|
24
|
+
-> { subscriber.unsubscribe_from(Object.new) }.should raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should not create duplicate subscriptions' do
|
28
|
+
subscriber.subscribe_to subscribable
|
29
|
+
subscriber.subscribe_to subscribable
|
30
|
+
|
31
|
+
subscriber.subscriptions.count.should eq 1
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should return a Subscription' do
|
35
|
+
subscription = subscriber.subscribe_to subscribable
|
36
|
+
|
37
|
+
subscription.should be_an_instance_of Notifi::Subscription
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
context 'unsubscribe_from' do
|
44
|
+
it 'should be able to unsubscribe' do
|
45
|
+
subscriber.subscribe_to subscribable
|
46
|
+
|
47
|
+
subscriber.unsubscribe_from subscribable
|
48
|
+
|
49
|
+
subscriber.subscriptions.inspect
|
50
|
+
|
51
|
+
subscriber.subscriptions.count.should eq 0
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should throw exception if not given a subscribeble' do
|
55
|
+
-> { subscriber.unsubscribe_from(Object.new) }.should raise_error(ArgumentError)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'on_notification' do
|
60
|
+
it 'should be called when notification is created' do
|
61
|
+
class Thing
|
62
|
+
include Mongoid::Document
|
63
|
+
include Notifi
|
64
|
+
|
65
|
+
acts_as_subscriber
|
66
|
+
|
67
|
+
attr_accessor :notified
|
68
|
+
|
69
|
+
@block_called = false
|
70
|
+
on_notification do |notification|
|
71
|
+
@block_called = true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
thing = Thing.create
|
76
|
+
|
77
|
+
thing.subscribe_to subscribable
|
78
|
+
thing.subscriptions.first.notify
|
79
|
+
|
80
|
+
thing.class.instance_variable_get(:@block_called).should be true
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'mark_as_read' do
|
85
|
+
it 'should set read to true on all notifications' do
|
86
|
+
subscription = subscriber.subscribe_to subscribable
|
87
|
+
|
88
|
+
subscription.notify
|
89
|
+
subscription.notify
|
90
|
+
|
91
|
+
subscriber.notifications.mark_as_read
|
92
|
+
|
93
|
+
subscriber.notifications.each { |n| n.read?.should be true }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: notifi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hunter Haydel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Simple frame for creating subsciption and notificaton records
|
84
|
+
email:
|
85
|
+
- haydh530@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/notifi.rb
|
96
|
+
- lib/notifi/base.rb
|
97
|
+
- lib/notifi/notification.rb
|
98
|
+
- lib/notifi/subscribable.rb
|
99
|
+
- lib/notifi/subscriber.rb
|
100
|
+
- lib/notifi/subscription.rb
|
101
|
+
- lib/notifi/version.rb
|
102
|
+
- notifi.gemspec
|
103
|
+
- spec/mongoid.yml
|
104
|
+
- spec/notification_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/subscribable_spec.rb
|
107
|
+
- spec/subscriber_spec.rb
|
108
|
+
homepage: http://www.github.com/wedgex/notifi
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.0.2
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Simple frame for creating subsciption and notificaton records
|
132
|
+
test_files:
|
133
|
+
- spec/mongoid.yml
|
134
|
+
- spec/notification_spec.rb
|
135
|
+
- spec/spec_helper.rb
|
136
|
+
- spec/subscribable_spec.rb
|
137
|
+
- spec/subscriber_spec.rb
|