track_changes 0.5.0 → 0.5.1

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.
Files changed (40) hide show
  1. data/LICENSE +20 -20
  2. data/README.rdoc +87 -87
  3. data/TODO +4 -4
  4. data/VERSION.yml +5 -5
  5. data/lib/track_changes/audit_filter.rb +69 -69
  6. data/lib/track_changes/base.rb +9 -9
  7. data/lib/track_changes/class_methods.rb +74 -74
  8. data/lib/track_changes/configuration.rb +38 -38
  9. data/lib/track_changes/filter.rb +43 -43
  10. data/lib/track_changes/initializer.rb +18 -18
  11. data/lib/track_changes/instance_methods.rb +5 -5
  12. data/lib/track_changes/result.rb +36 -36
  13. data/lib/track_changes.rb +12 -12
  14. data/test/base_test.rb +20 -20
  15. data/test/class_methods_test.rb +29 -29
  16. data/test/filter_test.rb +73 -73
  17. data/test/functional/posts_controller_test.rb +16 -16
  18. data/test/rails_root/app/controllers/application_controller.rb +2 -2
  19. data/test/rails_root/app/controllers/posts_controller.rb +36 -36
  20. data/test/rails_root/app/models/audit.rb +4 -4
  21. data/test/rails_root/app/models/post.rb +3 -3
  22. data/test/rails_root/config/boot.rb +110 -110
  23. data/test/rails_root/config/database.yml +3 -3
  24. data/test/rails_root/config/environment.rb +7 -7
  25. data/test/rails_root/config/environments/test.rb +7 -7
  26. data/test/rails_root/config/initializers/new_rails_defaults.rb +7 -7
  27. data/test/rails_root/config/initializers/session_store.rb +4 -4
  28. data/test/rails_root/config/initializers/track_changes_setup.rb +8 -8
  29. data/test/rails_root/config/routes.rb +3 -3
  30. data/test/rails_root/db/migrate/20100115021125_create_audits.rb +16 -16
  31. data/test/rails_root/db/migrate/20100115021151_create_posts.rb +15 -15
  32. data/test/rails_root/log/test.log +377 -2494
  33. data/test/rails_root/script/console +3 -3
  34. data/test/rails_root/script/generate +3 -3
  35. data/test/result_test.rb +35 -35
  36. data/test/test_helper.rb +29 -29
  37. data/test/track_changes/audit_filter_test.rb +154 -154
  38. data/test/track_changes/configuration_test.rb +36 -36
  39. data/test/track_changes/initializer_test.rb +37 -37
  40. metadata +31 -19
