track_changes 1.0.0.pre3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -2,4 +2,4 @@
2
2
  :major: 1
3
3
  :minor: 0
4
4
  :patch: 0
5
- :build: pre3
5
+ :build:
@@ -1,33 +1,33 @@
1
1
  module TrackChanges
2
- module ActionController #:nodoc:
3
- def self.included(base)
4
- base.send :extend, ClassMethods
5
- end
2
+ module ActionController
6
3
 
7
- module ClassMethods
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
- # Sets up an around filter to assign the controller's <tt>current_user</tt>
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
- self.class_eval do
25
- helper :audits
26
- before_filter TrackChanges::CurrentUserFilter
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.send :include, TrackChanges::ActionController
33
+ ActionController::Base.extend TrackChanges::ActionController
@@ -1,28 +1,22 @@
1
1
  module TrackChanges
2
- module ActiveRecord #:nodoc:
3
- def self.included(base)
4
- base.send :extend, ClassMethods
5
- end
6
-
7
- module ClassMethods
2
+ module ActiveRecord
8
3
 
9
- # Enables auditing of all changes to an ActiveRecord model. Sets up an
10
- # around filter that will create an Audit for the models <tt>changes</tt>
11
- # attribute.
12
- #
13
- # In addition, this will also define a <tt>attr_accessor</tt> for <tt>current_user</tt>.
14
- def track_changes
15
- send :include, TrackChanges::CurrentUser
16
- send :include, TrackChanges::Changes
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
- self.class_eval do
19
- has_many :audits, :as => :audited
13
+ self.class_eval do
14
+ has_many :audits, :as => :audited
20
15
 
21
- around_save TrackChanges::AroundSave
22
- end
16
+ around_update TrackChanges::AroundUpdate
23
17
  end
24
18
  end
25
19
  end
26
20
  end
27
21
 
28
- ActiveRecord::Base.send :include, TrackChanges::ActiveRecord
22
+ ActiveRecord::Base.extend TrackChanges::ActiveRecord
@@ -1,6 +1,6 @@
1
1
  module TrackChanges
2
- class AroundSave #:nodoc:
3
- def self.around_save(model)
2
+ class AroundUpdate #:nodoc:
3
+ def self.around_update(model)
4
4
  model.send(:track_changes_get_changes)
5
5
  yield
6
6
  model.send(:track_changes_save_audit)
@@ -8,6 +8,8 @@ module TrackChanges
8
8
  end
9
9
 
10
10
  def track_changes_save_audit
11
+ return unless self.id
12
+
11
13
  user = current_user || TrackChanges::Configuration::get_default_user
12
14
  Audit.create_changes(self, @__model_changes, current_user)
13
15
  end
@@ -1,5 +1,5 @@
1
1
  module TrackChanges
2
- module CurrentUserFilter #:nodoc:
2
+ class CurrentUserFilter #:nodoc:
3
3
  def self.filter(controller)
4
4
  controller.send(:__track_changes_to_models).each do |model|
5
5
  instance = controller.instance_variable_get("@#{model}")
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/around_save'
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 2, @post.audits.count
16
+ assert_equal 1, @post.audits.count
17
17
  assert_equal expected_change, @post.audits.desc.first.change_set
18
18
  end
19
19
 
@@ -1,6 +1,6 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'rails', '3.0.0.rc'
3
+ gem 'rails', '~> 3.0.0'
4
4
 
5
5
  if defined?(JRUBY_VERSION)
6
6
  gem 'activerecord-jdbc-adapter'
@@ -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.pre2)
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.9)
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.22)
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 (= 3.0.0.rc)
83
+ rails (~> 3.0.0)
84
84
  track_changes!
Binary file