Permalink
Browse files

import extracted timeline_fu plugin

  • Loading branch information...
0 parents commit bedd9bba697803f2079daa9640f6b23df7209b0b @jamesgolick committed Sep 30, 2008
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+TimelineFu
+==========
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2008 [name of plugin creator], released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the timeline_fu plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the timeline_fu plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'TimelineFu'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+require 'timeline_fu'
@@ -0,0 +1,7 @@
+require 'timeline_fu/acts_as_event_target'
+require 'timeline_fu/fires'
+
+module TimelineFu
+end
+
+ActiveRecord::Base.send(:include, TimelineFu::ActsAsEventTarget, TimelineFu::Fires)
@@ -0,0 +1,13 @@
+module TimelineFu
+ module ActsAsEventTarget
+ def self.included(klass)
+ klass.send(:extend, ClassMethods)
+ end
+
+ module ClassMethods
+ def acts_as_event_target
+ has_many :timeline_events, :as => :target
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module TimelineFu
+ module Fires
+ def self.included(klass)
+ klass.send(:extend, ClassMethods)
+ end
+
+ module ClassMethods
+ def fires(event_type, opts)
+ method_name = :"fire_#{event_type}_after_#{opts[:on]}"
+ define_method(method_name) do
+ create_options = [:target, :secondary_target, :actor].inject({}) do |memo, sym|
+ memo[sym] = opts[sym] == :self ? self : send(opts[sym]) if opts[sym]
+ memo
+ end
+ create_options[:event_type] = event_type
+
+ TimelineEvent.create!(create_options)
+ end
+
+ send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
+ end
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :timeline_fu do
+# # Task goes here
+# end
@@ -0,0 +1,9 @@
+require File.dirname(__FILE__)+'/test_helper'
+
+class ActsAsEventTargetTest < Test::Unit::TestCase
+ def test_should_have_many_timeline_events
+ reflection = Person.reflect_on_association(:timeline_events)
+ assert reflection
+ assert_equal :has_many, reflection.macro
+ end
+end
@@ -0,0 +1,38 @@
+require File.dirname(__FILE__)+'/test_helper'
+
+class FiresTest < Test::Unit::TestCase
+ def setup
+ @james = create_person(:email => 'james@giraffesoft.ca')
+ @mat = create_person(: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, :target => @list, :secondary_target => @list, :event_type => :list_created)
+ @list.save
+ end
+
+ def test_should_only_fire_if_the_condition_evaluates_to_true
+ TimelineEvent.expects(:create!).with(:actor => @mat, :target => @james, :event_type => :follow_created)
+ @james.new_watcher = @mat
+ @james.save
+ end
+
+ def test_should_not_fire_if_the_if_condition_evaluates_to_false
+ TimelineEvent.expects(:create!).with(:actor => @mat, :target => @james, :event_type => :follow_created).times(0)
+ @james.new_watcher = nil
+ @james.save
+ end
+
+ def test_should_fire_event_with_symbol_based_if_condition_that_is_true
+ @james.fire = true
+ TimelineEvent.expects(:create!).with(:actor => @james, :target => @james, :event_type => :person_updated)
+ @james.save
+ end
+
+ def test_should_fire_event_with_symbol_based_if_condition
+ @james.fire = false
+ TimelineEvent.expects(:create!).with(:actor => @james, :target => @james, :event_type => :person_updated).times(0)
+ @james.save
+ end
+end
@@ -0,0 +1,64 @@
+require 'rubygems'
+require 'activerecord'
+require 'mocha'
+require 'test/unit'
+require 'logger'
+
+require File.dirname(__FILE__)+'/../lib/timeline_fu'
+
+ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
+ActiveRecord::Base.establish_connection('sqlite3')
+
+ActiveRecord::Base.logger = Logger.new(STDERR)
+ActiveRecord::Base.logger.level = Logger::WARN
+
+ActiveRecord::Schema.define(:version => 0) do
+ create_table :people do |t|
+ t.string :email, :default => ''
+ t.string :password, :default => ''
+ end
+
+ create_table :lists do |t|
+ t.integer :author_id
+ t.string :title
+ end
+end
+
+class Person < ActiveRecord::Base
+ acts_as_event_target
+ attr_accessor :new_watcher, :fire
+
+ fires :follow_created, :on => :update, :actor => :new_watcher, :target => :self, :if => lambda { |person| !person.new_watcher.nil? }
+ fires :person_updated, :on => :update, :actor => :self, :target => :self, :if => :fire?
+
+ def fire?
+ new_watcher.nil? && fire
+ end
+end
+
+class List < ActiveRecord::Base
+ belongs_to :author, :class_name => "Person"
+
+ fires :list_created, :actor => :author, :secondary_target => :self, :target => :self, :on => :create
+end
+
+TimelineEvent = Class.new
+
+class Test::Unit::TestCase
+ protected
+ def hash_for_list(opts = {})
+ {:title => 'whatever'}.merge(opts)
+ end
+
+ def create_list(opts = {})
+ List.create!(hash_for_list(opts))
+ end
+
+ def hash_for_person(opts = {})
+ {:email => 'james'}.merge(opts)
+ end
+
+ def create_person(opts = {})
+ Person.create!(hash_for_person(opts))
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit bedd9bb

Please sign in to comment.