smajn-track_changes 0.2.1 → 0.3.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 CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  TrackChanges is a Rails plugin to facilitate tracking
4
4
  changes made to an ActiveRecord model in your
5
- Controller#update actions.
5
+ Controller#update actions. By default, the block is
6
+ only called if the changes hash is not empty.
6
7
 
7
8
  A TrackChanges::Filter is instantiated on each run of the <tt>before_filter</tt>
8
9
  so it should be thread-safe.
data/TODO CHANGED
@@ -1,5 +1,4 @@
1
1
  Track Changes TODO List
2
2
  =======================
3
3
 
4
- * Tests
5
4
  * Check for ivars
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 2
3
- :patch: 1
2
+ :minor: 3
3
+ :patch: 0
4
4
  :major: 0
@@ -9,11 +9,12 @@ module TrackChanges
9
9
  # Parameters:
10
10
  #
11
11
  # * <tt>model</tt>: A symbol of the model instance name to track changes to
12
+ # * <tt>only_if_changed</tt>: If true, will only yield block if changes is not empty. Defaults to <tt>true</tt>.
12
13
  # * <tt>options</tt>: An options hash the will be supplied to <tt>before_filter</tt> and <tt>after_filter</tt>. Defaults to <tt>:only => :update</tt>.
13
14
  # * <tt>&block</tt>: The supplied block is called with an instance of Result as its parameter.
14
- def track_changes_to(models, options = {:only => :update}, &block)
15
+ def track_changes_to(models, only_if_changed = true, options = {:only => :update}, &block)
15
16
  append_before_filter(options) do |controller|
16
- track_filter = Filter.new(models, block)
17
+ track_filter = Filter.new(models, only_if_changed, block)
17
18
  controller.track_changes_filter = track_filter
18
19
 
19
20
  track_filter.before(controller)
@@ -4,9 +4,10 @@ module TrackChanges
4
4
  class Filter
5
5
  attr_accessor :call_proc, :changes, :model, :original
6
6
 
7
- def initialize(model, call_proc)
7
+ def initialize(model, yield_only_changed, call_proc)
8
8
  @call_proc = call_proc
9
9
  @model = model
10
+ @yield_only_changed = yield_only_changed
10
11
  @changes = []
11
12
  @original = []
12
13
  end
@@ -17,11 +18,14 @@ module TrackChanges
17
18
 
18
19
  def after(controller)
19
20
  cur_model = controller.instance_variable_get("@#{@model}")
20
- # If changed? returns true, then the update didn't success.
21
+ # If changed? returns true, then the update didn't succeed.
21
22
  unless cur_model.changed?
22
23
  @changes = changes_between(@original, cur_model)
23
- @call_proc.call(Result.new(@model, controller, @changes))
24
+ unless @yield_only_changed && @changes.empty?
25
+ @call_proc.call(Result.new(@model, controller, @changes))
26
+ end
24
27
  end
28
+ nil
25
29
  end
26
30
 
27
31
  private
@@ -0,0 +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
@@ -0,0 +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
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ResultTest < Test::Unit::TestCase
4
+ context "a Result instance" do
5
+ setup do
6
+ @controller = mock()
7
+ @changes = { "attr" => ["old", "new"] }
8
+
9
+ @filter = TrackChanges::Result.new(:model, @controller, @changes)
10
+ end
11
+
12
+ should "define given symbol as method" do
13
+ @controller.expects(:instance_variable_get).with() {|v| v == "@model"}.returns(:ivar_result)
14
+ assert_equal :ivar_result, @filter.model
15
+ end
16
+
17
+ should "send method_missing calls to controller" do
18
+ @controller.expects(:bogus_method).returns(:got_it)
19
+ assert_equal :got_it, @filter.bogus_method
20
+ end
21
+
22
+ should "get @current_user from controller" do
23
+ @controller.expects(:instance_variable_get).with() {|v| v == "@current_user"}.returns(:ivar_result)
24
+ assert_equal :ivar_result, @filter.current_user
25
+ end
26
+
27
+ should "return changes" do
28
+ assert_equal @changes, @filter.changes
29
+ end
30
+
31
+ should "return controller" do
32
+ assert_equal @controller, @filter.controller
33
+ end
34
+ end
35
+ end
data/test/test_helper.rb CHANGED
@@ -2,9 +2,13 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
4
  require 'mocha'
5
+ require 'ostruct'
5
6
 
6
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
8
  require 'track_changes'
8
9
 
9
10
  class Test::Unit::TestCase
11
+ def test_truth
12
+ assert true
13
+ end
10
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smajn-track_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Haley
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-29 00:00:00 -08:00
12
+ date: 2009-02-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,7 +33,9 @@ files:
33
33
  - lib/track_changes/class_methods.rb
34
34
  - lib/track_changes/filter.rb
35
35
  - lib/track_changes.rb
36
- - test/track_changes_test.rb
36
+ - test/result_test.rb
37
+ - test/class_methods_test.rb
38
+ - test/filter_test.rb
37
39
  - test/test_helper.rb
38
40
  has_rdoc: true
39
41
  homepage: http://github.com/smajn/track_changes
@@ -1,7 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class TrackChangesTest < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end