gbdev-acts_as_callback_logger 0.1.3 → 0.2.0
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/LICENSE +3 -1
- data/Rakefile +14 -6
- data/lib/acts_as_callback_logger.rb +2 -1
- data/lib/callback_logger.rb +17 -17
- data/spec/acts_as_callback_logger_spec.rb +17 -14
- data/spec/schema.rb +1 -0
- data/spec/spec_helper.rb +2 -2
- metadata +1 -1
data/LICENSE
CHANGED
data/Rakefile
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
3
|
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
4
5
|
|
5
|
-
desc 'Default: run unit tests.'
|
6
|
-
task :default => :test
|
7
6
|
|
8
7
|
desc 'Test the acts_as_callback_logger plugin.'
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc 'Test the acts_as_callback_logger plugin.'
|
11
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
12
|
+
t.spec_files = FileList['spec/**/*.rb']
|
13
|
+
# t.fail_on_error = false
|
13
14
|
end
|
14
15
|
|
16
|
+
# Rake::TestTask.new(:test) do |t|
|
17
|
+
# t.libs << 'lib'
|
18
|
+
# t.pattern = 'test/**/*_test.rb'
|
19
|
+
# t.verbose = true
|
20
|
+
# end
|
21
|
+
|
15
22
|
desc 'Generate documentation for the acts_as_callback_logger plugin.'
|
16
23
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
24
|
rdoc.rdoc_dir = 'rdoc'
|
@@ -20,3 +27,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
20
27
|
rdoc.rdoc_files.include('README')
|
21
28
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
29
|
end
|
30
|
+
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'ruby-debug'
|
2
3
|
|
3
4
|
module GBDev #:nodoc:
|
4
5
|
module Acts #:nodoc:
|
@@ -87,7 +88,7 @@ module GBDev #:nodoc:
|
|
87
88
|
# 2 is the substring need for the model.
|
88
89
|
mod = self.msg.downcase.match(/^(deleted|updated)(.*)[(].*$/)[2]
|
89
90
|
mod = mod.titleize.gsub(' ','')
|
90
|
-
Inflector.camelize(mod).constantize
|
91
|
+
ActiveSupport::Inflector.camelize(mod).constantize
|
91
92
|
end
|
92
93
|
|
93
94
|
end # InstanceMethods
|
data/lib/callback_logger.rb
CHANGED
@@ -11,14 +11,14 @@ module GBDev #:nodoc:
|
|
11
11
|
attr_accessor :callback_logger_text_attr, :aacl_model
|
12
12
|
|
13
13
|
def log_callbacks(new_options = {})
|
14
|
-
options = {:
|
14
|
+
options = {:aacl_model => 'AppLog'}
|
15
15
|
options.merge!(new_options)
|
16
16
|
|
17
|
-
self.callback_logger_text_attr = options[:text]
|
18
|
-
|
19
17
|
mod = options[:aacl_model].titleize.gsub(' ','')
|
20
|
-
mod = Inflector.camelize(mod).constantize
|
18
|
+
mod = ActiveSupport::Inflector.camelize(mod).constantize
|
21
19
|
self.aacl_model = mod
|
20
|
+
|
21
|
+
self.callback_logger_text_attr = options[:text].to_sym if options.has_key?(:text)
|
22
22
|
|
23
23
|
extend GBDev::CallbackLogger::SingletonMethods
|
24
24
|
include GBDev::CallbackLogger::InstanceMethods
|
@@ -39,12 +39,10 @@ module GBDev #:nodoc:
|
|
39
39
|
|
40
40
|
module InstanceMethods
|
41
41
|
|
42
|
-
def log_create
|
43
|
-
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
42
|
+
def log_create
|
44
43
|
class_name = self.class.to_s.titleize.downcase
|
45
|
-
|
46
|
-
msg
|
47
|
-
self.class.aacl_model.create({:msg => msg})
|
44
|
+
msg = "Created #{class_name} (#{text_to_display})"
|
45
|
+
self.class.aacl_model.create({:msg => msg, :model => self.class.to_s})
|
48
46
|
rescue Exception => msg
|
49
47
|
# Just fail quietly
|
50
48
|
end
|
@@ -53,11 +51,9 @@ module GBDev #:nodoc:
|
|
53
51
|
data = self.changes.keys.collect{ |key| "#{key}||#{self.changes[key][0]}||#{self.changes[key][1]}" }.join('|::|')
|
54
52
|
data = "id||#{self.id}||#{self.id}|::|#{data}" unless data.empty?
|
55
53
|
|
56
|
-
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
57
54
|
class_name = self.class.to_s.titleize.downcase
|
58
|
-
|
59
|
-
msg
|
60
|
-
self.class.aacl_model.create({:msg => msg, :data => data}) unless data.empty?
|
55
|
+
msg = "Updated #{class_name} (#{text_to_display})"
|
56
|
+
self.class.aacl_model.create({:msg => msg, :data => data, :model => self.class.to_s}) unless data.empty?
|
61
57
|
rescue Exception => msg
|
62
58
|
# Just fail quietly
|
63
59
|
end
|
@@ -65,15 +61,19 @@ module GBDev #:nodoc:
|
|
65
61
|
def log_destroy
|
66
62
|
data = self.attributes.keys.collect{ |key| "#{key}||#{self.attributes[key]}" }.join('|::|')
|
67
63
|
|
68
|
-
self.class.callback_logger_text_attr = self.class.callback_logger_text_attr.to_sym
|
69
64
|
class_name = self.class.to_s.titleize.downcase
|
70
|
-
|
71
|
-
msg
|
72
|
-
self.class.aacl_model.create({:msg => msg, :data => data})
|
65
|
+
msg = "Deleted #{class_name} (#{text_to_display})"
|
66
|
+
self.class.aacl_model.create({:msg => msg, :data => data, :model => self.class.to_s})
|
73
67
|
rescue Exception => msg
|
74
68
|
# Just fail quietly
|
75
69
|
end
|
76
70
|
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def text_to_display
|
75
|
+
self.class.callback_logger_text_attr.nil? ? self.id : self.send(self.class.callback_logger_text_attr)
|
76
|
+
end
|
77
77
|
end # InstanceMethods
|
78
78
|
|
79
79
|
end # CallbackLogger
|
@@ -8,7 +8,7 @@ if Object.const_defined?(:ActiveRecord) and Object.const_defined?(:Mocha) and Ob
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class Program < ActiveRecord::Base
|
11
|
-
log_callbacks
|
11
|
+
log_callbacks({:text => 'name'})
|
12
12
|
end
|
13
13
|
|
14
14
|
class GroupType < ActiveRecord::Base
|
@@ -33,20 +33,19 @@ if Object.const_defined?(:ActiveRecord) and Object.const_defined?(:Mocha) and Ob
|
|
33
33
|
|
34
34
|
it 'should undo the program name change' do
|
35
35
|
@program.save.should be_true
|
36
|
-
|
37
36
|
program_id = @program.id
|
38
37
|
|
39
38
|
AppLog.count.should == 1
|
40
|
-
msg =
|
39
|
+
msg = "Created program (Some Program)"
|
41
40
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
42
41
|
|
43
42
|
@program.name = 'Dog Man'
|
44
43
|
@program.save.should be_true
|
45
44
|
AppLog.count.should == 2
|
46
|
-
msg =
|
45
|
+
msg = "Updated program (Dog Man)"
|
47
46
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
48
47
|
|
49
|
-
app_log = AppLog.find_by_msg(
|
48
|
+
app_log = AppLog.find_by_msg(msg)
|
50
49
|
app_log.undo.should be_true
|
51
50
|
app_log.restored_at.should_not be_nil
|
52
51
|
|
@@ -57,16 +56,18 @@ if Object.const_defined?(:ActiveRecord) and Object.const_defined?(:Mocha) and Ob
|
|
57
56
|
it 'should undo the group type deletion' do
|
58
57
|
@group_type.save.should be_true
|
59
58
|
|
59
|
+
group_type_id = @group_type.id
|
60
|
+
|
60
61
|
AppLog.count.should == 1
|
61
|
-
msg =
|
62
|
+
msg = "Created group type (#{group_type_id})"
|
62
63
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
63
64
|
|
64
65
|
@group_type.destroy
|
65
66
|
GroupType.count.should == 0
|
66
|
-
msg =
|
67
|
+
msg = "Deleted group type (#{group_type_id})"
|
67
68
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
68
69
|
|
69
|
-
app_log = AppLog.find_by_msg(
|
70
|
+
app_log = AppLog.find_by_msg("Deleted group type (#{group_type_id})")
|
70
71
|
app_log.undo.should be_true
|
71
72
|
app_log.restored_at.should_not be_nil
|
72
73
|
end
|
@@ -74,26 +75,28 @@ if Object.const_defined?(:ActiveRecord) and Object.const_defined?(:Mocha) and Ob
|
|
74
75
|
|
75
76
|
describe 'Models watched' do
|
76
77
|
it 'should add a created, updated and deleted entries to the app_logs table' do
|
77
|
-
@program.save.should be_true
|
78
|
+
@program.save.should be_true
|
79
|
+
program_id = @program.id
|
78
80
|
AppLog.count.should == 1
|
79
|
-
msg =
|
81
|
+
msg = "Created program (Some Program)"
|
80
82
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
81
83
|
|
82
84
|
@group_type.save.should be_true
|
85
|
+
group_type_id = @group_type.id
|
83
86
|
AppLog.count.should == 2
|
84
|
-
msg =
|
87
|
+
msg = "Created group type (#{group_type_id})"
|
85
88
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
86
89
|
|
87
90
|
@program.name = 'Dog Man'
|
88
91
|
@program.save.should be_true
|
89
92
|
AppLog.count.should == 3
|
90
|
-
msg =
|
93
|
+
msg = "Updated program (Dog Man)"
|
91
94
|
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
92
95
|
|
93
96
|
@group_type.destroy
|
94
97
|
AppLog.count.should == 4
|
95
|
-
msg =
|
96
|
-
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
98
|
+
msg = "Deleted group type (#{group_type_id})"
|
99
|
+
AppLog.count(:conditions => ["msg = ?", msg]).should == 1
|
97
100
|
end
|
98
101
|
end
|
99
102
|
|
data/spec/schema.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -4,8 +4,8 @@ require 'active_record'
|
|
4
4
|
require 'spec'
|
5
5
|
require 'mocha'
|
6
6
|
|
7
|
-
require '
|
8
|
-
require '
|
7
|
+
require File.dirname(__FILE__) + '/../lib/acts_as_callback_logger'
|
8
|
+
require File.dirname(__FILE__) + '/../lib/callback_logger'
|
9
9
|
|
10
10
|
include GBDev::Acts::CallbackLogger
|
11
11
|
include GBDev::CallbackLogger
|