giraffesoft-timeline_fu 0.1.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.
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.
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/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 1
@@ -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,27 @@
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
+
11
+ method_name = :"fire_#{event_type}_after_#{opts[:on]}"
12
+ define_method(method_name) do
13
+ create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
14
+ memo[sym] = send(opts[sym]) if opts[sym]
15
+ memo
16
+ end
17
+ create_options[:subject] ||= self
18
+ create_options[:event_type] = event_type.to_s
19
+
20
+ TimelineEvent.create!(create_options)
21
+ end
22
+
23
+ send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,61 @@
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
+ end
@@ -0,0 +1,82 @@
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, :actor => :author,
51
+ :on => :create
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
+ end
62
+
63
+ TimelineEvent = Class.new
64
+
65
+ class Test::Unit::TestCase
66
+ protected
67
+ def hash_for_list(opts = {})
68
+ {:title => 'whatever'}.merge(opts)
69
+ end
70
+
71
+ def create_list(opts = {})
72
+ List.create!(hash_for_list(opts))
73
+ end
74
+
75
+ def hash_for_person(opts = {})
76
+ {:email => 'james'}.merge(opts)
77
+ end
78
+
79
+ def create_person(opts = {})
80
+ Person.create!(hash_for_person(opts))
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: giraffesoft-timeline_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
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-02-20 00:00:00 -08: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
+
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.rb
38
+ - test/fires_test.rb
39
+ - test/test_helper.rb
40
+ has_rdoc: false
41
+ homepage: http://github.com/giraffesoft/timeline_fu
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Easily build timelines, much like GitHub's news feed
66
+ test_files: []
67
+