track_changes 1.0.0.pre3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +27 -0
- data/VERSION.yml +1 -1
- data/lib/track_changes/action_controller.rb +24 -24
- data/lib/track_changes/active_record.rb +13 -19
- data/lib/track_changes/{around_save.rb → around_update.rb} +2 -2
- data/lib/track_changes/changes.rb +2 -0
- data/lib/track_changes/current_user_filter.rb +1 -1
- data/lib/track_changes.rb +1 -1
- data/test/action_controller_test.rb +74 -0
- data/test/current_user_test.rb +15 -0
- data/test/functional/posts_controller_test.rb +1 -1
- data/test/railsapp/Gemfile +1 -1
- data/test/railsapp/Gemfile.lock +4 -4
- data/test/railsapp/db/test.sqlite3 +0 -0
- data/test/railsapp/log/test.log +2335 -3901
- data/test/track_changes_test.rb +9 -1
- metadata +11 -10
data/README.rdoc
CHANGED
@@ -3,6 +3,33 @@
|
|
3
3
|
TrackChanges is a Rails engine to facilitate tracking changes made to your
|
4
4
|
models.
|
5
5
|
|
6
|
+
== Release Notes
|
7
|
+
|
8
|
+
Version 1.0 is a major change. This gem has been re-written from scratch as a
|
9
|
+
Rails 3.0 engine. You will need to take additional steps to upgrade from
|
10
|
+
versions prior to 1.0.
|
11
|
+
|
12
|
+
=== General Upgrade Notes:
|
13
|
+
|
14
|
+
* You no longer need to <tt>include TrackChanges</tt> in your controllers.
|
15
|
+
* Additional arguments such as <tt>:only</tt>, and <tt>:except</tt> are no longer accepted in controllers
|
16
|
+
* Any model that specifies <tt>track_changes</tt> will always have an Audit created on updates.
|
17
|
+
* The revert capability is no longer available. This feature may return in the future.
|
18
|
+
|
19
|
+
=== Specific Upgrade Instructions:
|
20
|
+
|
21
|
+
* Update your <tt>Gemfile</tt>
|
22
|
+
* Remove all occurences of <tt>include TrackChanges</tt> from your controllers
|
23
|
+
* Remove your existing Audit class in <tt>RAILS_ROOT/app/models</tt>, this class is now part of the engine
|
24
|
+
* Modify any calls to <tt>track_changes</tt> to remove additional arguments
|
25
|
+
* Modify your <tt>RAILS_ROOT/config/track_changes_configuration</tt> to the following:
|
26
|
+
|
27
|
+
# Use whatever call you need to find the default user to be used if
|
28
|
+
# current_user is not set in your controller.
|
29
|
+
TrackChanges::Configuration::DEFAULT_USER_FINDER = Proc.new {
|
30
|
+
User.where(:login => "admin")
|
31
|
+
}
|
32
|
+
|
6
33
|
== Why?
|
7
34
|
|
8
35
|
I originally looked at the available auditing solutions and it appeared that
|
data/VERSION.yml
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
module TrackChanges
|
2
|
-
module ActionController
|
3
|
-
def self.included(base)
|
4
|
-
base.send :extend, ClassMethods
|
5
|
-
end
|
2
|
+
module ActionController
|
6
3
|
|
7
|
-
|
4
|
+
# Sets up an around filter to assign the controller's <tt>current_user</tt>
|
5
|
+
# to the given model names that are already set as instance variables in a
|
6
|
+
# prior <tt>before_filter</tt>.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# track_changes :post
|
11
|
+
# track_changes :post, :post_attribute
|
12
|
+
#
|
13
|
+
# Currently does not work if the instance variable is anything except
|
14
|
+
# an model that has <tt>current_user</tt> accessors.
|
15
|
+
def track_changes models, *args
|
16
|
+
models_to_track = []
|
17
|
+
models_to_track << models
|
18
|
+
if args.kind_of?(Array)
|
19
|
+
models_to_track << args
|
20
|
+
end
|
21
|
+
models_to_track.flatten!
|
8
22
|
|
9
|
-
|
10
|
-
# to the given model names that are already set as instance variables in a
|
11
|
-
# prior <tt>before_filter</tt>.
|
12
|
-
#
|
13
|
-
# Example:
|
14
|
-
#
|
15
|
-
# track_changes :post
|
16
|
-
# track_changes :post, :post_attribute
|
17
|
-
#
|
18
|
-
# Currently does not work if the instance variable is anything except
|
19
|
-
# an model that has <tt>current_user</tt> accessors.
|
20
|
-
def track_changes models
|
21
|
-
models_to_track = [models].flatten
|
22
|
-
define_method(:__track_changes_to_models) { models_to_track }
|
23
|
+
define_method(:__track_changes_to_models) { models_to_track }
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
25
|
+
self.class_eval do
|
26
|
+
helper :audits
|
27
|
+
before_filter TrackChanges::CurrentUserFilter
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
ActionController::Base.
|
33
|
+
ActionController::Base.extend TrackChanges::ActionController
|
@@ -1,28 +1,22 @@
|
|
1
1
|
module TrackChanges
|
2
|
-
module ActiveRecord
|
3
|
-
def self.included(base)
|
4
|
-
base.send :extend, ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
2
|
+
module ActiveRecord
|
8
3
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
# Enables auditing of all changes to an ActiveRecord model. Sets up an
|
5
|
+
# around filter that will create an Audit for the models <tt>changes</tt>
|
6
|
+
# attribute.
|
7
|
+
#
|
8
|
+
# In addition, this will also define a <tt>attr_accessor</tt> for <tt>current_user</tt>.
|
9
|
+
def track_changes
|
10
|
+
send :include, TrackChanges::CurrentUser
|
11
|
+
send :include, TrackChanges::Changes
|
17
12
|
|
18
|
-
|
19
|
-
|
13
|
+
self.class_eval do
|
14
|
+
has_many :audits, :as => :audited
|
20
15
|
|
21
|
-
|
22
|
-
end
|
16
|
+
around_update TrackChanges::AroundUpdate
|
23
17
|
end
|
24
18
|
end
|
25
19
|
end
|
26
20
|
end
|
27
21
|
|
28
|
-
ActiveRecord::Base.
|
22
|
+
ActiveRecord::Base.extend TrackChanges::ActiveRecord
|
data/lib/track_changes.rb
CHANGED
@@ -4,7 +4,7 @@ end
|
|
4
4
|
|
5
5
|
require 'track_changes/action_controller'
|
6
6
|
require 'track_changes/active_record'
|
7
|
-
require 'track_changes/
|
7
|
+
require 'track_changes/around_update'
|
8
8
|
require 'track_changes/changes'
|
9
9
|
require 'track_changes/configuration'
|
10
10
|
require 'track_changes/current_user'
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
class ActionControllerTest < Test::Unit::TestCase
|
3
|
+
class ActionControllerMock
|
4
|
+
def self.helper(args)
|
5
|
+
@__helper = args
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.helper_value
|
9
|
+
@__helper
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.before_filter(args)
|
13
|
+
@__before_filter = args
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.before_filter_value
|
17
|
+
@__before_filter
|
18
|
+
end
|
19
|
+
|
20
|
+
extend TrackChanges::ActionController
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def setup
|
25
|
+
@action_controller = ActionControllerMock.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_add_track_changes_class_method
|
29
|
+
assert ActionControllerMock.respond_to?(:track_changes)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_set_a_before_filter
|
33
|
+
ActionControllerMock.class_eval do
|
34
|
+
track_changes :some_var
|
35
|
+
end
|
36
|
+
|
37
|
+
assert ActionControllerMock.before_filter_value.respond_to?(:filter)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_accept_multiple_arguments
|
41
|
+
assert_nothing_raised do
|
42
|
+
ActionControllerMock.class_eval do
|
43
|
+
track_changes :foo
|
44
|
+
end
|
45
|
+
|
46
|
+
ActionControllerMock.class_eval do
|
47
|
+
track_changes :foo, :bar
|
48
|
+
end
|
49
|
+
|
50
|
+
ActionControllerMock.class_eval do
|
51
|
+
track_changes :foo, :bar, :baz
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_should_define_a_method_that_returns_interested_vars
|
57
|
+
ActionControllerMock.class_eval do
|
58
|
+
track_changes :foo, :bar, :baz
|
59
|
+
end
|
60
|
+
|
61
|
+
interesting_vars = ActionControllerMock.new.__track_changes_to_models
|
62
|
+
assert interesting_vars.include?(:foo), interesting_vars.inspect
|
63
|
+
assert interesting_vars.include?(:bar), interesting_vars.inspect
|
64
|
+
assert interesting_vars.include?(:baz), interesting_vars.inspect
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_include_helpers
|
68
|
+
ActionControllerMock.class_eval do
|
69
|
+
track_changes :foo
|
70
|
+
end
|
71
|
+
|
72
|
+
assert_equal :audits, ActionControllerMock.helper_value
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
class CurrentUserTest < Test::Unit::TestCase
|
3
|
+
|
4
|
+
class ClassUnderTest
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_should_add_current_user_accessor_when_included
|
8
|
+
ClassUnderTest.send(:include, TrackChanges::CurrentUser)
|
9
|
+
|
10
|
+
class_under_test = ClassUnderTest.new
|
11
|
+
|
12
|
+
assert class_under_test.respond_to?(:current_user)
|
13
|
+
assert class_under_test.respond_to?(:current_user=)
|
14
|
+
end
|
15
|
+
end
|
@@ -13,7 +13,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
13
13
|
@post.reload
|
14
14
|
expected_change = { "title" => ["Hello", "Changed"]}
|
15
15
|
assert_equal "Changed", @post.title
|
16
|
-
assert_equal
|
16
|
+
assert_equal 1, @post.audits.count
|
17
17
|
assert_equal expected_change, @post.audits.desc.first.change_set
|
18
18
|
end
|
19
19
|
|
data/test/railsapp/Gemfile
CHANGED
data/test/railsapp/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: C:/Users/Matt/Code/track_changes
|
3
3
|
specs:
|
4
|
-
track_changes (1.0.0.
|
4
|
+
track_changes (1.0.0.pre3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -51,7 +51,7 @@ GEM
|
|
51
51
|
mime-types (1.16)
|
52
52
|
polyglot (0.3.1)
|
53
53
|
rack (1.2.1)
|
54
|
-
rack-mount (0.6.
|
54
|
+
rack-mount (0.6.11)
|
55
55
|
rack (>= 1.0.0)
|
56
56
|
rack-test (0.5.4)
|
57
57
|
rack (>= 1.0)
|
@@ -72,7 +72,7 @@ GEM
|
|
72
72
|
thor (0.14.0)
|
73
73
|
treetop (1.4.8)
|
74
74
|
polyglot (>= 0.3.1)
|
75
|
-
tzinfo (0.3.
|
75
|
+
tzinfo (0.3.23)
|
76
76
|
|
77
77
|
PLATFORMS
|
78
78
|
java
|
@@ -80,5 +80,5 @@ PLATFORMS
|
|
80
80
|
DEPENDENCIES
|
81
81
|
activerecord-jdbc-adapter
|
82
82
|
activerecord-jdbcsqlite3-adapter
|
83
|
-
rails (
|
83
|
+
rails (~> 3.0.0)
|
84
84
|
track_changes!
|
Binary file
|