activerecord-diff 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordDiff
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -32,6 +32,7 @@ module ActiveRecord
32
32
  end
33
33
 
34
34
  def diff(other_record = nil)
35
+
35
36
  if other_record.nil?
36
37
  old_record, new_record = self.class.find(id), self
37
38
  else
@@ -40,11 +41,19 @@ module ActiveRecord
40
41
 
41
42
  if new_record.is_a?(Hash)
42
43
  diff_each(new_record) do |(attr_name, hash_value)|
43
- [attr_name, old_record.send(attr_name), hash_value]
44
+ if block_given?
45
+ yield attr_name, old_record.send(attr_name), hash_value
46
+ else
47
+ [attr_name, old_record.send(attr_name), hash_value]
48
+ end
44
49
  end
45
50
  else
46
51
  diff_each(self.class.diff_attrs) do |attr_name|
47
- [attr_name, old_record.send(attr_name), new_record.send(attr_name)]
52
+ if block_given?
53
+ yield attr_name, old_record.send(attr_name), new_record.send(attr_name)
54
+ else
55
+ [attr_name, old_record.send(attr_name), new_record.send(attr_name)]
56
+ end
48
57
  end
49
58
  end
50
59
  end
data/test/test.rb CHANGED
@@ -19,14 +19,16 @@ end
19
19
  Person.create :name => 'alice', :email_address => 'alice@example.org'
20
20
 
21
21
  Person.create :name => 'bob', :email_address => 'bob@example.org'
22
+ Person.create :name => 'BOB', :email_address => 'BOB@EXAMPLE.ORG'
22
23
 
23
24
  Person.create :name => 'eve', :email_address => 'bob@example.org'
24
25
 
26
+
25
27
  class TestCase < Test::Unit::TestCase
26
28
  def setup
27
29
  @people = Person.find(:all)
28
30
 
29
- @alice, @bob, @eve = *@people
31
+ @alice, @bob, @capital_bob, @eve = *@people
30
32
  end
31
33
 
32
34
  def keysort(hash)
@@ -55,6 +57,17 @@ class TestCase < Test::Unit::TestCase
55
57
  assert_diff @bob, @eve, {:name => %w( bob eve )}
56
58
  end
57
59
 
60
+ def test_diff_with_block
61
+ assert_diff @bob, @capital_bob, {:name => %w( bob BOB), :email_address => %w( bob@example.org BOB@EXAMPLE.ORG )}
62
+
63
+ diff = @bob.diff(@capital_bob) do |attrib, old, new|
64
+ [attrib, old.downcase, new.downcase]
65
+ end
66
+
67
+ assert diff.empty?
68
+ end
69
+
70
+
58
71
  def test_diff_against_saved_self
59
72
  assert ! @eve.diff?
60
73
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &10351960 !ruby/object:Gem::Requirement
17
+ requirement: &9846140 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *10351960
25
+ version_requirements: *9846140
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &10351750 !ruby/object:Gem::Requirement
28
+ requirement: &9845930 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *10351750
36
+ version_requirements: *9845930
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &10351540 !ruby/object:Gem::Requirement
39
+ requirement: &9845720 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *10351540
47
+ version_requirements: *9845720
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rake
50
- requirement: &10351330 !ruby/object:Gem::Requirement
50
+ requirement: &9845510 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *10351330
58
+ version_requirements: *9845510
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: sqlite3-ruby
61
- requirement: &10351120 !ruby/object:Gem::Requirement
61
+ requirement: &9845300 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *10351120
69
+ version_requirements: *9845300
70
70
  description: ! 'Simple diff for ActiveRecord '
71
71
  email:
72
72
  - stephenp@agrussell.com