eventifier 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +2 -1
  2. data/.travis.yml +14 -0
  3. data/Gemfile.lock +17 -3
  4. data/README.textile +7 -2
  5. data/Rakefile +1 -8
  6. data/eventifier.gemspec +7 -3
  7. data/lib/eventifier/active_record/event.rb +9 -0
  8. data/lib/eventifier/active_record/event_observer.rb +8 -0
  9. data/lib/eventifier/active_record/event_tracking.rb +11 -0
  10. data/lib/eventifier/active_record/ghost.rb +9 -0
  11. data/lib/eventifier/active_record/notification.rb +13 -0
  12. data/lib/eventifier/active_record_support.rb +5 -0
  13. data/lib/eventifier/event_mixin.rb +45 -0
  14. data/lib/eventifier/event_observer_mixin.rb +44 -0
  15. data/lib/eventifier/event_tracking.rb +29 -18
  16. data/lib/eventifier/ghost_mixin.rb +27 -0
  17. data/lib/eventifier/helper_methods.rb +2 -0
  18. data/lib/eventifier/mongoid/event.rb +16 -0
  19. data/lib/eventifier/mongoid/event_observer.rb +7 -0
  20. data/lib/eventifier/mongoid/event_tracking.rb +13 -0
  21. data/lib/eventifier/mongoid/ghost.rb +15 -0
  22. data/lib/eventifier/mongoid/notification.rb +19 -0
  23. data/lib/eventifier/mongoid/user_patch.rb +3 -0
  24. data/lib/eventifier/mongoid_support.rb +6 -0
  25. data/lib/eventifier/notification_helper.rb +4 -13
  26. data/lib/eventifier/notification_mailer.rb +5 -1
  27. data/lib/eventifier/notification_mixin.rb +42 -0
  28. data/lib/eventifier/version.rb +1 -1
  29. data/lib/eventifier.rb +8 -10
  30. data/lib/generators/eventifier/install/install_generator.rb +1 -1
  31. data/spec/event_helper_spec.rb +45 -35
  32. data/spec/event_observer_spec.rb +1 -6
  33. data/spec/event_spec.rb +16 -5
  34. data/spec/event_tracking_spec.rb +20 -0
  35. data/spec/eventifier_spec.rb +48 -52
  36. data/spec/fabricators/fabricator.rb +31 -0
  37. data/spec/ghost_spec.rb +30 -0
  38. data/spec/helper_methods_spec.rb +2 -2
  39. data/spec/integration/eventifier_spec.rb +85 -44
  40. data/spec/notification_helper_spec.rb +13 -6
  41. data/spec/notification_mailer_spec.rb +6 -1
  42. data/spec/notification_spec.rb +9 -11
  43. data/spec/spec_helper.rb +11 -2
  44. data/spec/support/database_cleaner.rb +3 -0
  45. data/spec/support/model_helpers.rb +36 -0
  46. data/spec/{support → test_classes}/active_record_support.rb +9 -0
  47. data/spec/test_classes/mongoid_support.rb +34 -0
  48. metadata +94 -24
  49. data/lib/eventifier/event.rb +0 -35
  50. data/lib/eventifier/event_observer.rb +0 -35
  51. data/lib/eventifier/notification.rb +0 -40
  52. data/spec/support/blueprints.rb +0 -24
@@ -16,27 +16,34 @@ describe Eventifier::NotificationHelper do
16
16
  :destroy => "{{user.name}} just deleted an Post",
17
17
  :update => {
18
18
  :single => "{{user.name}} made a change to their Post",
19
- :multiple => "{{user.name}} made some changes to their Post"
19
+ :multiple => "{{user.name}} made some changes to their Post",
20
+ :attributes => {
21
+ :deleted_at => "{{user.name}} deleted their Post"
22
+ }
20
23
  }
21
24
  }
22
25
  }
23
26
  I18n.backend.store_translations :en, :notifications => @notification_strings
24
27
  end
25
28
 
26
- describe ".notification_message" do
29
+ describe "#notification_message" do
27
30
  it "should return the I18n message for that event" do
28
31
 
29
- event = Eventifier::Event.make! :eventable => Post.make!, :verb => :create
32
+ event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :create)
30
33
  helper.notification_message(event).should == "<strong>#{event.user.name}</strong> just created an Post - you should check it out"
31
34
  end
32
35
 
33
36
  it "should return a message specific to a single change if only 1 change has been made" do
