Skip to content

Commit

Permalink
multitenancy support: add belongs_to :account to timeline event inc…
Browse files Browse the repository at this point in the history
…l. support for passing `:account` fires. tests are up to date as well.
  • Loading branch information
Christoph Schiessl committed Aug 29, 2011
1 parent b2f98a5 commit a86ce67
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions generators/timeline_fu/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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'
Expand Down
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
25 changes: 15 additions & 10 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 @@ -64,18 +66,21 @@ def test_should_fire_event_with_symbol_based_if_condition

def test_should_set_secondary_subject_to_self_when_requested
@list = List.new(hash_for_list(:author => @james))
TimelineEvent.expects(:create!).with(:actor => @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.expects(:create!).with(:actor => @mat,
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
31 changes: 27 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a86ce67

Please sign in to comment.