mifix-timeline_fu 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,110 @@
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.
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
+ - any other parameter is treated as TimelineEvent's attribute
47
+ - This is useful if you add a custom field to timeline_events table
48
+
49
+ Here's another example:
50
+
51
+ class Comment < ActiveRecord::Base
52
+ #...
53
+ belongs_to :commenter, :class_name => 'Person'
54
+ belongs_to :post
55
+ fires :new_comment, :on => :create,
56
+ :actor => :commenter,
57
+ #implicit :subject => self,
58
+ :secondary_subject => 'post',
59
+ :if => lambda { |comment| comment.commenter != comment.post.author }
60
+ end
61
+
62
+ === TimelineEvent instantiation
63
+
64
+ The ActiveRecord event hook will automatically instantiate a
65
+ TimelineEvent instance for you.
66
+ It will receive the following parameters in #create!
67
+
68
+ - event_type
69
+ - "new_comment" in the comment example
70
+ - actor
71
+ - the commenter
72
+ - subject
73
+ - the comment instance
74
+ - secondary_subject
75
+ - the post instance
76
+
77
+ The generated model stores most of its info as polymorphic relationships.
78
+
79
+ class TimelineEvent < ActiveRecord::Base
80
+ belongs_to :actor, :polymorphic => true
81
+ belongs_to :subject, :polymorphic => true
82
+ belongs_to :secondary_subject, :polymorphic => true
83
+ end
84
+
85
+ == How you actually get your timeline
86
+
87
+ To get your timeline you'll probably have to create your own finder SQL or scopes
88
+ (if your situation is extremely simple).
89
+
90
+ TimelineFu is not currently providing anything to generate your timeline because
91
+ different situations will have wildly different requirements. Like access control
92
+ issues and actually just what crazy stuff you're cramming in that timeline.
93
+
94
+ We're not saying it can't be done, just that we haven't done it yet.
95
+ Contributions are welcome :-)
96
+
97
+ == Get it
98
+
99
+ timeline_fu can be used as a plugin:
100
+
101
+ $ script/plugin install git://github.com/giraffesoft/timeline_fu.git
102
+
103
+ or as a gem plugin:
104
+
105
+ config.gem "giraffesoft-timeline_fu", :lib => "timeline_fu",
106
+ :source => "http://gems.github.com"
107
+
108
+ == License
109
+
110
+ Copyright (c) 2008 James Golick, released under the MIT license
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 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
@@ -0,0 +1,6 @@
1
+ require 'timeline_fu/fires'
2
+
3
+ module TimelineFu
4
+ end
5
+
6
+ ActiveRecord::Base.send :include, TimelineFu::Fires
@@ -0,0 +1,35 @@
1
+ module TimelineFu
2
+ module Fires
3
+ def self.included(klass)
4
+ klass.send(:extend, ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def fires(event_type, opts)
9
+ raise ArgumentError, "Argument :on is mandatory" unless opts.has_key?(:on)
10
+ opts[:subject] = :self unless opts.has_key?(:subject)
11
+
12
+ on = opts.delete(:on)
13
+ _if = opts.delete(:if)
14
+
15
+ method_name = :"fire_#{event_type}_after_#{on}"
16
+ define_method(method_name) do
17
+ create_options = opts.keys.inject({}) do |memo, sym|
18
+ case opts[sym]
19
+ when :self
20
+ memo[sym] = self
21
+ else
22
+ memo[sym] = send(opts[sym]) if opts[sym]
23
+ end
24
+ memo
25
+ end
26
+ create_options[:event_type] = event_type.to_s
27
+
28
+ TimelineEvent.create!(create_options)
29
+ end
30
+
31
+ send(:"after_#{on}", method_name, :if => _if)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module TimelineFu
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 TimelineFu
2
+ module Matchers
3
+ class FireEvent
4
+ def initialize(event_type, opts = {})
5
+ @event_type = event_type
6
+ @opts = opts
7
+ @method = :"fire_#{@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
+ "fire 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 fire_event(event_type, opts)
55
+ FireEvent.new(event_type, opts)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__)+'/test_helper'
2
+
3
+ class FiresTest < 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
+ TimelineEvent.expects(:create!).with(:actor => @james, :subject => @list, :event_type => 'list_created')
12
+ @list.save
13
+ end
14
+
15
+ def test_should_fire_event_with_secondary_subject
16
+ @list = List.new(hash_for_list(:author => @james));
17
+ TimelineEvent.stubs(:create!)
18
+ @list.save
19
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
20
+ TimelineEvent.expects(:create!).with(:actor => @mat,
21
+ :subject => @comment,
22
+ :secondary_subject => @list,
23
+ :event_type => 'comment_created')
24
+ @comment.save
25
+ end
26
+
27
+ def test_exception_raised_if_on_missing
28
+ # This needs to be tested with should_raise, to check out the msg content
29
+ assert_raise(ArgumentError) do
30
+ some_class = Class.new(ActiveRecord::Base)
31
+ some_class.class_eval do
32
+ attr_accessor :someone
33
+ fires :some_event, :actor => :someone
34
+ end
35
+ end
36
+ end
37
+
38
+ def test_should_only_fire_if_the_condition_evaluates_to_true
39
+ TimelineEvent.expects(:create!).with(:actor => @mat, :subject => @james, :event_type => 'follow_created')
40
+ @james.new_watcher = @mat
41
+ @james.save
42
+ end
43
+
44
+ def test_should_not_fire_if_the_if_condition_evaluates_to_false
45
+ TimelineEvent.expects(:create!).never
46
+ @james.new_watcher = nil
47
+ @james.save
48
+ end
49
+
50
+ def test_should_fire_event_with_symbol_based_if_condition_that_is_true
51
+ @james.fire = true
52
+ TimelineEvent.expects(:create!).with(:subject => @james, :event_type => 'person_updated')
53
+ @james.save
54
+ end
55
+
56
+ def test_should_fire_event_with_symbol_based_if_condition
57
+ @james.fire = false
58
+ TimelineEvent.expects(:create!).never
59
+ @james.save
60
+ end
61
+
62
+ def test_should_set_secondary_subject_to_self_when_requested
63
+ @list = List.new(hash_for_list(:author => @james));
64
+ TimelineEvent.stubs(:create!).with(has_entry(:event_type, "list_created"))
65
+ @list.save
66
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
67
+ TimelineEvent.stubs(:create!).with(has_entry(:event_type, "comment_created"))
68
+ @comment.save
69
+ TimelineEvent.expects(:create!).with(:actor => @mat,
70
+ :subject => @list,
71
+ :secondary_subject => @comment,
72
+ :event_type => 'comment_deleted')
73
+ @comment.destroy
74
+ end
75
+
76
+ def test_should_set_additional_attributes_when_present
77
+ @site = Site.create(:name => 'foo.com')
78
+ @article = Article.new(:body => 'cool article!', :author => @james, :site => @site)
79
+ TimelineEvent.expects(:create!).with(:actor => @james, :subject => @article, :event_type => 'article_created', :site => @site)
80
+ @article.save
81
+ end
82
+ end
@@ -0,0 +1,107 @@
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
+
31
+ create_table :sites do |t|
32
+ t.string :name
33
+ end
34
+
35
+ create_table :articles do |t|
36
+ t.integer :site_id
37
+ t.string :body
38
+ end
39
+ end
40
+
41
+ class Person < ActiveRecord::Base
42
+ attr_accessor :new_watcher, :fire
43
+
44
+ fires :follow_created, :on => :update,
45
+ :actor => :new_watcher,
46
+ :if => lambda { |person| !person.new_watcher.nil? }
47
+ fires :person_updated, :on => :update,
48
+ :if => :fire?
49
+
50
+ def fire?
51
+ new_watcher.nil? && fire
52
+ end
53
+ end
54
+
55
+ class List < ActiveRecord::Base
56
+ belongs_to :author, :class_name => "Person"
57
+ has_many :comments
58
+
59
+ fires :list_created, :actor => :author,
60
+ :on => :create
61
+ end
62
+
63
+ class Comment < ActiveRecord::Base
64
+ belongs_to :list
65
+ belongs_to :author, :class_name => "Person"
66
+
67
+ fires :comment_created, :actor => :author,
68
+ :on => :create,
69
+ :secondary_subject => :list
70
+ fires :comment_deleted, :actor => :author,
71
+ :on => :destroy,
72
+ :subject => :list,
73
+ :secondary_subject => :self
74
+ end
75
+
76
+ class Site < ActiveRecord::Base
77
+ end
78
+
79
+ class Article < ActiveRecord::Base
80
+ belongs_to :author, :class_name => "Person"
81
+ belongs_to :site
82
+
83
+ fires :article_created, :actor => :author,
84
+ :on => :create,
85
+ :site => :site
86
+ end
87
+
88
+ TimelineEvent = Class.new
89
+
90
+ class Test::Unit::TestCase
91
+ protected
92
+ def hash_for_list(opts = {})
93
+ {:title => 'whatever'}.merge(opts)
94
+ end
95
+
96
+ def create_list(opts = {})
97
+ List.create!(hash_for_list(opts))
98
+ end
99
+
100
+ def hash_for_person(opts = {})
101
+ {:email => 'james'}.merge(opts)
102
+ end
103
+
104
+ def create_person(opts = {})
105
+ Person.create!(hash_for_person(opts))
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mifix-timeline_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.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-05-26 00:00:00 -07: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
+ - README.rdoc
28
+ - VERSION.yml
29
+ - generators/timeline_fu
30
+ - generators/timeline_fu/templates
31
+ - generators/timeline_fu/templates/migration.rb
32
+ - generators/timeline_fu/templates/model.rb
33
+ - generators/timeline_fu/timeline_fu_generator.rb
34
+ - generators/timeline_fu/USAGE
35
+ - lib/timeline_fu
36
+ - lib/timeline_fu/fires.rb
37
+ - lib/timeline_fu/macros.rb
38
+ - lib/timeline_fu/matchers.rb
39
+ - lib/timeline_fu.rb
40
+ - test/fires_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/giraffesoft/timeline_fu
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --inline-source
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.2.0
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Easily build timelines, much like GitHub's news feed
69
+ test_files: []
70
+