34
- event = Eventifier::Event.make! :eventable => Post.make!, :verb => :update, :change_data => { :name => ["Fred", "Mike"] }
37
+ event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :update, :change_data => { :name => ["Fred", "Mike"] })
35
38
  helper.notification_message(event).should == "<strong>#{event.user.name}</strong> made a change to their Post"
36
39
  end
40
+ it "should return a message specific to a particular field change if configuration is present" do
41
+ event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :update, :change_data => { :deleted_at => [nil, Time.now] })
42
+ helper.notification_message(event).should == "<strong>#{event.user.name}</strong> deleted their Post"
43
+ end
37
44
 
38
45
  it "should return a message specific to multiple changes if more than 1 change has been made" do
39
- event = Eventifier::Event.make! :eventable => Post.make!, :verb => :update, :change_data => { :name => ["Fred", "Mike"], :age => [55, 65] }
46
+ event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :update, :change_data => { :name => ["Fred", "Mike"], :age => [55, 65] })
40
47
  helper.notification_message(event).should == "<strong>#{event.user.name}</strong> made some changes to their Post"
41
48
  end
42
49
 
@@ -50,7 +57,7 @@ describe Eventifier::NotificationHelper do
50
57
  }
51
58
  I18n.backend.store_translations :test, :notifications => @notification_strings
52
59
  I18n.with_locale("test") do
53
- event = Eventifier::Event.make! :eventable => Post.make!, :verb => :create
60
+ event = Fabricate(:event, :eventable => Fabricate(:post), :verb => :create)
54
61
  helper.notification_message(event).should == "<strong>#{event.user.name}</strong> created a <strong>Post</strong>"
55
62
  end
56
63
  end
@@ -2,11 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe Eventifier::NotificationMailer do
4
4
  describe "#notification_email" do
5
+ before do
6
+ Eventifier::NotificationMailer.any_instance.stub main_app: double('app', url_for: true)
7
+ end
8
+
5
9
  it "should response to notification emails" do
6
10
  Eventifier::NotificationMailer.should respond_to(:notification_email)
7
11
  end
12
+
8
13
  it "should be able to send a 'notification' email" do
9
- proc { Eventifier::NotificationMailer.notification_email(Eventifier::Notification.make!).deliver }.should change(ActionMailer::Base.deliveries, :count)
14
+ proc { Eventifier::NotificationMailer.notification_email(Fabricate(:notification)).deliver }.should change(ActionMailer::Base.deliveries, :count)
10
15
  end
11
16
  end
12
17
  end
@@ -1,13 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Eventifier::Notification do
4
- let(:notification) { Eventifier::Notification.make! }
4
+ let(:notification) { Fabricate(:notification) }
5
+
6
+ before do
7
+ Eventifier::NotificationMailer.any_instance.stub main_app: double('app', url_for: true)
8
+ end
5
9
 
6
10
  describe '.expire_for_past_events!' do
7
11
  let(:notification) { double('notification', :expire! => true) }
8
12
  let(:expired_event_ids) { [1, 3, 5, 7] }
9
13
 
10
- before :each do
14
+ before do
11
15
  Eventifier::Event.stub :expired_ids => expired_event_ids
12
16
  Eventifier::Notification.stub :for_events => [notification]
13
17
  end
@@ -29,7 +33,6 @@ describe Eventifier::Notification do
29
33
  describe ".unread_for" do
30
34
  it "should return unread notifications for a user" do
31
35
  user = notification.user
32
-
33
36
  Eventifier::Notification.unread_for(user).should include notification
34
37
  end
35
38
 
@@ -37,7 +40,7 @@ describe Eventifier::Notification do
37
40
  user = notification.user
38
41
 
39
42
  user.notifications_last_read_at = Time.now
40
- second_notification = Eventifier::Notification.make!
43
+ second_notification = Fabricate(:notification)
41
44
 
42
45
  Eventifier::Notification.unread_for(user).should_not include notification
43
46
  Eventifier::Notification.unread_for(user).should_not include second_notification
@@ -47,19 +50,14 @@ describe Eventifier::Notification do
47
50
  describe "#create" do
48
51
  it "sends an email to the user" do
49
52
  ActionMailer::Base.deliveries.clear
50
- notification = Eventifier::Notification.make!
53
+ notification = Fabricate(:notification)
51
54
  ActionMailer::Base.deliveries.count.should > 0
52
55
  end
53
56
  end
54
57
 
55
- # describe "#valid?" do
56
- # it_requires_a :user
57
- # it_requires_an :event
58
- # end
59
-
60
58
  describe "#unread_for?" do
