alerts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg
2
+ .DS_Store
@@ -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.
@@ -0,0 +1,102 @@
1
+ = Alerts
2
+
3
+ Easily build timelines, much like GitHub's news feed.
4
+
5
+ == Usage
6
+
7
+ Alerts requires you to have a Alert model.
8
+ The simplest way is to use the generator.
9
+
10
+ $ script/generate alerts && rake db:migrate
11
+ exists db/migrate
12
+ create db/migrate/20090333222034_create_alerts.rb
13
+ create app/models/alert.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 alerts 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 Alert 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
+ === Alert instantiation
61
+
62
+ The ActiveRecord event hook will automatically instantiate a
63
+ Alert 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 Alert < 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
+ Alerts 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
+ # Gemfile
98
+ gem "alerts"
99
+
100
+ == License
101
+
102
+ Copyright (c) 2008 James Golick, released under the MIT license
@@ -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 alerts 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 = "alerts"
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/alerts"
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 alerts plugin.'
30
+ Rake::RDocTask.new(:rdoc) do |rdoc|
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = 'Alerts'
33
+ rdoc.options << '--line-numbers' << '--inline-source'
34
+ rdoc.rdoc_files.include('README*')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 4
4
+ :patch: 1
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{alerts}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ritvvij Parrikh"]
9
+ s.date = %q{2011-06-23}
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/alerts/USAGE",
22
+ "generators/alerts/templates/migration.rb",
23
+ "generators/alerts/templates/model.rb",
24
+ "generators/alerts/alerts_generator.rb",
25
+ "init.rb",
26
+ "lib/alerts.rb",
27
+ "lib/alerts/fires.rb",
28
+ "lib/alerts/macros.rb",
29
+ "lib/alerts/matchers.rb",
30
+ "shoulda_macros/alerts_shoulda.rb",
31
+ "test/fires_test.rb",
32
+ "test/test_helper.rb",
33
+ "alerts.gemspec"
34
+ ]
35
+ s.has_rdoc = true
36
+ s.homepage = %q{http://github.com/ritvvijparrikh/alerts}
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/fires_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,2 @@
1
+ Generates both the Alert 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,9 @@
1
+ class AlertsGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate',
5
+ :migration_file_name => 'create_alerts'
6
+ m.template 'model.rb', 'app/models/alerts.rb'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAlerts < ActiveRecord::Migration
2
+ def self.change
3
+ create_table :alerts 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.text :message, :link
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
11
+
12
+
@@ -0,0 +1,5 @@
1
+ class Alert < ActiveRecord::Base
2
+ belongs_to :actor, :polymorphic => true
3
+ belongs_to :subject, :polymorphic => true
4
+ belongs_to :secondary_subject, :polymorphic => true
5
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'alerts'
@@ -0,0 +1,6 @@
1
+ require 'alerts/fires'
2
+
3
+ module Alerts
4
+ end
5
+
6
+ ActiveRecord::Base.send :include, Alerts::Fires
@@ -0,0 +1,42 @@
1
+ module Alerts
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
+ # Array provided, set multiple callbacks
12
+ if opts[:on].kind_of?(Array)
13
+ opts[:on].each { |on| fires(event_type, opts.merge({:on => on})) }
14
+ return
15
+ end
16
+
17
+ opts[:subject] = :self unless opts.has_key?(:subject)
18
+
19
+ method_name = :"fire_#{event_type}_after_#{opts[:on]}"
20
+ define_method(method_name) do
21
+ create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
22
+ if opts[sym]
23
+ if opts[sym].respond_to?(:call)
24
+ memo[sym] = opts[sym].call(self)
25
+ elsif opts[sym] == :self
26
+ memo[sym] = self
27
+ else
28
+ memo[sym] = send(opts[sym])
29
+ end
30
+ end
31
+ memo
32
+ end
33
+ create_options[:event_type] = event_type.to_s
34
+
35
+ Alert.create!(create_options)
36
+ end
37
+
38
+ send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module Alerts
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 Alerts
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,11 @@
1
+ require 'alerts/matchers'
2
+ require 'alerts/macros'
3
+
4
+ module ActiveSupport
5
+ class TestCase
6
+ include Alerts::Matchers
7
+ if ! defined? Spec
8
+ extend Alerts::Macros
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,78 @@
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
+ Alert.expects(:create!).with(:actor => @james, :subject => @list, :event_type => 'list_created_or_updated')
12
+ @list.save
13
+ Alert.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
+ Alert.stubs(:create!)
21
+ @list.save
22
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
23
+ Alert.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
+ Alert.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
+ Alert.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
+ Alert.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
+ Alert.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
+ Alert.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
+ Alert.stubs(:create!).with(has_entry(:event_type, "comment_created"))
71
+ @comment.save
72
+ Alert.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/alerts'
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 => lambda { |person| person.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
+ Alert = 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,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alerts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ritvvij Parrikh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Easily build timelines, much like GitHub's news feed
15
+ email: james@giraffesoft.ca
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ files:
21
+ - .gitignore
22
+ - MIT-LICENSE
23
+ - README.rdoc
24
+ - Rakefile
25
+ - VERSION.yml
26
+ - generators/alerts/USAGE
27
+ - generators/alerts/templates/migration.rb
28
+ - generators/alerts/templates/model.rb
29
+ - generators/alerts/alerts_generator.rb
30
+ - init.rb
31
+ - lib/alerts.rb
32
+ - lib/alerts/fires.rb
33
+ - lib/alerts/macros.rb
34
+ - lib/alerts/matchers.rb
35
+ - shoulda_macros/alerts_shoulda.rb
36
+ - test/fires_test.rb
37
+ - test/test_helper.rb
38
+ - alerts.gemspec
39
+ homepage: http://github.com/ritvvijparrikh/alerts
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Easily build timelines, much like GitHub's news feed
64
+ test_files:
65
+ - test/fires_test.rb
66
+ - test/test_helper.rb