gbdev-acts_as_callback_logger 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
- data/LICENSE +5 -0
- data/README +34 -0
- data/Rakefile +22 -0
- data/init.rb +7 -0
- data/install.rb +1 -0
- data/lib/acts_as_callback_logger.rb +93 -0
- data/lib/callback_logger.rb +68 -0
- data/spec/acts_as_callback_logger_spec.rb +104 -0
- data/spec/database.yml +7 -0
- data/spec/schema.rb +27 -0
- data/spec/spec_helper.rb +18 -0
- data/tasks/acts_as_callback_logger_tasks.rake +4 -0
- data/uninstall.rb +1 -0
- metadata +70 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
ActsAsCallbackLogger
|
2
|
+
===============
|
3
|
+
|
4
|
+
This plugin is extracted from several applications where we just wanted to log callback
|
5
|
+
events (after_create, after_update, after_destroy) for certain models tracking various
|
6
|
+
things that users did within the application.
|
7
|
+
|
8
|
+
1) To use the plugin, you must first have a table that you would like to use for logging.
|
9
|
+
That table must have the three columns "msg", "data" and "restored_at". An example would be:
|
10
|
+
|
11
|
+
create_table :app_logs do |t|
|
12
|
+
...
|
13
|
+
t.column :msg, :string, :null => false
|
14
|
+
t.column :data, :text
|
15
|
+
t.column :restored_at, :datetime
|
16
|
+
...
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
2) In the model for that table add "acts_as_callback_logger". For example:
|
21
|
+
|
22
|
+
class AppLog < ActiveRecord::Base
|
23
|
+
...
|
24
|
+
acts_as_callback_logger
|
25
|
+
...
|
26
|
+
end
|
27
|
+
|
28
|
+
3) In the model you want to log actions for add "log_callbacks". For example:
|
29
|
+
|
30
|
+
class Program < ActiveRecord::Base
|
31
|
+
...
|
32
|
+
log_callbacks
|
33
|
+
...
|
34
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the acts_as_callback_logger plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the acts_as_callback_logger plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActsAsCallbackLogger'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module GBDev #:nodoc:
|
4
|
+
module Acts #:nodoc:
|
5
|
+
module CallbackLogger #:nodoc:
|
6
|
+
|
7
|
+
def self.included(mod)
|
8
|
+
mod.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def acts_as_callback_logger(new_options = {})
|
14
|
+
options = {:text => 'name'}
|
15
|
+
options.merge!(new_options)
|
16
|
+
|
17
|
+
extend GBDev::Acts::CallbackLogger::SingletonMethods
|
18
|
+
include GBDev::Acts::CallbackLogger::InstanceMethods
|
19
|
+
end # acts_as_callback_logger
|
20
|
+
|
21
|
+
end # ClassMethods
|
22
|
+
|
23
|
+
module SingletonMethods
|
24
|
+
# def some_method
|
25
|
+
# end
|
26
|
+
end # SingletonMethods
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
def restore
|
30
|
+
restore_task = self.msg.split(' ')[0].downcase # Will be Created/Updated/Deleted/Restored
|
31
|
+
|
32
|
+
restore_status = case restore_task
|
33
|
+
#when 'created': restore_created
|
34
|
+
when 'updated': restore_updated
|
35
|
+
when 'deleted': restore_deleted
|
36
|
+
#when 'restored': restore_restored
|
37
|
+
else false
|
38
|
+
end
|
39
|
+
|
40
|
+
restore_status
|
41
|
+
end
|
42
|
+
alias :undo :restore
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def restore_created
|
47
|
+
# Do nothing for now
|
48
|
+
end
|
49
|
+
|
50
|
+
def restore_updated
|
51
|
+
mod = get_model_to_restore
|
52
|
+
hdata = {}
|
53
|
+
rdata = self.data.split('|::|')
|
54
|
+
rdata.each do |d|
|
55
|
+
d = d.split('||')
|
56
|
+
hdata[d[0].to_sym] = d[1]
|
57
|
+
end
|
58
|
+
|
59
|
+
obj_id = hdata.delete(:id).to_i
|
60
|
+
|
61
|
+
if mod.exists?(obj_id)
|
62
|
+
obj = mod.find(obj_id)
|
63
|
+
obj.update_attributes(hdata)
|
64
|
+
else
|
65
|
+
obj = mod.new(hdata)
|
66
|
+
end
|
67
|
+
retVal = obj.save
|
68
|
+
|
69
|
+
if retVal
|
70
|
+
self.restored_at = DateTime.now
|
71
|
+
self.save
|
72
|
+
end
|
73
|
+
|
74
|
+
retVal
|
75
|
+
end
|
76
|
+
alias :restore_deleted :restore_updated
|
77
|
+
|
78
|
+
def restore_restored
|
79
|
+
# Do nothing for now
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_model_to_restore
|
83
|
+
# 2 is the substring need for the model.
|
84
|
+
mod = self.msg.downcase.match(/^(deleted|updated)(.*)[(].*$/)[2]
|
85
|
+
mod = mod.titleize.gsub(' ','')
|
86
|
+
Inflector.camelize(mod).constantize
|
87
|
+
end
|
88
|
+
|
89
|
+
end # InstanceMethods
|
90
|
+
|
91
|
+
end # CallbackLogger
|
92
|
+
end # Acts
|
93
|
+
end # GBDev
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module GBDev #:nodoc:
|
4
|
+
module CallbackLogger #:nodoc:
|
5
|
+
|
6
|
+
def self.included(mod)
|
7
|
+
mod.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
attr_accessor :callback_logger_text_attr
|
12
|
+
|
13
|
+
def log_callbacks(new_options = {})
|
14
|
+
options = {:text => 'name'}
|
15
|
+
options.merge!(new_options)
|
16
|
+
|
17
|
+
self.callback_logger_text_attr = options[:text]
|
18
|
+
|
19
|
+
extend GBDev::CallbackLogger::SingletonMethods
|
20
|
+
include GBDev::CallbackLogger::InstanceMethods
|
21
|
+
end # log_callbacks
|
22
|
+
|
23
|
+
end # ClassMethods
|
24
|
+
|
25
|
+
module SingletonMethods
|
26
|
+
# def some_method
|
27
|
+
# end
|
28
|
+
end # SingletonMethods
|
29
|
+
|
30
|
+
module InstanceMethods
|
31
|
+
def after_create
|
32
|
+
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
33
|
+
class_name = self.class.to_s.titleize.downcase
|
34
|
+
descr_text = self.send(self.class.callback_logger_text_attr)
|
35
|
+
msg = "Created #{class_name} (#{descr_text})"
|
36
|
+
AppLog.create({:msg => msg})
|
37
|
+
rescue Exception => msg
|
38
|
+
# Just fail quietly
|
39
|
+
end
|
40
|
+
|
41
|
+
def after_update
|
42
|
+
data = self.changes.keys.collect{ |key| "#{key}||#{self.changes[key][0]}||#{self.changes[key][1]}" }.join('|::|')
|
43
|
+
data = "id||#{self.id}||#{self.id}|::|#{data}" unless data.empty?
|
44
|
+
|
45
|
+
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
46
|
+
class_name = self.class.to_s.titleize.downcase
|
47
|
+
descr_text = self.send(self.class.callback_logger_text_attr)
|
48
|
+
msg = "Updated #{class_name} (#{descr_text})"
|
49
|
+
AppLog.create({:msg => msg, :data => data}) unless data.empty?
|
50
|
+
rescue Exception => msg
|
51
|
+
# Just fail quietly
|
52
|
+
end
|
53
|
+
|
54
|
+
def after_destroy
|
55
|
+
data = self.attributes.keys.collect{ |key| "#{key}||#{self.attributes[key]}" }.join('|::|')
|
56
|
+
|
57
|
+
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
58
|
+
class_name = self.class.to_s.titleize.downcase
|
59
|
+
descr_text = self.send(self.class.callback_logger_text_attr)
|
60
|
+
msg = "Deleted #{class_name} (#{descr_text})"
|
61
|
+
AppLog.create({:msg => msg, :data => data})
|
62
|
+
rescue Exception => msg
|
63
|
+
# Just fail quietly
|
64
|
+
end
|
65
|
+
end # InstanceMethods
|
66
|
+
|
67
|
+
end # CallbackLogger
|
68
|
+
end # GBDev
|
@@ -0,0 +1,104 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
|
3
|
+
|
4
|
+
if Object.const_defined?(:ActiveRecord) and Object.const_defined?(:Mocha) and Object.const_defined?(:Spec)
|
5
|
+
|
6
|
+
class AppLog < ActiveRecord::Base
|
7
|
+
acts_as_callback_logger
|
8
|
+
end
|
9
|
+
|
10
|
+
class Program < ActiveRecord::Base
|
11
|
+
log_callbacks
|
12
|
+
end
|
13
|
+
|
14
|
+
class GroupType < ActiveRecord::Base
|
15
|
+
log_callbacks
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
describe 'CallbackLogger' do
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
AppLog.delete_all
|
23
|
+
Program.delete_all
|
24
|
+
GroupType.delete_all
|
25
|
+
@program = Program.new({:name => 'Some Program'})
|
26
|
+
@group_type = GroupType.new({:name => 'Some Group Type'})
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'AppLog' do
|
30
|
+
it 'should not have any logs in it to start' do
|
31
|
+
AppLog.count.should be_zero
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should undo the program name change' do
|
35
|
+
@program.save.should be_true
|
36
|
+
|
37
|
+
program_id = @program.id
|
38
|
+
|
39
|
+
AppLog.count.should == 1
|
40
|
+
msg = 'Created program (Some Program)'
|
41
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
42
|
+
|
43
|
+
@program.name = 'Dog Man'
|
44
|
+
@program.save.should be_true
|
45
|
+
AppLog.count.should == 2
|
46
|
+
msg = 'Updated program (Dog Man)'
|
47
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
48
|
+
|
49
|
+
app_log = AppLog.find_by_msg('Updated program (Dog Man)')
|
50
|
+
app_log.undo.should be_true
|
51
|
+
app_log.restored_at.should_not be_nil
|
52
|
+
|
53
|
+
program = Program.find(program_id)
|
54
|
+
program.name.should == 'Some Program'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should undo the group type deletion' do
|
58
|
+
@group_type.save.should be_true
|
59
|
+
|
60
|
+
AppLog.count.should == 1
|
61
|
+
msg = 'Created group type (Some Group Type)'
|
62
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
63
|
+
|
64
|
+
@group_type.destroy
|
65
|
+
GroupType.count.should == 0
|
66
|
+
msg = 'Deleted group type (Some Group Type)'
|
67
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
68
|
+
|
69
|
+
app_log = AppLog.find_by_msg('Deleted group type (Some Group Type)')
|
70
|
+
app_log.undo.should be_true
|
71
|
+
app_log.restored_at.should_not be_nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'Models watched' do
|
76
|
+
it 'should add a created, updated and deleted entries to the app_logs table' do
|
77
|
+
@program.save.should be_true
|
78
|
+
AppLog.count.should == 1
|
79
|
+
msg = 'Created program (Some Program)'
|
80
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
81
|
+
|
82
|
+
@group_type.save.should be_true
|
83
|
+
AppLog.count.should == 2
|
84
|
+
msg = 'Created group type (Some Group Type)'
|
85
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
86
|
+
|
87
|
+
@program.name = 'Dog Man'
|
88
|
+
@program.save.should be_true
|
89
|
+
AppLog.count.should == 3
|
90
|
+
msg = 'Updated program (Dog Man)'
|
91
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
92
|
+
|
93
|
+
@group_type.destroy
|
94
|
+
AppLog.count.should == 4
|
95
|
+
msg = 'Deleted group type (Some Group Type)'
|
96
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
else
|
103
|
+
$stderr.puts "Warning: ActiveRecord, Spec or Mocha not found -- skipping AAR tests"
|
104
|
+
end
|
data/spec/database.yml
ADDED
data/spec/schema.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
|
3
|
+
begin
|
4
|
+
execute 'DROP TABLE app_logs'
|
5
|
+
execute 'DROP TABLE programs'
|
6
|
+
execute 'DROP TABLE group_types'
|
7
|
+
rescue
|
8
|
+
# Do nothing because this is the first time this script has ran so
|
9
|
+
# the tables did not exist yet. Thus is why the error was thrown.
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :app_logs do |t|
|
13
|
+
t.column :msg, :string, :null => false
|
14
|
+
t.column :data, :text
|
15
|
+
t.column :restored_at, :datetime
|
16
|
+
t.column :created_at, :datetime
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :programs do |t|
|
20
|
+
t.column :name, :string, :limit => 255
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :group_types do |t|
|
24
|
+
t.column :name, :string, :limit => 255
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "active_record"
|
3
|
+
require 'spec'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
require '../lib/acts_as_callback_logger'
|
7
|
+
require '../lib/callback_logger'
|
8
|
+
|
9
|
+
include GBDev::Acts::CallbackLogger
|
10
|
+
include GBDev::CallbackLogger
|
11
|
+
|
12
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
14
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'postgresql'])
|
15
|
+
|
16
|
+
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
|
17
|
+
|
18
|
+
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gbdev-acts_as_callback_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Hays
|
8
|
+
- John Dell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-08-23 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Rails gem/plugin to log callback events (after_create, after_update, after_destroy) for certain models tracking various things that users did within the application. SEE README FOR MORE INFO.
|
18
|
+
email: gems@gbdev.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- init.rb
|
28
|
+
- install.rb
|
29
|
+
- LICENSE
|
30
|
+
- Rakefile
|
31
|
+
- README
|
32
|
+
- uninstall.rb
|
33
|
+
- lib/acts_as_callback_logger.rb
|
34
|
+
- lib/callback_logger.rb
|
35
|
+
- spec/acts_as_callback_logger_spec.rb
|
36
|
+
- spec/database.yml
|
37
|
+
- spec/schema.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- tasks/acts_as_callback_logger_tasks.rake
|
40
|
+
has_rdoc: false
|
41
|
+
homepage: http://github.com/gbdev/acts_as_callback_logger
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.2.0
|
63
|
+
signing_key:
|
64
|
+
specification_version: 2
|
65
|
+
summary: Rails gem/plugin to log callback events (after_create, after_update, after_destroy) for certain models tracking various things that users did within the application.
|
66
|
+
test_files:
|
67
|
+
- spec/acts_as_callback_logger_spec.rb
|
68
|
+
- spec/database.yml
|
69
|
+
- spec/schema.rb
|
70
|
+
- spec/spec_helper.rb
|