61
59
  let(:user) { double(User, :notifications_last_read_at => last_read) }
62
- subject { Eventifier::Notification.make(:created_at => Time.now).unread_for?(user) }
60
+ subject { Fabricate.build(:notification, :created_at => Time.now).unread_for?(user) }
63
61
  context 'when the user has never read notifications' do
64
62
  let(:last_read) { nil }
65
63
  it 'should return true' do
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,17 @@
1
1
  $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
2
  $LOAD_PATH.unshift File.dirname(__FILE__)
3
3
 
4
- Dir["./spec/support/**/*.rb"].each {|f| require f}
4
+ require 'fabrication'
5
+ orm = ENV['ORM'] || 'active_record'
6
+ require "./spec/test_classes/#{orm}_support.rb"
7
+
8
+ Fabrication.configure do |config|
9
+ config.fabricator_dir = ["spec/fabricators"]
10
+ end
11
+
12
+
13
+ Dir["./spec/support/**/*.rb"].each { |f| require f }
5
14
 
6
15
  require 'rubygems'
7
16
  require 'rspec'
8
- require 'eventifier'
17
+ require 'eventifier'
@@ -0,0 +1,3 @@
1
+ require 'database_cleaner'
2
+ DatabaseCleaner.strategy = :truncation
3
+ DatabaseCleaner.clean
@@ -0,0 +1,36 @@
1
+ module ModelHelpers
2
+ def self.included(base)
3
+ base.instance_eval do
4
+ extend ModelHelpers::ClassMethods
5
+ end
6
+ end
7
+
8
+ module ClassMethods
9
+ def it_requires_a(attribute)
10
+ it "requires a #{attribute}" do
11
+ instance = Fabricate.build(self.class.describes.name.demodulize.downcase.to_sym, attribute => nil)
12
+ instance.errors[attribute].should_not be_nil
13
+ end
14
+ end
15
+
16
+ def it_requires_an(attribute)
17
+ it "requires an #{attribute}" do
18
+ instance = Fabricate.build(self.class.describes.name.demodulize.downcase.to_sym, attribute => nil)
19
+ instance.errors[attribute].should_not be_nil
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ RSpec.configure do |config|
26
+ config.include ModelHelpers
27
+ end
28
+
29
+ RSpec::Matchers.define(:require_a) do |attribute|
30
+ description { "require the presence of a #{attribute.to_s}" }
31
+ match { |instance|
32
+ instance.send("#{attribute}=", nil)
33
+ instance.save
34
+ !instance.errors[attribute.to_sym].empty?
35
+ }
36
+ end
@@ -17,10 +17,19 @@ ActiveRecord::Schema.define(:version => 0) do
17
17
  t.timestamps
18
18
  end
19
19
 
20
+ create_table :ghosts, :force => true do |t|
21
+ t.string :ghost_class
22
+ t.integer :ghost_id
23
+ t.text :data_hash
24
+
25
+ t.timestamps
26
+ end
27
+
20
28
  create_table :notifications, :force => true do |t|
21
29
  t.integer :event_id
22
30
  t.integer :user_id
23
31
  t.integer :parent_id
32
+ t.string :url
24
33
 
25
34
  t.timestamps
26
35
  end
@@ -0,0 +1,34 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new("localhost", 27017).db('eventifier')
5
+ end
6
+
7
+ class User
8
+ include Mongoid::Document
9
+
10
+ field :name, :type => String
11
+
12
+ has_many :subscriptions
13
+ validates :name, :presence => true, :uniqueness => true
14
+ end
15
+
16
+ class Subscription
17
+ include Mongoid::Document
18
+
19
+ belongs_to :user
20
+ belongs_to :post
21
+ end
22
+
23
+ class Post
24
+ include Mongoid::Document
25
+
26
+ belongs_to :author, :class_name => "User"
27
+ has_many :subscriptions
28
+
29
+ def readers
30
+ subscriptions.map &:user
31
+ end
32
+
33
+ alias_method :user, :author
34
+ end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nathan Sampimon
9
+ - Peter Murray
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-06-09 00:00:00.000000000 Z
13
+ date: 2012-08-16 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: activerecord
16
- requirement: &70263342002960 !ruby/object:Gem::Requirement
17
+ requirement: &70102747627500 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,10 +22,21 @@ dependencies:
21
22
  version: '0'
22
23
  type: :development
23
24
  prerelease: false
