cia 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cia (0.3.5)
4
+ cia (0.3.6)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -58,6 +58,9 @@ class User < ActiveRecord::Base
58
58
  ...
59
59
  end
60
60
  end
61
+
62
+ # adding an actor e.g. for user creation
63
+ CIA.current_actor = @user
61
64
  ```
62
65
 
63
66
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/cia
3
3
  specs:
4
- cia (0.3.4)
4
+ cia (0.3.5)
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.3.4)
4
+ cia (0.3.5)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/cia.rb CHANGED
@@ -22,10 +22,19 @@ module CIA
22
22
  Thread.current[:cia_transaction]
23
23
  end
24
24
 
25
+ def self.current_actor=(user)
26
+ current_transaction[:actor] = user if current_transaction
27
+ end
28
+
29
+ def self.current_actor
30
+ current_transaction[:actor] if current_transaction
31
+ end
32
+
25
33
  def self.record(action, source)
26
34
  return unless current_transaction
27
35
  options = source.class.audited_attribute_options
28
36
  return if options and options[:if] and not source.send(options[:if])
37
+ return if options and options[:unless] and source.send(options[:unless])
29
38
 
30
39
  changes = source.changes.slice(*source.class.audited_attributes)
31
40
  message = source.audit_message if source.respond_to?(:audit_message)
@@ -15,6 +15,7 @@ module CIA
15
15
  self.audited_attributes = Set.new unless audited_attributes
16
16
  self.audited_attributes += attributes.map(&:to_s)
17
17
 
18
+ raise "cannot have :if and :unless" if options[:if] && options[:unless]
18
19
  self.audited_attribute_options ||= {}
19
20
  self.audited_attribute_options.merge!(options)
20
21
 
@@ -1,3 +1,3 @@
1
1
  module CIA
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.6'
3
3
  end
@@ -2,7 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe CIA::AttributeChange do
4
4
  it "stores times as db format" do
5
- create_change(:old_value => Time.at(123456789)).reload.old_value.sub(/\.\d+$/,'').should == "1973-11-29 13:33:09"
5
+ t = Time.now
6
+ create_change(:old_value => t).reload.old_value.sub(/\.\d+$/,'').should == t.to_s(:db)
6
7
  end
7
8
 
8
9
  it "stores dates as db format" do
@@ -89,7 +89,7 @@ describe CIA do
89
89
 
90
90
  it "tracks if :if is true" do
91
91
  expect{
92
- object.bar = true
92
+ object.tested = true
93
93
  object.save!
94
94
  }.to change{ CIA::Event.count }.by(+1)
95
95
  CIA::Event.last.action.should == "create"
@@ -103,6 +103,25 @@ describe CIA do
103
103
  end
104
104
  end
105
105
 
106
+ context ":unless" do
107
+ let(:object) { CarWithUnless.new }
108
+
109
+ it "tracks if :unless is false" do
110
+ expect{
111
+ object.save!
112
+ }.to change{ CIA::Event.count }.by(+1)
113
+ CIA::Event.last.action.should == "create"
114
+ end
115
+
116
+ it "does not track if :unless is true" do
117
+ expect{
118
+ object.tested = true
119
+ object.save!
120
+ }.to_not change{ CIA::Event.count }
121
+ CIA::Event.last.should == nil
122
+ end
123
+ end
124
+
106
125
  context "events" do
107
126
  def parse_event_changes(event)
108
127
  event.attribute_changes.map { |c| [c.attribute_name, c.old_value, c.new_value] }
@@ -220,4 +239,36 @@ describe CIA do
220
239
  end
221
240
  end
222
241
  end
242
+
243
+ context ".current_actor" do
244
+ it "is nil when nothing is set" do
245
+ CIA.current_actor.should == nil
246
+ end
247
+
248
+ it "is nil when no actor is set" do
249
+ CIA.audit do
250
+ CIA.current_actor.should == nil
251
+ end
252
+ end
253
+
254
+ it "is the current :actor" do
255
+ CIA.audit :actor => 111 do
256
+ CIA.current_actor.should == 111
257
+ end
258
+ end
259
+ end
260
+
261
+ context ".current_actor=" do
262
+ it "does nothing if no transaction is running" do
263
+ CIA.current_actor = 111
264
+ CIA.current_transaction.should == nil
265
+ end
266
+
267
+ it "sets when transaction is started" do
268
+ CIA.audit :actor => 222 do
269
+ CIA.current_actor = 111
270
+ CIA.current_transaction.should == {:actor => 111}
271
+ end
272
+ end
273
+ end
223
274
  end
@@ -52,12 +52,15 @@ end
52
52
  class CarWithIf < ActiveRecord::Base
53
53
  self.table_name = "cars"
54
54
  include CIA::Auditable
55
- audit_attribute :wheels, :if => :foo?
56
- attr_accessor :bar
55
+ audit_attribute :wheels, :if => :tested
56
+ attr_accessor :tested
57
+ end
57
58
 
58
- def foo?
59
- bar
60
- end
59
+ class CarWithUnless < ActiveRecord::Base
60
+ self.table_name = "cars"
61
+ include CIA::Auditable
62
+ audit_attribute :wheels, :unless => :tested
63
+ attr_accessor :tested
61
64
  end
62
65
 
63
66
  def create_event(options={})
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.3.5
4
+ version: 0.3.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-11 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: michael@grosser.it
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  segments:
55
55
  - 0
56
- hash: 4110523684065790258
56
+ hash: -3737857467428511425
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  none: false
59
59
  requirements:
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  segments:
64
64
  - 0
65
- hash: 4110523684065790258
65
+ hash: -3737857467428511425
66
66
  requirements: []
67
67
  rubyforge_project:
68
68
  rubygems_version: 1.8.24