notification_fu 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 James Golick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,108 @@
1
+ = TimelineFu
2
+
3
+ Easily build timelines, much like GitHub's news feed.
4
+
5
+ == Usage
6
+
7
+ TimelineFu requires you to have a TimelineEvent model.
8
+ The simplest way is to use the generator.
9
+
10
+ $ script/generate timeline_fu && rake db:migrate
11
+ exists db/migrate
12
+ create db/migrate/20090333222034_create_timeline_events.rb
13
+ create app/models/timeline_event.rb
14
+ # Migration blabber...
15
+
16
+ Next step is to determine what generates an event in your various models.
17
+
18
+ class Post < ActiveRecord::Base
19
+ #...
20
+ belongs_to :author, :class_name => 'Person'
21
+ fires :new_post, :on => :create,
22
+ :actor => :author
23
+ end
24
+
25
+ You can add 'fires' statements to as many models as you want on as many models
26
+ as you want.
27
+
28
+ They are hooked for you after standard ActiveRecord events. In
29
+ the previous example, it's an after_create on Posts.
30
+
31
+ === Parameters for #fires
32
+
33
+ You can supply a few parameters to fires, two of them are mandatory.
34
+ - the first param is a custom name for the event type. It'll be your way of figuring out what events your reading back from the timeline_events table later.
35
+ - :new_post in the example
36
+
37
+ The rest all fit neatly in an options hash.
38
+
39
+ - :on => [ActiveRecord event]
40
+ - mandatory. You use it to specify whether you want the event created after a create, update or destroy. You can also supply an array of events, e.g. [:create, :update].
41
+ - :actor is your way of specifying who took this action.
42
+ - In the example, post.author is going to be this person.
43
+ - :subject is automatically set to self, which is good most of the time. You can however override it if you need to, using :subject.
44
+ - :secondary_subject can let you specify something else that's related to the event. A comment to a blog post would be a good example.
45
+ - :if => symbol or proc/lambda lets you put conditions on when a TimelineEvent is created. It's passed right to the after_xxx ActiveRecord event hook, so it's has the same behavior.
46
+
47
+ Here's another example:
48
+
49
+ class Comment < ActiveRecord::Base
50
+ #...
51
+ belongs_to :commenter, :class_name => 'Person'
52
+ belongs_to :post
53
+ fires :new_comment, :on => :create,
54
+ :actor => :commenter,
55
+ #implicit :subject => self,
56
+ :secondary_subject => 'post',
57
+ :if => lambda { |comment| comment.commenter != comment.post.author }
58
+ end
59
+
60
+ === TimelineEvent instantiation
61
+
62
+ The ActiveRecord event hook will automatically instantiate a
63
+ TimelineEvent instance for you.
64
+ It will receive the following parameters in #create!
65
+
66
+ - event_type
67
+ - "new_comment" in the comment example
68
+ - actor
69
+ - the commenter
70
+ - subject
71
+ - the comment instance
72
+ - secondary_subject
73
+ - the post instance
74
+
75
+ The generated model stores most of its info as polymorphic relationships.
76
+
77
+ class TimelineEvent < ActiveRecord::Base
78
+ belongs_to :actor, :polymorphic => true
79
+ belongs_to :subject, :polymorphic => true
80
+ belongs_to :secondary_subject, :polymorphic => true
81
+ end
82
+
83
+ == How you actually get your timeline
84
+
85
+ To get your timeline you'll probably have to create your own finder SQL or scopes
86
+ (if your situation is extremely simple).
87
+
88
+ TimelineFu is not currently providing anything to generate your timeline because
89
+ different situations will have wildly different requirements. Like access control
90
+ issues and actually just what crazy stuff you're cramming in that timeline.
91
+
92
+ We're not saying it can't be done, just that we haven't done it yet.
93
+ Contributions are welcome :-)
94
+
95
+ == Get it
96
+
97
+ timeline_fu can be used as a plugin:
98
+
99
+ $ script/plugin install git://github.com/giraffesoft/timeline_fu.git
100
+
101
+ or as a gem plugin:
102
+
103
+ config.gem "giraffesoft-timeline_fu", :lib => "timeline_fu",
104
+ :source => "http://gems.github.com"
105
+
106
+ == License
107
+
108
+ Copyright (c) 2008 James Golick, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the timeline_fu plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ begin
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |s|
18
+ s.name = "timeline_fu"
19
+ s.summary = %Q{Easily build timelines, much like GitHub's news feed}
20
+ s.email = "james@giraffesoft.ca"
21
+ s.homepage = "http://github.com/giraffesoft/timeline_fu"
22
+ s.description = "Easily build timelines, much like GitHub's news feed"
23
+ s.authors = ["James Golick", "Mathieu Martin", "Francois Beausoleil"]
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ desc 'Generate documentation for the timeline_fu plugin.'
30
+ Rake::RDocTask.new(:rdoc) do |rdoc|
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = 'TimelineFu'
33
+ rdoc.options << '--line-numbers' << '--inline-source'
34
+ rdoc.rdoc_files.include('README*')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 3
4
+ :patch: 0
@@ -0,0 +1,2 @@
1
+ Generates both the TimelineEvent class and the migration to create its table. The table will have subject, actor and secondary actor as polymorphic associations.
2
+ The use of this generator is optional. See README for more details.
@@ -0,0 +1,15 @@
1
+ class CreateTimelineEvents < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :timeline_events do |t|
4
+ t.string :event_type, :subject_type, :actor_type, :secondary_subject_type
5
+ t.integer :subject_id, :actor_id, :secondary_subject_id
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :timeline_events
12
+ end
13
+ end
14
+
15
+
@@ -0,0 +1,5 @@
1
+ class TimelineEvent < ActiveRecord::Base
2
+ belongs_to :actor, :polymorphic => true
3
+ belongs_to :subject, :polymorphic => true
4
+ belongs_to :secondary_subject, :polymorphic => true
5
+ end
@@ -0,0 +1,9 @@
1
+ class TimelineFuGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate',
5
+ :migration_file_name => 'create_timeline_events'
6
+ m.template 'model.rb', 'app/models/timeline_event.rb'
7
+ end
8
+ end
9
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'timeline_fu'
@@ -0,0 +1,6 @@
1
+ require 'notification_fu/notifies'
2
+
3
+ module NotificationFu
4
+ end
5
+
6
+ ActiveRecord::Base.send :include, NotificationFu::Notifies
@@ -0,0 +1,20 @@
1
+ module NotificationFu
2
+ module Macros
3
+ def should_fire_event(event_type, opts = {})
4
+ should "fire #{event_type} on #{opts[:on]}" do
5
+ matcher = fire_event(event_type, opts)
6
+
7
+ assert_accepts matcher, self.class.name.gsub(/Test$/, '').constantize
8
+ end
9
+ end
10
+
11
+ def should_not_fire_event(event_type, opts = {})
12
+ should "fire #{event_type} on #{opts[:on]}" do
13
+ matcher = fire_event(event_type, opts)
14
+
15
+ assert_rejects matcher, self.class.name.gsub(/Test$/, '').constantize
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,58 @@
1
+ module NotificationFu
2
+ module Matchers
3
+ class NotifyEvent
4
+ def initialize(event_type, opts = {})
5
+ @event_type = event_type
6
+ @opts = opts
7
+ @method = :"notify_#{@event_type}_after_#{@opts[:on]}"
8
+ end
9
+
10
+ def matches?(subject)
11
+ @subject = subject
12
+
13
+ defines_callback_method? && setups_up_callback?
14
+ end
15
+
16
+ def defines_callback_method?
17
+ if @subject.instance_methods.include?(@method.to_s)
18
+ true
19
+ else
20
+ @missing = "#{@subject.name} does not respond to #{@method}"
21
+ false
22
+ end
23
+ end
24
+
25
+ def setups_up_callback?
26
+ callback_chain_name = "after_#{@opts[:on]}_callback_chain"
27
+ callback_chain = @subject.send(callback_chain_name)
28
+ if callback_chain.any? {|chain| chain.method == @method }
29
+ true
30
+ else
31
+ @missing = "does setup after #{@opts[:on]} callback for #{@method}"
32
+ false
33
+ end
34
+ end
35
+
36
+ def description
37
+ "Notify a #{@event_type} event"
38
+ end
39
+
40
+ def expectation
41
+ expected = "#{@subject.name} to #{description}"
42
+ end
43
+
44
+ def failure_message
45
+ "Expected #{expectation} (#{@missing})"
46
+ end
47
+
48
+ def negative_failure_message
49
+ "Did not expect #{expectation}"
50
+ end
51
+
52
+ end
53
+
54
+ def notify_event(event_type, opts)
55
+ NotifyEvent.new(event_type, opts)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,39 @@
1
+ module NotificationFu
2
+ module Notifies
3
+ def self.included(klass)
4
+ klass.send(:extend, ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def notifies(event_type, opts)
9
+ raise ArgumentError, "Argument :on is mandatory" unless opts.has_key?(:on)
10
+
11
+ # Array provided, set multiple callbacks
12
+ if opts[:on].kind_of?(Array)
13
+ opts[:on].each { |on| notifies(event_type, opts.merge({:on => on})) }
14
+ return
15
+ end
16
+
17
+ opts[:subject] = :self unless opts.has_key?(:subject)
18
+
19
+ method_name = :"notify_#{event_type}_after_#{opts[:on]}"
20
+ define_method(method_name) do
21
+ create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
22
+ case opts[sym]
23
+ when :self
24
+ memo[sym] = self
25
+ else
26
+ memo[sym] = send(opts[sym]) if opts[sym]
27
+ end
28
+ memo
29
+ end
30
+ create_options[:event_type] = event_type.to_s
31
+
32
+ Notification.create!(create_options)
33
+ end
34
+
35
+ send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{notification_fu}
5
+ s.version = "0.4.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["James Golick", "Mathieu Martin", "Francois Beausoleil"]
9
+ s.date = %q{2009-06-26}
10
+ s.description = %q{Easily build timelines, much like GitHub's news feed}
11
+ s.email = %q{james@giraffesoft.ca}
12
+ s.extra_rdoc_files = [
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "MIT-LICENSE",
18
+ "README.rdoc",
19
+ "Rakefile",
20
+ "VERSION.yml",
21
+ "generators/notification_fu/USAGE",
22
+ "generators/notification_fu/templates/migration.rb",
23
+ "generators/notification_fu/templates/model.rb",
24
+ "generators/notification_fu/timeline_fu_generator.rb",
25
+ "init.rb",
26
+ "lib/notification_fu.rb",
27
+ "lib/notification_fu/notifies.rb",
28
+ "lib/notification_fu/macros.rb",
29
+ "lib/notification_fu/matchers.rb",
30
+ "shoulda_macros/notification_fu_shoulda.rb",
31
+ "test/notifies_test.rb",
32
+ "test/test_helper.rb",
33
+ "notification_fu.gemspec"
34
+ ]
35
+ s.has_rdoc = true
36
+ s.homepage = %q{http://github.com/localocracy/notification_fu}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.1}
40
+ s.summary = %q{Easily build timelines, much like GitHub's news feed}
41
+ s.test_files = [
42
+ "test/notifies_test.rb",
43
+ "test/test_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 2
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ else
52
+ end
53
+ else
54
+ end
55
+ end
@@ -0,0 +1,11 @@
1
+ require 'timeline_fu/matchers'
2
+ require 'timeline_fu/macros'
3
+
4
+ module ActiveSupport
5
+ class TestCase
6
+ include NotificationFu::Matchers
7
+ if ! defined? Spec
8
+ extend NotificationFu::Macros
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__)+'/test_helper'
2
+
3
+ class NotifiesTest < Test::Unit::TestCase
4
+ def setup
5
+ @james = create_person(:email => 'james@giraffesoft.ca')
6
+ @mat = create_person(:email => 'mat@giraffesoft.ca')
7
+ end
8
+
9
+ def test_should_fire_the_appropriate_callback
10
+ @list = List.new(hash_for_list(:author => @james));
11
+ Notification.expects(:create!).with(:actor => @james, :subject => @list, :event_type => 'list_created_or_updated')
12
+ @list.save
13
+ Notification.expects(:create!).with(:actor => @mat, :subject => @list, :event_type => 'list_created_or_updated')
14
+ @list.author = @mat
15
+ @list.save
16
+ end
17
+
18
+ def test_should_fire_event_with_secondary_subject
19
+ @list = List.new(hash_for_list(:author => @james));
20
+ Notification.stubs(:create!)
21
+ @list.save
22
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
23
+ Notification.expects(:create!).with(:actor => @mat,
24
+ :subject => @comment,
25
+ :secondary_subject => @list,
26
+ :event_type => 'comment_created')
27
+ @comment.save
28
+ end
29
+
30
+ def test_exception_raised_if_on_missing
31
+ # This needs to be tested with should_raise, to check out the msg content
32
+ assert_raise(ArgumentError) do
33
+ some_class = Class.new(ActiveRecord::Base)
34
+ some_class.class_eval do
35
+ attr_accessor :someone
36
+ fires :some_event, :actor => :someone
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_should_only_fire_if_the_condition_evaluates_to_true
42
+ Notification.expects(:create!).with(:actor => @mat, :subject => @james, :event_type => 'follow_created')
43
+ @james.new_watcher = @mat
44
+ @james.save
45
+ end
46
+
47
+ def test_should_not_fire_if_the_if_condition_evaluates_to_false
48
+ Notification.expects(:create!).never
49
+ @james.new_watcher = nil
50
+ @james.save
51
+ end
52
+
53
+ def test_should_fire_event_with_symbol_based_if_condition_that_is_true
54
+ @james.fire = true
55
+ Notification.expects(:create!).with(:subject => @james, :event_type => 'person_updated')
56
+ @james.save
57
+ end
58
+
59
+ def test_should_fire_event_with_symbol_based_if_condition
60
+ @james.fire = false
61
+ Notification.expects(:create!).never
62
+ @james.save
63
+ end
64
+
65
+ def test_should_set_secondary_subject_to_self_when_requested
66
+ @list = List.new(hash_for_list(:author => @james));
67
+ Notification.stubs(:create!).with(has_entry(:event_type, "list_created_or_updated"))
68
+ @list.save
69
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
70
+ Notification.stubs(:create!).with(has_entry(:event_type, "comment_created"))
71
+ @comment.save
72
+ Notification.expects(:create!).with(:actor => @mat,
73
+ :subject => @list,
74
+ :secondary_subject => @comment,
75
+ :event_type => 'comment_deleted')
76
+ @comment.destroy
77
+ end
78
+ end
@@ -0,0 +1,86 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'mocha'
4
+ require 'test/unit'
5
+ require 'logger'
6
+
7
+ require File.dirname(__FILE__)+'/../lib/timeline_fu'
8
+
9
+ ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
10
+ ActiveRecord::Base.establish_connection('sqlite3')
11
+
12
+ ActiveRecord::Base.logger = Logger.new(STDERR)
13
+ ActiveRecord::Base.logger.level = Logger::WARN
14
+
15
+ ActiveRecord::Schema.define(:version => 0) do
16
+ create_table :people do |t|
17
+ t.string :email, :default => ''
18
+ t.string :password, :default => ''
19
+ end
20
+
21
+ create_table :lists do |t|
22
+ t.integer :author_id
23
+ t.string :title
24
+ end
25
+
26
+ create_table :comments do |t|
27
+ t.integer :list_id, :author_id
28
+ t.string :body
29
+ end
30
+ end
31
+
32
+ class Person < ActiveRecord::Base
33
+ attr_accessor :new_watcher, :fire
34
+
35
+ fires :follow_created, :on => :update,
36
+ :actor => :new_watcher,
37
+ :if => lambda { |person| !person.new_watcher.nil? }
38
+ fires :person_updated, :on => :update,
39
+ :if => :fire?
40
+
41
+ def fire?
42
+ new_watcher.nil? && fire
43
+ end
44
+ end
45
+
46
+ class List < ActiveRecord::Base
47
+ belongs_to :author, :class_name => "Person"
48
+ has_many :comments
49
+
50
+ fires :list_created_or_updated, :actor => :author,
51
+ :on => [:create, :update]
52
+ end
53
+
54
+ class Comment < ActiveRecord::Base
55
+ belongs_to :list
56
+ belongs_to :author, :class_name => "Person"
57
+
58
+ fires :comment_created, :actor => :author,
59
+ :on => :create,
60
+ :secondary_subject => :list
61
+ fires :comment_deleted, :actor => :author,
62
+ :on => :destroy,
63
+ :subject => :list,
64
+ :secondary_subject => :self
65
+ end
66
+
67
+ TimelineEvent = Class.new
68
+
69
+ class Test::Unit::TestCase
70
+ protected
71
+ def hash_for_list(opts = {})
72
+ {:title => 'whatever'}.merge(opts)
73
+ end
74
+
75
+ def create_list(opts = {})
76
+ List.create!(hash_for_list(opts))
77
+ end
78
+
79
+ def hash_for_person(opts = {})
80
+ {:email => 'james'}.merge(opts)
81
+ end
82
+
83
+ def create_person(opts = {})
84
+ Person.create!(hash_for_person(opts))
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notification_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - James Golick
8
+ - Mathieu Martin
9
+ - Francois Beausoleil
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-06-26 00:00:00 -04:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: Easily build timelines, much like GitHub's news feed
19
+ email: james@giraffesoft.ca
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - README.rdoc
26
+ files:
27
+ - .gitignore
28
+ - MIT-LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - generators/notification_fu/USAGE
33
+ - generators/notification_fu/templates/migration.rb
34
+ - generators/notification_fu/templates/model.rb
35
+ - generators/notification_fu/timeline_fu_generator.rb
36
+ - init.rb
37
+ - lib/notification_fu.rb
38
+ - lib/notification_fu/notifies.rb
39
+ - lib/notification_fu/macros.rb
40
+ - lib/notification_fu/matchers.rb
41
+ - shoulda_macros/notification_fu_shoulda.rb
42
+ - test/notifies_test.rb
43
+ - test/test_helper.rb
44
+ - notification_fu.gemspec
45
+ has_rdoc: true
46
+ homepage: http://github.com/localocracy/notification_fu
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Easily build timelines, much like GitHub's news feed
73
+ test_files:
74
+ - test/notifies_test.rb
75
+ - test/test_helper.rb