24
- version_requirements: *70263342002960
25
+ version_requirements: *70102747627500
26
+ - !ruby/object:Gem::Dependency
27
+ name: mongoid
28
+ requirement: &70102747627040 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70102747627040
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: actionmailer
27
- requirement: &70263342002460 !ruby/object:Gem::Requirement
39
+ requirement: &70102747626540 !ruby/object:Gem::Requirement
28
40
  none: false
29
41
  requirements:
30
42
  - - ! '>='
@@ -32,10 +44,21 @@ dependencies:
32
44
  version: '0'
33
45
  type: :runtime
34
46
  prerelease: false
35
- version_requirements: *70263342002460
47
+ version_requirements: *70102747626540
48
+ - !ruby/object:Gem::Dependency
49
+ name: fabrication
50
+ requirement: &70102747626060 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70102747626060
36
59
  - !ruby/object:Gem::Dependency
37
- name: machinist
38
- requirement: &70263342002040 !ruby/object:Gem::Requirement
60
+ name: database_cleaner
61
+ requirement: &70102747625400 !ruby/object:Gem::Requirement
39
62
  none: false
40
63
  requirements:
41
64
  - - ! '>='
@@ -43,10 +66,10 @@ dependencies:
43
66
  version: '0'
44
67
  type: :development
45
68
  prerelease: false
46
- version_requirements: *70263342002040
69
+ version_requirements: *70102747625400
47
70
  - !ruby/object:Gem::Dependency
48
71
  name: pg
49
- requirement: &70263342001480 !ruby/object:Gem::Requirement
72
+ requirement: &70102747624340 !ruby/object:Gem::Requirement
50
73
  none: false
51
74
  requirements:
52
75
  - - ! '>='
@@ -54,10 +77,10 @@ dependencies:
54
77
  version: '0'
55
78
  type: :development
56
79
  prerelease: false
57
- version_requirements: *70263342001480
80
+ version_requirements: *70102747624340
58
81
  - !ruby/object:Gem::Dependency
59
82
  name: rspec
60
- requirement: &70263342000840 !ruby/object:Gem::Requirement
83
+ requirement: &70102747645620 !ruby/object:Gem::Requirement
61
84
  none: false
62
85
  requirements:
63
86
  - - ! '>='
@@ -65,10 +88,32 @@ dependencies:
65
88
  version: '0'
66
89
  type: :development
67
90
  prerelease: false
68
- version_requirements: *70263342000840
91
+ version_requirements: *70102747645620
69
92
  - !ruby/object:Gem::Dependency
70
93
  name: activerecord
71
- requirement: &70263342000400 !ruby/object:Gem::Requirement
94
+ requirement: &70102747645200 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ type: :runtime
101
+ prerelease: false
102
+ version_requirements: *70102747645200
103
+ - !ruby/object:Gem::Dependency
104
+ name: bson_ext
105
+ requirement: &70102747644640 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :runtime
112
+ prerelease: false
113
+ version_requirements: *70102747644640
114
+ - !ruby/object:Gem::Dependency
115
+ name: mongoid
116
+ requirement: &70102747644020 !ruby/object:Gem::Requirement
72
117
  none: false
73
118
  requirements:
74
119
  - - ! '>='
@@ -76,10 +121,10 @@ dependencies:
76
121
  version: '0'
77
122
  type: :runtime
78
123
  prerelease: false
79
- version_requirements: *70263342000400
124
+ version_requirements: *70102747644020
80
125
  - !ruby/object:Gem::Dependency
81
126
  name: actionmailer
82
- requirement: &70263342016180 !ruby/object:Gem::Requirement
127
+ requirement: &70102747643360 !ruby/object:Gem::Requirement
83
128
  none: false
84
129
  requirements:
85
130
  - - ! '>='
@@ -87,7 +132,7 @@ dependencies:
87
132
  version: '0'
88
133
  type: :runtime
89
134
  prerelease: false
90
- version_requirements: *70263342016180
135
+ version_requirements: *70102747643360
91
136
  description: Tracks and logs events and sends notifications of events on Active Record
92
137
  models.
93
138
  email:
@@ -97,21 +142,36 @@ extensions: []
97
142
  extra_rdoc_files: []
98
143
  files:
99
144
  - .gitignore
145
+ - .travis.yml
100
146
  - Gemfile
101
147
  - Gemfile.lock
102
148
  - README.textile
103
149
  - Rakefile
104
150
  - eventifier.gemspec
105
151
  - lib/eventifier.rb
