acts_as_eventable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ ActsAsEventable
2
+ ===============
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2010 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "acts_as_eventable"
9
+ gem.summary = %Q{Add a readable event history to your models}
10
+ gem.description = %Q{Add a readable event history to your models}
11
+ gem.email = "jim@jimvanfleet.com"
12
+ gem.homepage = "http://github.com/bigfleet/acts_as_eventable"
13
+ gem.authors = ["bigfleet"]
14
+ gem.add_development_dependency "ruby-sqlite", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ desc 'Default: run unit tests.'
23
+ task :default => :test
24
+
25
+ desc 'Test the acts_as_eventable plugin.'
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = true
31
+ end
32
+
33
+ desc 'Generate documentation for the acts_as_eventable plugin.'
34
+ Rake::RDocTask.new(:rdoc) do |rdoc|
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = 'ActsAsEventable'
37
+ rdoc.options << '--line-numbers' << '--inline-source'
38
+ rdoc.rdoc_files.include('README')
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,11 @@
1
+ class ActsAsEventableMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate'
5
+ end
6
+ end
7
+
8
+ def file_name
9
+ "acts_as_eventable_migration"
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ class ActsAsEventableMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :events do |t|
4
+ t.string :eventable_type, :null => false
5
+ t.integer :eventable_id, :null => false
6
+ t.string :field_name, :null => false
7
+ t.string :whodunnit
8
+ t.string :message
9
+ t.datetime :created_at
10
+ end
11
+ add_index :events, [:eventable_id, :eventable_type, :field_name]
12
+ end
13
+
14
+ def self.down
15
+ drop_table :events
16
+ end
17
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'eventable'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,32 @@
1
+ module ActsAsEventable
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ base.instance_eval{
5
+ cattr_accessor :eventable_options
6
+ }
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def acts_as_eventable(options)
12
+ instance_eval{
13
+ self.eventable_options = options
14
+ }
15
+ send :include, InstanceMethods
16
+ has_many :events, :as => :eventable, :dependent => :destroy, :order => "created_at desc"
17
+ after_save :record_events
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def record_events
23
+ active_keys = changes.keys.reject{ |key| %w{id created_at updated_at}.include?(key)}
24
+ active_keys.map do |key|
25
+ old_val, new_val = changes[key]
26
+ events.create(Event.attributes_from(self, key, old_val, new_val))
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ ActiveRecord::Base.send :include, ActsAsEventable
@@ -0,0 +1,23 @@
1
+ class Event < ActiveRecord::Base
2
+
3
+ def self.attributes_from(model, key, old_val, new_val)
4
+ eventable_options = model.class.eventable_options
5
+ msg = if eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][new_val]
6
+ eventable_options[:events][key.to_sym][new_val]
7
+ elsif eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][:message]
8
+ the_proc = eventable_options[:events][key.to_sym][:message]
9
+ if key.index("_id") # hackish, but this is a convention
10
+ reference = key[0..-4].to_sym
11
+ the_proc.call(model.send(reference).to_s)
12
+ else
13
+ the_proc.call(new_val)
14
+ end
15
+ elsif key.index("_id") # hackish, but this is a convention
16
+ reference = key[0..-4].to_sym
17
+ "#{key.titleize} changed to #{model.send(reference).to_s}"
18
+ else
19
+ "#{key.titleize} changed to #{new_val}"
20
+ end
21
+ {:field_name => key, :message => msg, :whodunnit => Eventable.whodunnit}
22
+ end
23
+ end
data/lib/eventable.rb ADDED
@@ -0,0 +1,29 @@
1
+ # ActsAsEventable
2
+ require 'acts_as_eventable/acts_as_eventable'
3
+ require 'acts_as_eventable/event'
4
+
5
+ module Eventable
6
+ @@whodunnit = nil
7
+
8
+ def self.included(base)
9
+ base.before_filter :set_whodunnit
10
+ end
11
+
12
+ def self.whodunnit
13
+ @@whodunnit.respond_to?(:call) ? @@whodunnit.call : @@whodunnit
14
+ end
15
+
16
+ def self.whodunnit=(value)
17
+ @@whodunnit = value
18
+ end
19
+
20
+ private
21
+
22
+ def set_whodunnit
23
+ @@whodunnit = lambda {
24
+ self.send :current_user rescue nil
25
+ }
26
+ end
27
+ end
28
+
29
+ ActionController::Base.send :include, Eventable
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_eventable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ApplicationController < ActionController::Base
4
+ def rescue_action(e)
5
+ raise e
6
+ end
7
+
8
+ # Returns id of hypothetical current user
9
+ def current_user
10
+ 153
11
+ end
12
+ end
13
+
14
+ class FoosController < ApplicationController
15
+ def create
16
+ @foo = Foo.create params[:foo]
17
+ head :ok
18
+ end
19
+
20
+ def update
21
+ @foo = Foo.find params[:id]
22
+ @foo.update_attributes params[:foo]
23
+ head :ok
24
+ end
25
+ end
26
+
27
+
28
+ class ActsAsEventableControllerTest < ActionController::TestCase #Test::Unit::TestCase
29
+ def setup
30
+ @controller = FoosController.new
31
+ @request = ActionController::TestRequest.new
32
+ @response = ActionController::TestResponse.new
33
+
34
+ ActionController::Routing::Routes.draw do |map|
35
+ map.resources :foos
36
+ end
37
+ end
38
+
39
+ test 'create' do
40
+ post :create, :foo => { :status => 'Flugel' }
41
+ foo = assigns(:foo)
42
+ assert_equal 1, foo.events.length
43
+ assert_equal 153, foo.events.last.whodunnit.to_i
44
+ end
45
+
46
+ test 'update' do
47
+ w = Foo.create :status => 'Duvel'
48
+ assert_equal 1, w.events.length
49
+ put :update, :id => w.id, :foo => { :status => 'Bugle' }
50
+ foo = assigns(:foo)
51
+ assert_equal 2, foo.events.length
52
+ assert_equal 153, foo.events.last.whodunnit.to_i
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,147 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsEventableTest < Test::Unit::TestCase
4
+
5
+ load_schema
6
+
7
+ class Foo < ActiveRecord::Base
8
+ acts_as_eventable :events =>{
9
+ :no_homers => {true => "Homers have been barred.", false => "Homers have been allowed."},
10
+ :custom_status => {:message => Proc.new {|n| "The value of a custom string field changed to #{n}" }},
11
+ :custom_bar_id => {:message => Proc.new{|n| "Active Bar set to #{n}"}}
12
+ }
13
+ belongs_to :bar, :class_name => "ActsAsEventableTest::Bar"
14
+ belongs_to :custom_bar, :class_name => "ActsAsEventableTest::Bar"
15
+ end
16
+
17
+ class Bar < ActiveRecord::Base
18
+ def to_s
19
+ name
20
+ end
21
+ end
22
+
23
+ def test_schema_has_loaded_correctly
24
+ Foo.create
25
+ Bar.create
26
+ assert Foo.count >= 1
27
+ assert Bar.count >= 1
28
+ end
29
+
30
+ def test_cattr_accessor_installed
31
+ assert_not_nil Foo.eventable_options
32
+ end
33
+
34
+ def test_desired_boolean_change_trigger
35
+ foo = Foo.create
36
+ foo.update_attribute(:no_homers, true)
37
+ assert_equal 1, foo.events.size
38
+ end
39
+
40
+ def test_desired_boolean_messaging
41
+ foo = Foo.create
42
+ foo.update_attribute(:no_homers, true)
43
+ assert_equal "Homers have been barred.", foo.events.first.message
44
+ end
45
+
46
+ def test_desired_boolean_messaging_stackable
47
+ foo = Foo.create
48
+ foo.update_attribute(:no_homers, true)
49
+ sleep 1 #mur
50
+ foo.update_attribute(:no_homers, false)
51
+ assert_equal "Homers have been allowed.", foo.events.first.message
52
+ assert_equal "Homers have been barred.", foo.events.last.message
53
+ end
54
+
55
+ def test_desired_string_change_trigger
56
+ foo = Foo.create
57
+ foo.update_attribute(:status, "New")
58
+ assert_equal 1, foo.events.size
59
+ end
60
+
61
+ def test_desired_string_messaging
62
+ foo = Foo.create
63
+ foo.update_attribute(:status, "New")
64
+ assert_equal "Status changed to New", foo.events.first.message
65
+ end
66
+
67
+ def test_desired_string_stackability
68
+ foo = Foo.create
69
+ foo.update_attribute(:status, "Old")
70
+ sleep 1 #mur
71
+ foo.update_attribute(:status, "New")
72
+ assert_equal "Status changed to New", foo.events.first.message
73
+ assert_equal "Status changed to Old", foo.events.last.message
74
+ end
75
+
76
+ def test_desired_custom_string_change_trigger
77
+ foo = Foo.create
78
+ foo.update_attribute(:custom_status, "New")
79
+ assert_equal 1, foo.events.size
80
+ end
81
+
82
+ def test_desired_custom_string_messaging
83
+ foo = Foo.create
84
+ foo.update_attribute(:custom_status, "New")
85
+ assert_equal "The value of a custom string field changed to New", foo.events.first.message
86
+ end
87
+
88
+ def test_desired_custom_string_stackability
89
+ foo = Foo.create
90
+ foo.update_attribute(:custom_status, "Old")
91
+ sleep 1 #mur
92
+ foo.update_attribute(:custom_status, "New")
93
+ assert_equal "The value of a custom string field changed to New", foo.events.first.message
94
+ assert_equal "The value of a custom string field changed to Old", foo.events.last.message
95
+ end
96
+
97
+ def test_desired_reference_change_trigger
98
+ foo = Foo.create
99
+ bar = Bar.create(:name => "Baloney")
100
+ foo.bar = bar; foo.save
101
+ assert_equal 1, foo.events.size
102
+ end
103
+
104
+ def test_desired_reference_messaging
105
+ foo = Foo.create
106
+ bar = Bar.create(:name => "Baloney")
107
+ foo.bar = bar; foo.save
108
+ assert_equal "Bar changed to Baloney", foo.events.first.message
109
+ end
110
+
111
+ def test_desired_reference_stackability
112
+ foo = Foo.create
113
+ bar1 = Bar.create(:name => "Peanut Butter")
114
+ bar2 = Bar.create(:name => "Jelly")
115
+ foo.bar = bar1; foo.save
116
+ sleep 1 #mur
117
+ foo.bar = bar2; foo.save
118
+ assert_equal "Bar changed to Jelly", foo.events.first.message
119
+ assert_equal "Bar changed to Peanut Butter", foo.events.last.message
120
+ end
121
+
122
+ def test_desired_custom_reference_change_trigger
123
+ foo = Foo.create
124
+ bar = Bar.create(:name => "Baloney")
125
+ foo.custom_bar = bar; foo.save
126
+ assert_equal 1, foo.events.size
127
+ end
128
+
129
+ def test_desired_custom_reference_messaging
130
+ foo = Foo.create
131
+ bar = Bar.create(:name => "Baloney")
132
+ foo.custom_bar = bar; foo.save
133
+ assert_equal "Active Bar set to Baloney", foo.events.first.message
134
+ end
135
+
136
+ def test_desired_custom_reference_stackability
137
+ foo = Foo.create
138
+ bar1 = Bar.create(:name => "Peanut Butter")
139
+ bar2 = Bar.create(:name => "Jelly")
140
+ foo.custom_bar = bar1; foo.save
141
+ sleep 1 #mur
142
+ foo.custom_bar = bar2; foo.save
143
+ assert_equal "Active Bar set to Jelly", foo.events.first.message
144
+ assert_equal "Active Bar set to Peanut Butter", foo.events.last.message
145
+ end
146
+
147
+ end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: vendor/plugins/acts_as_eventable/test/acts_as_eventable_plugin.sqlite.db
data/test/models.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Foo < ActiveRecord::Base
2
+ acts_as_eventable :events =>{
3
+ :no_homers => {true => "Homers have been barred.", false => "Homers have been allowed."},
4
+ :custom_status => {:message => Proc.new {|n| "The value of a custom string field changed to #{n}" }},
5
+ :custom_bar_id => {:message => Proc.new{|n| "Active Bar set to #{n}"}}
6
+ }
7
+ belongs_to :bar, :class_name => "Models::Bar"
8
+ belongs_to :custom_bar, :class_name => "Models::Bar"
9
+ end
10
+
11
+ class Bar < ActiveRecord::Base
12
+ def to_s
13
+ name
14
+ end
15
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,25 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :foos, :force => true do |t|
3
+ t.boolean :no_homers
4
+ t.string :status
5
+ t.string :custom_status
6
+ t.integer :bar_id
7
+ t.integer :custom_bar_id
8
+
9
+ t.timestamps
10
+ end
11
+ create_table :bars, :force => true do |t|
12
+ t.string :name
13
+
14
+ t.timestamps
15
+ end
16
+ # This is the active table
17
+ create_table :events, :force => true do |t|
18
+ t.string :eventable_type, :null => false
19
+ t.integer :eventable_id, :null => false
20
+ t.string :field_name, :null => false
21
+ t.string :whodunnit
22
+ t.string :message
23
+ t.datetime :created_at
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+
7
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
8
+ require 'models'
9
+
10
+ def load_schema
11
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
12
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
13
+
14
+ db_adapter = ENV['DB']
15
+
16
+ # no db passed, try one of these fine config-free DBs before bombing.
17
+ db_adapter ||=
18
+ begin
19
+ require 'rubygems'
20
+ require 'sqlite'
21
+ 'sqlite'
22
+ rescue MissingSourceFile
23
+ begin
24
+ require 'sqlite3'
25
+ 'sqlite3'
26
+ rescue MissingSourceFile
27
+ end
28
+ end
29
+
30
+ if db_adapter.nil?
31
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
32
+ end
33
+
34
+ ActiveRecord::Base.establish_connection(config[db_adapter])
35
+ load(File.dirname(__FILE__) + "/schema.rb")
36
+ require File.dirname(__FILE__) + '/../init.rb'
37
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_eventable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - bigfleet
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-23 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ruby-sqlite
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Add a readable event history to your models
33
+ email: jim@jimvanfleet.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README
40
+ files:
41
+ - MIT-LICENSE
42
+ - README
43
+ - Rakefile
44
+ - VERSION
45
+ - generators/acts_as_eventable_migration/acts_as_eventable_migration_generator.rb
46
+ - generators/acts_as_eventable_migration/templates/migration.rb
47
+ - init.rb
48
+ - install.rb
49
+ - lib/acts_as_eventable/acts_as_eventable.rb
50
+ - lib/acts_as_eventable/event.rb
51
+ - lib/eventable.rb
52
+ - tasks/acts_as_eventable_tasks.rake
53
+ - test/acts_as_eventable_controller_test.rb
54
+ - test/acts_as_eventable_test.rb
55
+ - test/database.yml
56
+ - test/models.rb
57
+ - test/schema.rb
58
+ - test/test_helper.rb
59
+ - uninstall.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/bigfleet/acts_as_eventable
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Add a readable event history to your models
90
+ test_files:
91
+ - test/acts_as_eventable_controller_test.rb
92
+ - test/acts_as_eventable_test.rb
93
+ - test/models.rb
94
+ - test/schema.rb
95
+ - test/test_helper.rb