trackable 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/trackable/acts_as_trackable.rb +2 -1
- data/lib/trackable/event.rb +4 -2
- data/test/schema.rb +2 -1
- data/test/trackable_test.rb +33 -32
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -23,7 +23,8 @@ module ActsAsTrackable
|
|
23
23
|
active_keys = changes.keys.reject{ |key| %w{id created_at updated_at}.include?(key)}
|
24
24
|
active_keys.map do |key|
|
25
25
|
old_val, new_val = changes[key]
|
26
|
-
|
26
|
+
attrs = Event.attributes_from(self, key, old_val, new_val)
|
27
|
+
events.create(attrs) if attrs
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
data/lib/trackable/event.rb
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
class Event < ActiveRecord::Base
|
2
2
|
|
3
3
|
def self.attributes_from(model, key, old_val, new_val)
|
4
|
+
return if old_val.blank? && new_val.blank? #not something we need to track.
|
4
5
|
eventable_options = model.class.eventable_options
|
5
6
|
msg = if eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][new_val]
|
6
7
|
eventable_options[:events][key.to_sym][new_val]
|
7
8
|
elsif eventable_options[:events][key.to_sym] && eventable_options[:events][key.to_sym][:message]
|
8
9
|
the_proc = eventable_options[:events][key.to_sym][:message]
|
9
|
-
if key.
|
10
|
+
if key.ends_with?("_id") # hackish, but this is a convention
|
10
11
|
reference = key[0..-4].to_sym
|
11
12
|
the_proc.call(model.send(reference).to_s)
|
12
13
|
else
|
13
14
|
the_proc.call(new_val)
|
14
15
|
end
|
15
|
-
elsif key.
|
16
|
+
elsif key.ends_with?("_id") # hackish, but this is a convention
|
16
17
|
reference = key[0..-4].to_sym
|
17
18
|
"#{key.humanize} changed to #{model.send(reference).to_s}"
|
18
19
|
else
|
@@ -20,4 +21,5 @@ class Event < ActiveRecord::Base
|
|
20
21
|
end
|
21
22
|
{:field_name => key, :message => msg, :whodunnit => Trackable.whodunnit}
|
22
23
|
end
|
24
|
+
|
23
25
|
end
|
data/test/schema.rb
CHANGED
@@ -2,7 +2,8 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
2
2
|
create_table :foos, :force => true do |t|
|
3
3
|
t.boolean :no_homers
|
4
4
|
t.string :status
|
5
|
-
t.string :custom_status
|
5
|
+
t.string :custom_status
|
6
|
+
t.string :alternate_identification
|
6
7
|
t.integer :bar_id
|
7
8
|
t.integer :custom_bar_id
|
8
9
|
|
data/test/trackable_test.rb
CHANGED
@@ -48,6 +48,32 @@ class TrackableTest < Test::Unit::TestCase
|
|
48
48
|
assert_equal "Status changed to New", foo.events.first.message
|
49
49
|
end
|
50
50
|
|
51
|
+
def test_desired_nil_string_messaging
|
52
|
+
foo = Foo.create
|
53
|
+
foo.update_attribute(:status, nil)
|
54
|
+
assert_equal 0, foo.events.size
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_desired_new_blank_string_messaging
|
58
|
+
foo = Foo.new(:status => "")
|
59
|
+
foo.save
|
60
|
+
assert_equal 0, foo.events.size
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_desired_existing_nil_string_messaging
|
64
|
+
foo = Foo.create(:status => "bazzed")
|
65
|
+
assert_equal 1, foo.events.size
|
66
|
+
foo.update_attribute(:status, nil)
|
67
|
+
assert_equal 2, foo.events.size
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_desired_existing_nil_string_messaging
|
71
|
+
foo = Foo.create(:status => "bazzed")
|
72
|
+
assert_equal 1, foo.events.size
|
73
|
+
foo.update_attribute(:status, nil)
|
74
|
+
assert_equal 2, foo.events.size
|
75
|
+
end
|
76
|
+
|
51
77
|
def test_desired_string_stackability
|
52
78
|
foo = Foo.create
|
53
79
|
foo.update_attribute(:status, "Old")
|
@@ -68,16 +94,7 @@ class TrackableTest < Test::Unit::TestCase
|
|
68
94
|
foo.update_attribute(:custom_status, "New")
|
69
95
|
assert_equal "The value of a custom string field changed to New", foo.events.first.message
|
70
96
|
end
|
71
|
-
|
72
|
-
def test_desired_custom_string_stackability
|
73
|
-
foo = Foo.create
|
74
|
-
foo.update_attribute(:custom_status, "Old")
|
75
|
-
sleep 1 #mur
|
76
|
-
foo.update_attribute(:custom_status, "New")
|
77
|
-
assert_equal "The value of a custom string field changed to New", foo.events.first.message
|
78
|
-
assert_equal "The value of a custom string field changed to Old", foo.events.last.message
|
79
|
-
end
|
80
|
-
|
97
|
+
|
81
98
|
def test_desired_reference_change_trigger
|
82
99
|
foo = Foo.create
|
83
100
|
bar = Bar.create(:name => "Baloney")
|
@@ -92,17 +109,12 @@ class TrackableTest < Test::Unit::TestCase
|
|
92
109
|
assert_equal "Bar changed to Baloney", foo.events.first.message
|
93
110
|
end
|
94
111
|
|
95
|
-
def
|
112
|
+
def test_desired_reference_like_messaging
|
96
113
|
foo = Foo.create
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
foo.bar = bar2; foo.save
|
102
|
-
assert_equal "Bar changed to Jelly", foo.events.first.message
|
103
|
-
assert_equal "Bar changed to Peanut Butter", foo.events.last.message
|
104
|
-
end
|
105
|
-
|
114
|
+
foo.update_attribute(:alternate_identification, "Only a string")
|
115
|
+
assert_equal "Alternate identification changed to Only a string", foo.events.first.message
|
116
|
+
end
|
117
|
+
|
106
118
|
def test_desired_custom_reference_change_trigger
|
107
119
|
foo = Foo.create
|
108
120
|
bar = Bar.create(:name => "Baloney")
|
@@ -116,16 +128,5 @@ class TrackableTest < Test::Unit::TestCase
|
|
116
128
|
foo.custom_bar = bar; foo.save
|
117
129
|
assert_equal "Active Bar set to Baloney", foo.events.first.message
|
118
130
|
end
|
119
|
-
|
120
|
-
def test_desired_custom_reference_stackability
|
121
|
-
foo = Foo.create
|
122
|
-
bar1 = Bar.create(:name => "Peanut Butter")
|
123
|
-
bar2 = Bar.create(:name => "Jelly")
|
124
|
-
foo.custom_bar = bar1; foo.save
|
125
|
-
sleep 1 #mur
|
126
|
-
foo.custom_bar = bar2; foo.save
|
127
|
-
assert_equal "Active Bar set to Jelly", foo.events.first.message
|
128
|
-
assert_equal "Active Bar set to Peanut Butter", foo.events.last.message
|
129
|
-
end
|
130
|
-
|
131
|
+
|
131
132
|
end
|