gbdev-acts_as_callback_logger 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -2,4 +2,6 @@ Licensing terms follow:
2
2
 
3
3
  This software is placed under the Creative Commons Attribution-Share ALike 3.0 Unported License
4
4
 
5
- http://creativecommons.org/licenses/by-sa/3.0/
5
+ http://creativecommons.org/licenses/by-sa/3.0/
6
+
7
+ MIT
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
- Rake::TestTask.new(:test) do |t|
10
- t.libs << 'lib'
11
- t.pattern = 'test/**/*_test.rb'
12
- t.verbose = true
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
@@ -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 = {:text => 'name', :aacl_model => 'AppLog'}
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
- descr_text = self.send(self.class.callback_logger_text_attr)
46
- msg = "Created #{class_name} (#{descr_text})"
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
- descr_text = self.send(self.class.callback_logger_text_attr)
59
- msg = "Updated #{class_name} (#{descr_text})"
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
- descr_text = self.send(self.class.callback_logger_text_attr)
71
- msg = "Deleted #{class_name} (#{descr_text})"
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 = 'Created program (Some Program)'
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 = 'Updated program (Dog Man)'
45
+ msg = "Updated program (Dog Man)"
47
46
  AppLog.count(:conditions => ["msg = ?", msg]).should == 1
48
47
 
49
- app_log = AppLog.find_by_msg('Updated program (Dog Man)')
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 = 'Created group type (Some Group Type)'
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 = 'Deleted group type (Some Group Type)'
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('Deleted group type (Some Group Type)')
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 = 'Created program (Some Program)'
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 = 'Created group type (Some Group Type)'
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 = 'Updated program (Dog Man)'
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 = 'Deleted group type (Some Group Type)'
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
@@ -12,6 +12,7 @@ ActiveRecord::Schema.define(:version => 1) do
12
12
  create_table :app_logs do |t|
13
13
  t.column :msg, :string, :null => false
14
14
  t.column :data, :text
15
+ t.column :model, :text
15
16
  t.column :restored_at, :datetime
16
17
  t.column :created_at, :datetime
17
18
  end
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 '../lib/acts_as_callback_logger'
8
- require '../lib/callback_logger'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gbdev-acts_as_callback_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wes Hays