cia 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cia (0.2.0)
4
+ cia (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -49,6 +49,9 @@ end
49
49
  User.last.cia_events
50
50
  changes = User.last.cia_attribute_changes
51
51
  last_passwords = changes.where(:attribute_name => "crypted_password").map(&:new_value)
52
+
53
+ # exceptions (raised by default)
54
+ CIA.exception_handler = lambda{|e| raise e unless Rails.env.production? }
52
55
  ```
53
56
 
54
57
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/cia
3
3
  specs:
4
- cia (0.1.1)
4
+ cia (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/cia
3
3
  specs:
4
- cia (0.1.1)
4
+ cia (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/cia.rb CHANGED
@@ -10,6 +10,10 @@ require 'cia/delete_event'
10
10
  require 'cia/attribute_change'
11
11
 
12
12
  module CIA
13
+ class << self
14
+ attr_accessor :exception_handler
15
+ end
16
+
13
17
  def self.audit(options = {})
14
18
  Thread.current[:cia_transaction] = Transaction.new(options)
15
19
  yield
@@ -24,7 +28,10 @@ module CIA
24
28
  def self.record_audit(event_type, object)
25
29
  CIA.current_transaction.record(event_type, object)
26
30
  rescue Object => e
27
- Rails.logger.error("Failed to record audit: #{e}\n#{e.backtrace}")
28
- raise e unless Rails.env.production?
31
+ if exception_handler
32
+ exception_handler.call e
33
+ else
34
+ raise e
35
+ end
29
36
  end
30
37
  end
data/lib/cia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CIA
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/spec/cia_spec.rb CHANGED
@@ -49,11 +49,6 @@ describe CIA do
49
49
  end
50
50
  end
51
51
 
52
- before do
53
- Rails.stub(:logger).and_return(mock(:error => ""))
54
- Rails.env.stub(:production?).and_return(true)
55
- end
56
-
57
52
  it "tracks create" do
58
53
  expect{
59
54
  object.save!
@@ -77,29 +72,39 @@ describe CIA do
77
72
  CIA::Event.last.class.should == CIA::UpdateEvent
78
73
  end
79
74
 
80
- context "exceptions" do
75
+ context "exception_handler" do
81
76
  before do
82
- Rails.stub(:logger).and_return(mock(:error => ""))
83
- Rails.env.stub(:production?).and_return(true)
77
+ $stderr.stub(:puts)
84
78
  transaction.stub(:record).and_raise(StandardError.new("foo"))
85
79
  end
86
80
 
87
- it "logs exceptions raised by the transaction" do
88
- Rails.logger.should_receive(:error).with { |x| x =~ /Failed to record audit: foo/ }
89
- object.save! rescue nil
81
+ def capture_exception
82
+ begin
83
+ old = CIA.exception_handler
84
+ ex = nil
85
+ CIA.exception_handler = lambda{|e| ex = e }
86
+ yield
87
+ ex
88
+ rescue
89
+ CIA.exception_handler = old
90
+ end
90
91
  end
91
92
 
92
- it "re-raise exceptions when not in production" do
93
- Rails.env.stub(:production?).and_return(false)
94
-
95
- expect {
93
+ it "raises exceptions by the transaction" do
94
+ ex = nil
95
+ begin
96
96
  object.save!
97
- }.to raise_error(StandardError)
97
+ rescue Object => e
98
+ ex = e
99
+ end
100
+ ex.inspect.should == '#<StandardError: foo>'
98
101
  end
99
102
 
100
- it "does not re-raise exceptions when in production" do
101
- Rails.env.stub(:production?).and_return(true)
102
- object.save!
103
+ it "can capture exception via handler" do
104
+ ex = capture_exception do
105
+ object.save!
106
+ end
107
+ ex.inspect.should == '#<StandardError: foo>'
103
108
  end
104
109
  end
105
110
  end
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,7 @@ ActiveRecord::Base.establish_connection(
7
7
  )
8
8
 
9
9
  ActiveRecord::Schema.define(:version => 1) do
10
- eval(File.read('MIGRATION.rb'))
10
+ eval(File.read(File.expand_path('../../MIGRATION.rb', __FILE__)))
11
11
 
12
12
  create_table :cars do |t|
13
13
  t.integer :wheels
@@ -59,13 +59,3 @@ def create_change
59
59
  event = create_event
60
60
  CIA::AttributeChange.create!(:event => event, :source => event.source, :attribute_name => "bar")
61
61
  end
62
-
63
- module Rails
64
- def self.logger
65
- raise "NOT STUBBED"
66
- end
67
-
68
- def self.env
69
- @@env ||= "NOT STUBBED"
70
- end
71
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  segments:
61
61
  - 0
62
- hash: 331448571299140438
62
+ hash: 562244213003532092
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  segments:
70
70
  - 0
71
- hash: 331448571299140438
71
+ hash: 562244213003532092
72
72
  requirements: []
73
73
  rubyforge_project:
74
74
  rubygems_version: 1.8.24