@@ -1,43 +1,43 @@
1
- require 'track_changes/result'
2
-
3
- module TrackChanges
4
- class Filter
5
- attr_accessor :call_proc, :changes, :model, :original
6
-
7
- def initialize(model, yield_only_changed, call_proc)
8
- @call_proc = call_proc
9
- @model = model
10
- @yield_only_changed = yield_only_changed
11
- @changes = []
12
- @original = []
13
- end
14
-
15
- def before(controller)
16
- @original = clone_model(controller)
17
- end
18
-
19
- def after(controller)
20
- cur_model = controller.instance_variable_get("@#{@model}")
21
- # If changed? returns true, then the update didn't succeed.
22
- unless cur_model.changed?
23
- @changes = changes_between(@original, cur_model)
24
- unless @yield_only_changed && @changes.empty?
25
- @call_proc.call(Result.new(@model, controller, @changes))
26
- end
27
- end
28
- nil
29
- end
30
-
31
- private
32
-
33
- def changes_between(old, new)
34
- old.attributes = new.attributes
35
- old.changes
36
- end
37
-
38
- def clone_model(controller)
39
- ivar = controller.instance_variable_get("@#{@model}")
40
- ivar.clone if ivar.respond_to?(:clone)
41
- end
42
- end
43
- end
1
+ require 'track_changes/result'
2
+
3
+ module TrackChanges
4
+ class Filter
5
+ attr_accessor :call_proc, :changes, :model, :original
6
+
7
+ def initialize(model, yield_only_changed, call_proc)
8
+ @call_proc = call_proc
9
+ @model = model
10
+ @yield_only_changed = yield_only_changed
11
+ @changes = []
12
+ @original = []
13
+ end
14
+
15
+ def before(controller)
16
+ @original = clone_model(controller)
17
+ end
18
+
19
+ def after(controller)
20
+ cur_model = controller.instance_variable_get("@#{@model}")
21
+ # If changed? returns true, then the update didn't succeed.
22
+ unless cur_model.changed?
23
+ @changes = changes_between(@original, cur_model)
24
+ unless @yield_only_changed && @changes.empty?
25
+ @call_proc.call(Result.new(@model, controller, @changes))
26
+ end
27
+ end
28
+ nil
29
+ end
30
+
31
+ private
32
+
33
+ def changes_between(old, new)
34
+ old.attributes = new.attributes
35
+ old.changes
36
+ end
37
+
38
+ def clone_model(controller)
39
+ ivar = controller.instance_variable_get("@#{@model}")
40
+ ivar.clone if ivar.respond_to?(:clone)
41
+ end
42
+ end
43
+ end
@@ -1,18 +1,18 @@
1
- module TrackChanges
2
- class Initializer
3
- attr_accessor :configuration
4
-
5
- include Singleton
6
-
7
- class << self
8
- alias_method :__tr_instance, :instance
9
-
10
- def instance(&block)
11
- obj = __tr_instance
12
- obj.configuration ||= Configuration.new
13
- yield obj.configuration if block_given?
14
- obj
15
- end
16
- end
17
- end
18
- end
1
+ module TrackChanges
2
+ class Initializer
3
+ attr_accessor :configuration
4
+
5
+ include Singleton
6
+
7
+ class << self
8
+ alias_method :__tr_instance, :instance
9
+
10
+ def instance(&block)
11
+ obj = __tr_instance
12
+ obj.configuration ||= Configuration.new
13
+ yield obj.configuration if block_given?
14
+ obj
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
- module TrackChanges
2
- module InstanceMethods
3
- attr_accessor :track_changes_filter
4
- end
5
- end
1
+ module TrackChanges
2
+ module InstanceMethods
3
+ attr_accessor :track_changes_filter
4
+ end
5
+ end
@@ -1,36 +1,36 @@
1
- module TrackChanges
2
- # A Result instance is yielded by ClassMethods#track_changes_to.
3
- #
4
- # Example:
5
- #
6
- # track_changes_to :post do |result|
7
- # result.post # => The instance variable in the current_controler
8
- # # given by the :post option.
9
- # result.controller # => The instance of the current controller
10
- # result.current_user # => The current controllers @current_user
11
- # result.some_method # => Call some_method on the current controller instance
12
- # end
13
- class Result
14
- attr_accessor :changes, :controller
15
-
16
- def initialize(model_name, controller, changes)
17
- @changes = changes
18
- @controller = controller
19
-
20
- self.class.send(:define_method, model_name) do
21
- @controller.instance_variable_get("@#{model_name}")
22
- end
23
- end
24
-
25
- # Pass all other methods to the controller.
26
- def method_missing(method_sym, *args)
27
- controller.send(method_sym, *args)
28
- end
29
-
30
- # A convienence method that returns the <tt>@current_user</tt>
31
- # in the current controller.
32
- def current_user
33
- user = controller.instance_variable_get("@current_user")
34
- end
35
- end
36
- end
1
+ module TrackChanges
2
+ # A Result instance is yielded by ClassMethods#track_changes_to.
3
+ #
4
+ # Example:
5
+ #
6
+ # track_changes_to :post do |result|
7
+ # result.post # => The instance variable in the current_controler
8
+ # # given by the :post option.
9
+ # result.controller # => The instance of the current controller
10
+ # result.current_user # => The current controllers @current_user
11
+ # result.some_method # => Call some_method on the current controller instance
12
+ # end
13
+ class Result
14
+ attr_accessor :changes, :controller
15
+
16
+ def initialize(model_name, controller, changes)
17
+ @changes = changes
18
+ @controller = controller
19
+
20
+ self.class.send(:define_method, model_name) do
21
+ @controller.instance_variable_get("@#{model_name}")
22
+ end
23
+ end
24
+
25
+ # Pass all other methods to the controller.
26
+ def method_missing(method_sym, *args)
27
+ controller.send(method_sym, *args)
28
+ end
29
+
30
+ # A convienence method that returns the <tt>@current_user</tt>
31
+ # in the current controller.
32
+ def current_user
33
+ user = controller.instance_variable_get("@current_user")
34
+ end
35
+ end
36
+ end
data/lib/track_changes.rb CHANGED
@@ -1,12 +1,12 @@
1
- require 'track_changes/result'
2
- require 'track_changes/configuration'
3
- require 'track_changes/initializer'
4
- require 'track_changes/audit_filter'
5
- require 'track_changes/filter'
6
- require 'track_changes/instance_methods'
7
- require 'track_changes/class_methods'
8
- require 'track_changes/base'
9
-
10
- module TrackChanges
11
-
12
- end
1
+ require 'track_changes/result'
2
+ require 'track_changes/configuration'
3
+ require 'track_changes/initializer'
4
+ require 'track_changes/audit_filter'
5
+ require 'track_changes/filter'
6
+ require 'track_changes/instance_methods'
7
+ require 'track_changes/class_methods'
8
+ require 'track_changes/base'
9
+
10
+ module TrackChanges
11
+
12
+ end
data/test/base_test.rb CHANGED
@@ -1,20 +1,20 @@
1
- require 'test_helper'
2
-
3
- class BaseTest < Test::Unit::TestCase
4
- context "when TrackChanges is included in a Class" do
5
- setup do
6
- class A
7
- include TrackChanges
8
- end
9
- end
10
-
11
- should "include InstanceMethods" do
12
- assert A.included_modules.include?(TrackChanges::InstanceMethods)
13
- end
14
-
15
- should "extend class with ClassMethods" do
16
- extended_modules = (class << A; self; end).included_modules
17
- assert extended_modules.include?(TrackChanges::ClassMethods)
18
- end
19
- end
20
- end
1
+ require 'test_helper'
2
+
3
+ class BaseTest < Test::Unit::TestCase
4
+ context "when TrackChanges is included in a Class" do
5
+ setup do
6
+ class A
7
+ include TrackChanges
8
+ end
9
+ end
10
+
11
+ should "include InstanceMethods" do
12
+ assert A.included_modules.include?(TrackChanges::InstanceMethods)
13
+ end
14
+
15
+ should "extend class with ClassMethods" do
16
+ extended_modules = (class << A; self; end).included_modules
17
+ assert extended_modules.include?(TrackChanges::ClassMethods)
18
+ end
19
+ end
20
+ end
@@ -1,29 +1,29 @@
1
- require 'test_helper'
2
-
3
- class ClassMethodsTest < Test::Unit::TestCase
4
- include TrackChanges::ClassMethods
5
-
6
- context "a ClassMethods module" do
7
- context "when track_changes_to method called" do
8
- setup { track_changes_to(:something) {} }
9
-
10
- should "call append_before_filter" do
11
- assert @before_filter
12
- end
13
-
14
- should "call append_after_filter" do
15
- assert @after_filter
16
- end
17
- end
18
- end
19
-
20
- protected
21
-
22
- def append_before_filter(options = {}, &block)
23
- @before_filter = block
24
- end
25
-
26
- def append_after_filter(options = {}, &block)
27
- @after_filter = block
28
- end
29
- end
1
+ require 'test_helper'
2
+
3
+ class ClassMethodsTest < Test::Unit::TestCase
4
+ include TrackChanges::ClassMethods
5
+
6
+ context "a ClassMethods module" do
7
+ context "when track_changes_to method called" do
8
+ setup { track_changes_to(:something) {} }
9
+
10
+ should "call append_before_filter" do
11
+ assert @before_filter
12
+ end
13
+
14
+ should "call append_after_filter" do
15
+ assert @after_filter
16
+ end
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def append_before_filter(options = {}, &block)
23
+ @before_filter = block
24
+ end
25
+
26
+ def append_after_filter(options = {}, &block)
27
+ @after_filter = block
28
+ end
29
+ end
data/test/filter_test.rb CHANGED
@@ -1,73 +1,73 @@
1
- require 'test_helper'
2
-
3
- class FilterTest < Test::Unit::TestCase
4
- context "a Filter instance" do
5
- setup do
6
- @controller = mock('@controller')
7
- @model = mock('@model')
8
- @proc_result = nil
9
- @proc = Proc.new { |value| @proc_result = value }
10
-
11
- @filter = TrackChanges::Filter.new(:model, true, @proc)
12
- end
13
-
14
- should "set @original when method before called" do
15
- @model.expects(:clone).returns(mock('@model cloned', :my_mock? => true))
16
- @controller.expects(:instance_variable_get).returns(@model)
17
- @filter.before(@controller)
18
-
19
- assert @filter.original.my_mock?
20
- end
21
-
22
- should "return a Result when after called" do
23
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
24
- :changes => {"attr" => ["old", "new"]})
25
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
26
- :changed? => false)
27
-
28
- @controller.expects(:instance_variable_get).once.returns(@new_model)
29
-
30
- @filter.after(@controller)
31
- assert @proc_result.is_a?(TrackChanges::Result)
32
- assert_equal({"attr" => ["old", "new"]}, @proc_result.changes)
33
- end
34
-
35
- context "when yield_only_changed is true" do
36
- setup do
37
- @proc_result = nil
38
- @filter = TrackChanges::Filter.new(:model, true, @proc)
39
- end
40
-
41
- should "not call block when method after called and changes is empty" do
42
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
43
- :changes => {})
44
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
45
- :changed? => false)
46
-
47
- @controller.expects(:instance_variable_get).once.returns(@new_model)
48
-
49
- @filter.after(@controller)
50
- assert_nil @proc_result
51
- end
52
- end
53
-
54
- context "when yield_only_changed is false" do
55
- setup do
56
- @proc_result = nil
57
- @filter = TrackChanges::Filter.new(:model, false, @proc)
58
- end
59
-
60
- should "call block when method after called and changes is empty" do
61
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
62
- :changes => {})
63
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
64
- :changed? => false)
65
-
66
- @controller.expects(:instance_variable_get).once.returns(@new_model)
67
-
68
- @filter.after(@controller)
69
- assert_not_nil @proc_result
70
- end
71
- end
72
- end
73
- end
1
+ require 'test_helper'
2
+
3
+ class FilterTest < Test::Unit::TestCase
4
+ context "a Filter instance" do
5
+ setup do
6
+ @controller = mock('@controller')
7
+ @model = mock('@model')
8
+ @proc_result = nil
9
+ @proc = Proc.new { |value| @proc_result = value }
10
+
11
+ @filter = TrackChanges::Filter.new(:model, true, @proc)
12
+ end
13
+
14
+ should "set @original when method before called" do
15
+ @model.expects(:clone).returns(mock('@model cloned', :my_mock? => true))
16
+ @controller.expects(:instance_variable_get).returns(@model)
17
+ @filter.before(@controller)
18
+
19
+ assert @filter.original.my_mock?
20
+ end
21
+
22
+ should "return a Result when after called" do
23
+ @filter.original = mock('original', :attributes= => {"attr" => "new"},
24
+ :changes => {"attr" => ["old", "new"]})
25
+ @new_model = mock('new_model', :attributes => {"attr" => "new"},
26
+ :changed? => false)
27
+
28
+ @controller.expects(:instance_variable_get).once.returns(@new_model)
29
+
30
+ @filter.after(@controller)
31
+ assert @proc_result.is_a?(TrackChanges::Result)
32
+ assert_equal({"attr" => ["old", "new"]}, @proc_result.changes)
33
+ end
34
+
35
+ context "when yield_only_changed is true" do
36
+ setup do
37
+ @proc_result = nil
38
+ @filter = TrackChanges::Filter.new(:model, true, @proc)
39
+ end
40
+
41
+ should "not call block when method after called and changes is empty" do
42
+ @filter.original = mock('original', :attributes= => {"attr" => "new"},
43
+ :changes => {})
44
+ @new_model = mock('new_model', :attributes => {"attr" => "new"},
45
+ :changed? => false)
46
+
47
+ @controller.expects(:instance_variable_get).once.returns(@new_model)
48
+
49
+ @filter.after(@controller)
50
+ assert_nil @proc_result
51
+ end
52
+ end
53
+
54
+ context "when yield_only_changed is false" do
55
+ setup do
56
+ @proc_result = nil
57
+ @filter = TrackChanges::Filter.new(:model, false, @proc)
58
+ end
59
+
60
+ should "call block when method after called and changes is empty" do
61
+ @filter.original = mock('original', :attributes= => {"attr" => "new"},
62
+ :changes => {})
63
+ @new_model = mock('new_model', :attributes => {"attr" => "new"},
64
+ :changed? => false)
65
+
66
+ @controller.expects(:instance_variable_get).once.returns(@new_model)
67
+
68
+ @filter.after(@controller)
69
+ assert_not_nil @proc_result
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,16 +1,16 @@
1
- require 'test_helper'
2
-
3
- class PostsControllerTest < ActionController::TestCase
4
- test "should update post" do
5
- post = Post.create!(:title => "Hello, World", :body => "This is a test.", :author => "Matt Haley")
6
- assert !post.new_record?
7
-
8
- assert_difference("Audit.count") do
9
- put :update, :id => post.to_param, :post => { :title => "First post!" }
10
- assert_redirected_to post_path(assigns(:post))
11
-
12
- audit = assigns(:post).audits.first
13
- assert_equal ["Hello, World", "First post!"], audit.change_set["title"]
14
- end
15
- end
16
- end
1
+ require 'test_helper'
2
+
3
+ class PostsControllerTest < ActionController::TestCase
4
+ test "should update post" do
5
+ post = Post.create!(:title => "Hello, World", :body => "This is a test.", :author => "Matt Haley")
6
+ assert !post.new_record?
7
+
8
+ assert_difference("Audit.count") do
9
+ put :update, :id => post.to_param, :post => { :title => "First post!" }
10
+ assert_redirected_to post_path(assigns(:post))
11
+
12
+ audit = assigns(:post).audits.first
13
+ assert_equal ["Hello, World", "First post!"], audit.change_set["title"]
14
+ end
15
+ end
16
+ end
@@ -1,2 +1,2 @@
1
- class ApplicationController < ActionController::Base
2
- end
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -1,36 +1,36 @@
1
- require 'track_changes'
2
-
3
- class PostsController < ApplicationController
4
- include TrackChanges
5
- before_filter :find_post
6
- track_changes :post, :only => :update
7
-
8
- def show
9
- render :text => @post.inspect
10
- end
11
-
12
- # PUT /posts/1
13
- # PUT /posts/1.xml
14
- def update
15
- respond_to do |format|
16
- if @post.update_attributes(params[:post])
17
- flash[:notice] = 'Post was successfully updated.'
18
- format.html { redirect_to(@post) }
19
- format.xml { head :ok }
20
- else
21
- format.html { render :action => "edit" }
22
- format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
23
- end
24
- end
25
- end
26
-
27
- protected
28
-
29
- def current_user
30
- "John Smith"
31
- end
32
-
33
- def find_post
34
- @post = Post.find(params[:id])
35
- end
36
- end
1
+ require 'track_changes'
2
+
3
+ class PostsController < ApplicationController
4
+ include TrackChanges
5
+ before_filter :find_post
6
+ track_changes :post, :only => :update
7
+
8
+ def show
9
+ render :text => @post.inspect
10
+ end
11
+
12
+ # PUT /posts/1
13
+ # PUT /posts/1.xml
14
+ def update
15
+ respond_to do |format|
16
+ if @post.update_attributes(params[:post])
17
+ flash[:notice] = 'Post was successfully updated.'
18
+ format.html { redirect_to(@post) }
19
+ format.xml { head :ok }
20
+ else
21
+ format.html { render :action => "edit" }
22
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
23
+ end
24
+ end
25
+ end
26
+
27
+ protected
28
+
29
+ def current_user
30
+ "John Smith"
31
+ end
32
+
33
+ def find_post
34
+ @post = Post.find(params[:id])
35
+ end
36
+ end
@@ -1,4 +1,4 @@
1
- class Audit < ActiveRecord::Base
2
- belongs_to :audited, :polymorphic => true
3
- serialize :change_set
4
- end
1
+ class Audit < ActiveRecord::Base
2
+ belongs_to :audited, :polymorphic => true
3
+ serialize :change_set
4
+ end
@@ -1,3 +1,3 @@
1
- class Post < ActiveRecord::Base
2
- has_many :audits, :as => :audited
3
- end
1
+ class Post < ActiveRecord::Base
2
+ has_many :audits, :as => :audited
3
+ end