106
- - lib/eventifier/event.rb
152
+ - lib/eventifier/active_record/event.rb
153
+ - lib/eventifier/active_record/event_observer.rb
154
+ - lib/eventifier/active_record/event_tracking.rb
155
+ - lib/eventifier/active_record/ghost.rb
156
+ - lib/eventifier/active_record/notification.rb
157
+ - lib/eventifier/active_record_support.rb
107
158
  - lib/eventifier/event_helper.rb
108
- - lib/eventifier/event_observer.rb
159
+ - lib/eventifier/event_mixin.rb
160
+ - lib/eventifier/event_observer_mixin.rb
109
161
  - lib/eventifier/event_tracking.rb
162
+ - lib/eventifier/ghost_mixin.rb
110
163
  - lib/eventifier/helper_methods.rb
111
164
  - lib/eventifier/matchers.rb
112
- - lib/eventifier/notification.rb
165
+ - lib/eventifier/mongoid/event.rb
166
+ - lib/eventifier/mongoid/event_observer.rb
167
+ - lib/eventifier/mongoid/event_tracking.rb
168
+ - lib/eventifier/mongoid/ghost.rb
169
+ - lib/eventifier/mongoid/notification.rb
170
+ - lib/eventifier/mongoid/user_patch.rb
171
+ - lib/eventifier/mongoid_support.rb
113
172
  - lib/eventifier/notification_helper.rb
114
173
  - lib/eventifier/notification_mailer.rb
174
+ - lib/eventifier/notification_mixin.rb
115
175
  - lib/eventifier/version.rb
116
176
  - lib/generators/eventifier/install/install_generator.rb
117
177
  - lib/generators/eventifier/install/templates/event_tracking.rb
@@ -120,7 +180,10 @@ files:
120
180
  - spec/event_helper_spec.rb
121
181
  - spec/event_observer_spec.rb
122
182
  - spec/event_spec.rb
183
+ - spec/event_tracking_spec.rb
123
184
  - spec/eventifier_spec.rb
185
+ - spec/fabricators/fabricator.rb
186
+ - spec/ghost_spec.rb
124
187
  - spec/helper_methods_spec.rb
125
188
  - spec/integration/eventifier_spec.rb
126
189
  - spec/notification_helper_spec.rb
@@ -128,8 +191,10 @@ files:
128
191
  - spec/notification_spec.rb
129
192
  - spec/spec_helper.rb
130
193
  - spec/support/action_mailer.rb
131
- - spec/support/active_record_support.rb
132
- - spec/support/blueprints.rb
194
+ - spec/support/database_cleaner.rb
195
+ - spec/support/model_helpers.rb
196
+ - spec/test_classes/active_record_support.rb
197
+ - spec/test_classes/mongoid_support.rb
133
198
  homepage: http://github.com/inspire9/eventifier
134
199
  licenses: []
135
200
  post_install_message:
@@ -158,7 +223,10 @@ test_files:
158
223
  - spec/event_helper_spec.rb
159
224
  - spec/event_observer_spec.rb
160
225
  - spec/event_spec.rb
226
+ - spec/event_tracking_spec.rb
161
227
  - spec/eventifier_spec.rb
228
+ - spec/fabricators/fabricator.rb
229
+ - spec/ghost_spec.rb
162
230
  - spec/helper_methods_spec.rb
163
231
  - spec/integration/eventifier_spec.rb
164
232
  - spec/notification_helper_spec.rb
@@ -166,5 +234,7 @@ test_files:
166
234
  - spec/notification_spec.rb
167
235
  - spec/spec_helper.rb
168
236
  - spec/support/action_mailer.rb
