Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for :unless, passing attributes, parametrizing :event_class_name, and cleanup #11

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
Gemfile.lock
gemfiles/*.gemfile.lock
pkg
.DS_Store
11 changes: 11 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
appraise "activerecord23" do
gem "activerecord", "~> 2.3.14"
end

appraise "activerecord30" do
gem "activerecord", "~> 3.0.10"
end

appraise "activerecord31" do
gem "activerecord", "~> 3.1.1"
end
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ The rest all fit neatly in an options hash.
- :subject is automatically set to self, which is good most of the time. You can however override it if you need to, using :subject.
- :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.
- :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.
- :event_class_name => string specifying the event class name (or an array of them) you'd like you use. Defaults to "TimelineEvent".
- If you specify more than one event class name, an object for each will be created in the appropriate callbacks.
- any other parameter is treated as TimelineEvent's attribute
- This is useful if you add a custom field to timeline_events table

Here's another example:

Expand Down
22 changes: 6 additions & 16 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'bundler/gem_tasks'
require 'appraisal'

desc 'Default: run unit tests.'
task :default => :test
desc 'Default: run unit tests against all supported versions of ActiveRecord'
task :default => ["appraisal:install"] do |t|
exec("rake appraisal test")
end

desc 'Test the timeline_fu plugin.'
Rake::TestTask.new(:test) do |t|
Expand All @@ -12,20 +16,6 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end

begin
require 'jeweler'
Jeweler::Tasks.new do |s|
s.name = "timeline_fu"
s.summary = %Q{Easily build timelines, much like GitHub's news feed}
s.email = "james@giraffesoft.ca"
s.homepage = "http://github.com/giraffesoft/timeline_fu"
s.description = "Easily build timelines, much like GitHub's news feed"
s.authors = ["James Golick", "Mathieu Martin", "Francois Beausoleil"]
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end

desc 'Generate documentation for the timeline_fu plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
Expand Down
4 changes: 0 additions & 4 deletions VERSION.yml

This file was deleted.

7 changes: 7 additions & 0 deletions gemfiles/activerecord23.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 2.3.14"

gemspec :path=>"../"
7 changes: 7 additions & 0 deletions gemfiles/activerecord30.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 3.0.10"

gemspec :path=>"../"
7 changes: 7 additions & 0 deletions gemfiles/activerecord31.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "activerecord", "~> 3.1.1"

gemspec :path=>"../"
7 changes: 4 additions & 3 deletions lib/timeline_fu.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'timeline_fu/fires'
require "active_record"

module TimelineFu
module TimelineFu
autoload :Fires, "timeline_fu/fires"
end

ActiveRecord::Base.send :include, TimelineFu::Fires
ActiveRecord::Base.send(:include, TimelineFu::Fires)
16 changes: 12 additions & 4 deletions lib/timeline_fu/fires.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ def fires(event_type, opts)

opts[:subject] = :self unless opts.has_key?(:subject)

method_name = :"fire_#{event_type}_after_#{opts[:on]}"
on = opts.delete(:on)
_if = opts.delete(:if)
_unless = opts.delete(:unless)

event_class_names = Array(opts.delete(:event_class_name) || "TimelineEvent")

method_name = :"fire_#{event_type}_after_#{on}"
define_method(method_name) do
create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
create_options = opts.keys.inject({}) do |memo, sym|
if opts[sym]
if opts[sym].respond_to?(:call)
memo[sym] = opts[sym].call(self)
Expand All @@ -32,10 +38,12 @@ def fires(event_type, opts)
end
create_options[:event_type] = event_type.to_s

TimelineEvent.create!(create_options)
event_class_names.each do |class_name|
class_name.classify.constantize.create!(create_options)
end
end

send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
send(:"after_#{on}", method_name, :if => _if, :unless => _unless)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/timeline_fu/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module TimelineFu
VERSION = "0.4.4"
end
21 changes: 21 additions & 0 deletions test/fires_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,25 @@ def test_should_set_secondary_subject_to_self_when_requested
:event_type => 'comment_deleted')
@comment.destroy
end

def test_should_set_additional_attributes_when_present
@site = Site.create(:name => 'foo.com')
@article = Article.new(:body => 'cool article!', :author => @james, :site => @site)
TimelineEvent.expects(:create!).with(:actor => @james, :subject => @article, :event_type => 'article_created', :site => @site)
@article.save
end

def test_should_use_specified_event_class_when_present
@company = Company.new(:owner => @james, :name => 'A great company!')
CompanyEvent.expects(:create!).with(:actor => @james, :subject => @company, :event_type => 'company_created')
@company.save
end

def test_should_support_specifying_multiple_event_classes
CompanyEvent.stubs(:create!)
@company = Company.create(:owner => @james, :name => 'A great company!')
CompanyEvent.expects(:create!).with(:actor => @james, :subject => @company, :event_type => 'company_updated')
IRSEvent.expects(:create!).with(:actor => @james, :subject => @company, :event_type => 'company_updated')
@company.save
end
end
46 changes: 43 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rubygems'
require 'activerecord'
require 'mocha'
require 'test/unit'
require 'mocha'
require 'logger'

require File.dirname(__FILE__)+'/../lib/timeline_fu'
$:.push File.expand_path("../lib", __FILE__)
require "timeline_fu"

ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection('sqlite3')
Expand All @@ -27,6 +27,20 @@
t.integer :list_id, :author_id
t.string :body
end

create_table :sites do |t|
t.string :name
end

create_table :articles do |t|
t.integer :site_id
t.string :body
end

create_table :companies do |t|
t.integer :owner_id
t.string :name
end
end

class Person < ActiveRecord::Base
Expand Down Expand Up @@ -64,6 +78,32 @@ class Comment < ActiveRecord::Base
:secondary_subject => :self
end

class Site < ActiveRecord::Base
end

class Article < ActiveRecord::Base
belongs_to :author, :class_name => "Person"
belongs_to :site

fires :article_created, :actor => :author,
:on => :create,
:site => :site
end

class Company < ActiveRecord::Base
belongs_to :owner, :class_name => "Person"

fires :company_created, :actor => :owner,
:on => :create,
:event_class_name => "CompanyEvent"

fires :company_updated, :actor => :owner,
:on => :update,
:event_class_name => ["CompanyEvent", "IRSEvent"]
end

IRSEvent = Class.new
CompanyEvent = Class.new
TimelineEvent = Class.new

class Test::Unit::TestCase
Expand Down
66 changes: 20 additions & 46 deletions timeline_fu.gemspec
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "timeline_fu/version"

Gem::Specification.new do |s|
s.name = %q{timeline_fu}
s.version = "0.4.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.name = "timeline_fu"
s.version = TimelineFu::VERSION
s.authors = ["James Golick", "Mathieu Martin", "Francois Beausoleil"]
s.date = %q{2011-06-23}
s.description = %q{Easily build timelines, much like GitHub's news feed}
s.email = %q{james@giraffesoft.ca}
s.extra_rdoc_files = [
"README.rdoc"
]
s.files = [
".gitignore",
"MIT-LICENSE",
"README.rdoc",
"Rakefile",
"VERSION.yml",
"generators/timeline_fu/USAGE",
"generators/timeline_fu/templates/migration.rb",
"generators/timeline_fu/templates/model.rb",
"generators/timeline_fu/timeline_fu_generator.rb",
"init.rb",
"lib/timeline_fu.rb",
"lib/timeline_fu/fires.rb",
"lib/timeline_fu/macros.rb",
"lib/timeline_fu/matchers.rb",
"shoulda_macros/timeline_fu_shoulda.rb",
"test/fires_test.rb",
"test/test_helper.rb",
"timeline_fu.gemspec"
]
s.email = "james@giraffesoft.ca"
s.homepage = "https://github.com/jamesgolick/timeline_fu"
s.summary = "Easily build timelines, much like GitHub's news feed"
s.description = "Easily build timelines, much like GitHub's news feed"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.has_rdoc = true
s.homepage = %q{http://github.com/giraffesoft/timeline_fu}
s.extra_rdoc_files = ["README.rdoc"]
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
s.summary = %q{Easily build timelines, much like GitHub's news feed}
s.test_files = [
"test/fires_test.rb",
"test/test_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2
s.add_runtime_dependency "activerecord"

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
s.add_development_dependency "appraisal"
s.add_development_dependency "logger"
s.add_development_dependency "mocha"
s.add_development_dependency "sqlite3"
end