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

Various fixes and improvements #6

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pkg
.DS_Store
.DS_Store
*.rbc
10 changes: 7 additions & 3 deletions generators/timeline_fu/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
class CreateTimelineEvents < ActiveRecord::Migration
def self.up
create_table :timeline_events do |t|
t.references :account
t.string :event_type, :subject_type, :actor_type, :secondary_subject_type
t.integer :subject_id, :actor_id, :secondary_subject_id
t.timestamps
end

add_index :timeline_events, :account_id
add_index :timeline_events, [:subject_id , :subject_type]
add_index :timeline_events, [:actor_id , :actor_type]
add_index :timeline_events, [:secondary_subject_id , :secondary_subject_type], :name => 'secondary_subject_timeline_events'
end

def self.down
drop_table :timeline_events
end
end


1 change: 1 addition & 0 deletions generators/timeline_fu/templates/model.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class TimelineEvent < ActiveRecord::Base
belongs_to :account
belongs_to :actor, :polymorphic => true
belongs_to :subject, :polymorphic => true
belongs_to :secondary_subject, :polymorphic => true
Expand Down
2 changes: 1 addition & 1 deletion lib/timeline_fu/fires.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def fires(event_type, opts)

method_name = :"fire_#{event_type}_after_#{opts[:on]}"
define_method(method_name) do
create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
create_options = [:account, :actor, :subject, :secondary_subject].inject({}) do |memo, sym|
if opts[sym]
if opts[sym].respond_to?(:call)
memo[sym] = opts[sym].call(self)
Expand Down
33 changes: 22 additions & 11 deletions test/fires_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

class FiresTest < Test::Unit::TestCase
def setup
@james = create_person(:email => 'james@giraffesoft.ca')
@mat = create_person(:email => 'mat@giraffesoft.ca')
@account = create_account
@james = create_person(:account => @account, :email => 'james@giraffesoft.ca')
@mat = create_person(:account => @account, :email => 'mat@giraffesoft.ca')
end

def test_should_fire_the_appropriate_callback
@list = List.new(hash_for_list(:author => @james));
TimelineEvent.expects(:create!).with(:actor => @james, :subject => @list, :event_type => 'list_created_or_updated')
TimelineEvent.expects(:create!).with(:account => @account, :actor => @james, :subject => @list, :event_type => 'list_created_or_updated')
@list.save
TimelineEvent.expects(:create!).with(:actor => @mat, :subject => @list, :event_type => 'list_created_or_updated')
TimelineEvent.expects(:create!).with(:account => @account, :actor => @mat, :subject => @list, :event_type => 'list_created_or_updated')
@list.author = @mat
@list.save
end
Expand All @@ -20,7 +21,8 @@ def test_should_fire_event_with_secondary_subject
TimelineEvent.stubs(:create!)
@list.save
@comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
TimelineEvent.expects(:create!).with(:actor => @mat,
TimelineEvent.expects(:create!).with(:account => @account,
:actor => @mat,
:subject => @comment,
:secondary_subject => @list,
:event_type => 'comment_created')
Expand All @@ -39,7 +41,7 @@ def test_exception_raised_if_on_missing
end

def test_should_only_fire_if_the_condition_evaluates_to_true
TimelineEvent.expects(:create!).with(:actor => @mat, :subject => @james, :event_type => 'follow_created')
TimelineEvent.expects(:create!).with(:account => @account, :actor => @mat, :subject => @james, :event_type => 'follow_created')
@james.new_watcher = @mat
@james.save
end
Expand All @@ -52,7 +54,7 @@ def test_should_not_fire_if_the_if_condition_evaluates_to_false

def test_should_fire_event_with_symbol_based_if_condition_that_is_true
@james.fire = true
TimelineEvent.expects(:create!).with(:subject => @james, :event_type => 'person_updated')
TimelineEvent.expects(:create!).with(:account => @account, :subject => @james, :event_type => 'person_updated')
@james.save
end

Expand All @@ -63,13 +65,22 @@ def test_should_fire_event_with_symbol_based_if_condition
end

def test_should_set_secondary_subject_to_self_when_requested
@list = List.new(hash_for_list(:author => @james));
TimelineEvent.stubs(:create!).with(has_entry(:event_type, "list_created_or_updated"))
@list = List.new(hash_for_list(:author => @james))
TimelineEvent.expects(:create!).with(:account => @account,
:actor => @james,
:subject => @list,
:secondary_subject => @list,
:event_type => 'list_created_or_updated')
@list.save
@comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
TimelineEvent.stubs(:create!).with(has_entry(:event_type, "comment_created"))
TimelineEvent.expects(:create!).with(:account => @account,
:actor => @mat,
:subject => @comment,
:secondary_subject => @comment,
:event_type => 'comment_created')
@comment.save
TimelineEvent.expects(:create!).with(:actor => @mat,
TimelineEvent.expects(:create!).with(:account => @account,
:actor => @mat,
:subject => @list,
:secondary_subject => @comment,
:event_type => 'comment_deleted')
Expand Down
33 changes: 28 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rubygems'
require 'activerecord'
require 'active_record'
require 'mocha'
require 'test/unit'
require 'logger'
Expand All @@ -13,7 +13,13 @@
ActiveRecord::Base.logger.level = Logger::WARN

ActiveRecord::Schema.define(:version => 0) do
create_table :accounts do |t|
t.string :name, :default => ""
t.string :description, :default => ""
end

create_table :people do |t|
t.references :account
t.string :email, :default => ''
t.string :password, :default => ''
end
Expand All @@ -29,9 +35,15 @@
end
end

class Account < ActiveRecord::Base
has_many :people
end

class Person < ActiveRecord::Base
attr_accessor :new_watcher, :fire


belongs_to :account

fires :follow_created, :on => :update,
:actor => lambda { |person| person.new_watcher },
:if => lambda { |person| !person.new_watcher.nil? }
Expand All @@ -47,18 +59,21 @@ class List < ActiveRecord::Base
belongs_to :author, :class_name => "Person"
has_many :comments

fires :list_created_or_updated, :actor => :author,
fires :list_created_or_updated, :account => Proc.new { |list| list.author.account_id },
:actor => :author,
:on => [:create, :update]
end

class Comment < ActiveRecord::Base
belongs_to :list
belongs_to :author, :class_name => "Person"

fires :comment_created, :actor => :author,
fires :comment_created, :account => Proc.new { |comment| comment.list.author.account_id },
:actor => :author,
:on => :create,
:secondary_subject => :list
fires :comment_deleted, :actor => :author,
fires :comment_deleted, :account => Proc.new { |comment| comment.list.author.account_id },
:actor => :author,
:on => :destroy,
:subject => :list,
:secondary_subject => :self
Expand All @@ -68,6 +83,14 @@ class Comment < ActiveRecord::Base

class Test::Unit::TestCase
protected
def hash_for_account(opts = {})
{:name => "fantasy inc.", :description => "rails shop"}.merge(opts)
end

def create_account(opts = {})
Account.create!(hash_for_account(opts))
end

def hash_for_list(opts = {})
{:title => 'whatever'}.merge(opts)
end
Expand Down
1 change: 0 additions & 1 deletion timeline_fu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Gem::Specification.new do |s|
"test/test_helper.rb",
"timeline_fu.gemspec"
]
s.has_rdoc = true
s.homepage = %q{http://github.com/giraffesoft/timeline_fu}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
Expand Down