169
- - spec/support/active_record_support.rb
170
- - spec/support/blueprints.rb
237
+ - spec/support/database_cleaner.rb
238
+ - spec/support/model_helpers.rb
239
+ - spec/test_classes/active_record_support.rb
240
+ - spec/test_classes/mongoid_support.rb
@@ -1,35 +0,0 @@
1
- module Eventifier
2
- class Event < ::ActiveRecord::Base
3
-
4
- belongs_to :user
5
- belongs_to :eventable, :polymorphic => true
6
- has_many :notifications
7
-
8
- serialize :change_data
9
-
10
- validates :user, :presence => true
11
- validates :eventable, :presence => true
12
- validates :verb, :presence => true
13
-
14
- def self.add_notification(*arg)
15
- observer_instances.each { |observer| observer.add_notification(*arg) }
16
- end
17
-
18
-
19
- def self.create_event(verb, object, options = {})
20
- changed_data = object.changes.stringify_keys
21
- changed_data = changed_data.reject { |attribute, value| options[:except].include?(attribute) } if options[:except]
22
- changed_data = changed_data.select { |attribute, value| options[:only].include?(attribute) } if options[:only]
23
- self.create(
24
- :user => object.user,
25
- :eventable => object,
26
- :verb => verb,
27
- :change_data => changed_data.symbolize_keys
28
- )
29
- end
30
-
31
- def self.find_all_by_eventable object
32
- where :eventable_id => object.id, :eventable_type => object.class.name
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- module Eventifier
2
- class EventObserver < ActiveRecord::Observer
3
- observe Eventifier::Event
4
-
5
- def add_notification klass_name, relation, method
6
- observed_classes.each do |observed_class|
7
- notification_mappings[klass_name.name] ||= {}
8
- notification_mappings[klass_name.name][method] = relation
9
- end
10
- end
11
-
12
- def after_create event
13
- Rails.logger.info "Firing #{event.eventable_type}##{event.verb} - #{notification_mappings[event.eventable_type][event.verb]}" if notification_mappings.has_key?(event.eventable_type) and notification_mappings[event.eventable_type].has_key?(event.verb) and defined?(Rails)
14
- method_from_relation(event.eventable, notification_mappings[event.eventable_type][event.verb]).each do |user|
15
- next if user == event.user
16
- Eventifier::Notification.create :event => event, :user => user
17
- end if notification_mappings.has_key?(event.eventable_type) and notification_mappings[event.eventable_type].has_key?(event.verb)
18
- end
19
-
20
- def method_from_relation object, relation
21
- if relation.kind_of?(Hash)
22
- method_from_relation(proc { |object, method| object.send(method) }.call(object, relation.keys.first), relation.values.first)
23
- else
24
- send_to = proc { |object, method| object.send(method) }.call(object, relation)
25
- send_to = send_to.kind_of?(Array) ? send_to : [send_to]
26
- end
27
- end
28
-
29
- private
30
-
31
- def notification_mappings
32
- @notification_mapppings ||= {}
33
- end
34
- end
35
- end
@@ -1,40 +0,0 @@
1
- module Eventifier
2
- class Notification < ActiveRecord::Base
3
- belongs_to :event
4
- belongs_to :user
5
-
6
- default_scope order("created_at DESC")
7
- scope :for_events, lambda { |ids| where(:event_id => ids) }
8
- scope :for_user, lambda { |id| where(:user_id => id) }
9
- scope :latest, order('notifications.created_at DESC').limit(5)
10
-
11
- validates :event, :presence => true
12
- validates :user, :presence => true
13
- validates :event_id, :uniqueness => { :scope => :user_id }
14
-
15
- after_create :send_email
16
-
17
- def self.expire_for_past_events!(time_limit = 1.day.ago)
18
- self.for_events(Event.expired_ids(time_limit)).each &:expire!
19
- end
20
-
21
- def self.unread_for(user)
22
- if user.notifications_last_read_at
23
- for_user(user).
24
- where("notifications.created_at > ?", user.notifications_last_read_at)
25
- else
26
- for_user(user)
27
- end
28
- end
29
-
30
- def unread_for?(user)
31
- return true if user.notifications_last_read_at.nil?
32
- created_at > user.notifications_last_read_at
33
- end
34
-
35
- def send_email
36
- # TODO: Are we okay to have notifier sit on the gem? How is this going to be handled?
37
- Eventifier::NotificationMailer.notification_email(self).deliver
38
- end
39
- end
40
- end
@@ -1,24 +0,0 @@
1
- require 'machinist/active_record'
2
- require 'eventifier'
3
-
4
- Eventifier::Event.blueprint do
5
- user { object.user || User.make! }
6
- eventable { object.eventable || Post.make! }
7
- verb { :update }
8
- change_data { { :date => [ 5.days.ago, 3.days.ago ] } }
9
- end
10
-
11
- Eventifier::Notification.blueprint do
12
- event { object.event || Eventifier::Event.make! }
13
- user { object.user || User.make! }
14
- end
15
-
16
- Post.blueprint do
17
- title { "My amazing blog post" }
18
- body { "A deep and profound analysis of life" }
19
- end
20
-
21
- User.blueprint do
22
- name { "Billy #{sn}" }
23
- email { "billy#{sn}@email.com" }
24
- end