cia 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/gemfiles/rails2.gemfile.lock +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/lib/cia.rb +3 -2
- data/lib/cia/event.rb +3 -2
- data/lib/cia/version.rb +1 -1
- data/spec/cia/attribute_change_spec.rb +17 -0
- data/spec/cia_spec.rb +13 -5
- data/spec/spec_helper.rb +10 -0
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/lib/cia.rb
CHANGED
@@ -32,12 +32,13 @@ module CIA
|
|
32
32
|
|
33
33
|
return if not message and changes.empty? and action.to_s == "update"
|
34
34
|
|
35
|
-
event = CIA::Event.
|
35
|
+
event = CIA::Event.new(current_transaction.merge(
|
36
36
|
:action => action.to_s,
|
37
37
|
:source => source,
|
38
38
|
:message => message
|
39
39
|
))
|
40
|
-
event.
|
40
|
+
event.add_attribute_changes(changes)
|
41
|
+
event.save!
|
41
42
|
event
|
42
43
|
rescue Object => e
|
43
44
|
if exception_handler
|
data/lib/cia/event.rb
CHANGED
@@ -19,9 +19,10 @@ module CIA
|
|
19
19
|
end
|
20
20
|
|
21
21
|
# tested via transaction_test.rb
|
22
|
-
def
|
22
|
+
def add_attribute_changes(changes)
|
23
23
|
changes.each do |attribute_name, (old_value, new_value)|
|
24
|
-
attribute_changes.
|
24
|
+
attribute_changes.build(
|
25
|
+
:event => self,
|
25
26
|
:attribute_name => attribute_name,
|
26
27
|
:old_value => old_value,
|
27
28
|
:new_value => new_value,
|
data/lib/cia/version.rb
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CIA::AttributeChange do
|
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"
|
6
|
+
end
|
7
|
+
|
8
|
+
it "stores dates as db format" do
|
9
|
+
create_change(:old_value => Date.new(2012)).reload.old_value.should == "2012-01-01"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "stores booleans as db format" do
|
13
|
+
create_change(:old_value => false).reload.old_value.should == "f"
|
14
|
+
create_change(:old_value => true).reload.old_value.should == "t"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "stores nil as nil" do
|
18
|
+
create_change(:old_value => nil).reload.old_value.should == nil
|
19
|
+
end
|
20
|
+
|
4
21
|
it "delegates create_at to event" do
|
5
22
|
t = Time.now
|
6
23
|
event = CIA::Event.new(:created_at => t)
|
data/spec/cia_spec.rb
CHANGED
@@ -119,32 +119,40 @@ describe CIA do
|
|
119
119
|
it "records attribute creations" do
|
120
120
|
source = Car.create!
|
121
121
|
source.wheels = 4
|
122
|
-
event = CIA.record(:update, source)
|
122
|
+
event = CIA.record(:update, source).reload
|
123
123
|
|
124
124
|
parse_event_changes(event).should == [["wheels", nil, "4"]]
|
125
125
|
end
|
126
126
|
|
127
|
+
it "can act on attributes in before_save" do
|
128
|
+
x = nil
|
129
|
+
CIA.current_transaction[:hacked_before_save_action] = lambda{|event| x = event.attribute_changes.size }
|
130
|
+
source = Car.create!
|
131
|
+
source.wheels = 4
|
132
|
+
CIA.record(:update, source)
|
133
|
+
x.should == 1
|
134
|
+
end
|
135
|
+
|
127
136
|
it "records multiple attributes" do
|
128
137
|
source = CarWith3Attributes.create!
|
129
138
|
source.wheels = 4
|
130
139
|
source.drivers = 2
|
131
140
|
source.color = "red"
|
132
|
-
event = CIA.record(:update, source)
|
133
|
-
|
141
|
+
event = CIA.record(:update, source).reload
|
134
142
|
parse_event_changes(event).should =~ [["wheels", nil, "4"], ["drivers", nil, "2"], ["color", nil, "red"]]
|
135
143
|
end
|
136
144
|
|
137
145
|
it "records attribute changes" do
|
138
146
|
source = Car.create!(:wheels => 2)
|
139
147
|
source.wheels = 4
|
140
|
-
event = CIA.record(:update, source)
|
148
|
+
event = CIA.record(:update, source).reload
|
141
149
|
parse_event_changes(event).should == [["wheels", "2", "4"]]
|
142
150
|
end
|
143
151
|
|
144
152
|
it "records attribute deletions" do
|
145
153
|
source = Car.create!(:wheels => 2)
|
146
154
|
source.wheels = nil
|
147
|
-
event = CIA.record(:update, source)
|
155
|
+
event = CIA.record(:update, source).reload
|
148
156
|
parse_event_changes(event).should == [["wheels", "2", nil]]
|
149
157
|
end
|
150
158
|
|
data/spec/spec_helper.rb
CHANGED
@@ -68,3 +68,13 @@ def create_change(options={})
|
|
68
68
|
event = options.delete(:event) || create_event
|
69
69
|
CIA::AttributeChange.create!({:event => event, :source => event.source, :attribute_name => "bar"}.merge(options))
|
70
70
|
end
|
71
|
+
|
72
|
+
# simulate a hacked cia event
|
73
|
+
CIA::Event.class_eval do
|
74
|
+
before_save :hacked_before_save
|
75
|
+
attr_accessor :hacked_before_save_action
|
76
|
+
|
77
|
+
def hacked_before_save
|
78
|
+
hacked_before_save_action.call(self) if hacked_before_save_action
|
79
|
+
end
|
80
|
+
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.3.
|
4
|
+
version: 0.3.5
|
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-
|
12
|
+
date: 2012-09-11 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:
|
56
|
+
hash: 4110523684065790258
|
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:
|
65
|
+
hash: 4110523684065790258
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project:
|
68
68
|
rubygems_version: 1.8.24
|