acts_as_comparable 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,13 @@
1
+ 2007-06-30 zdennis <zdennis@elijah.local>
2
+
3
+ * Renamed MockModel to DummyModel in test_helper.rb
4
+
5
+ * Added/Updated tests for acts_as_comparable with different options
6
+
7
+ * Removed tests for the now private method comparables_attributes_for
8
+
9
+ * Made comparable_attributes_for a private method
10
+
1
11
  2007-04-20 zdennis <zdennis@elijah.local>
2
12
 
3
13
  * Made script/plugin installation friendly
data/README CHANGED
@@ -13,6 +13,10 @@ Testing acts_as_comparable relies on:
13
13
  * Mocha 0.4.0 or higher (tested with 0.4.0)
14
14
  * Ruby 1.8.4, 1.8.5 or 1.8.6
15
15
 
16
+ DOCS
17
+ ----
18
+ http://www.continuousthining.com/tags/acts_as_comparable
19
+
16
20
  SVN
17
21
  ----
18
22
  Public anonymous read svn:
@@ -53,17 +53,18 @@ module ActiveRecord::Acts::Comparable
53
53
  # * :attrs_map - a hash of field to field mappings as symbols which define how fields should be compared
54
54
  def acts_as_comparable( options = {} )
55
55
  #pullin in necessary methods
56
- cattr_accessor :comparable_attributes, :comparable_options
56
+ cattr_accessor :comparable_options
57
57
  include ActiveRecord::Acts::Comparable::InstanceMethods
58
58
 
59
59
  #setup options
60
60
  self.comparable_options = options
61
- self.comparable_attributes = comparable_options_to_attributes_hash options
62
61
  end
63
62
 
64
63
  private
65
64
 
66
- def comparable_options_to_attributes_hash( options )
65
+ def comparable_options_to_attributes_hash( options )
66
+ options ||= {}
67
+
67
68
  #setup attributes
68
69
  attrs = {}
69
70
 
@@ -156,12 +157,14 @@ module ActiveRecord::Acts::Comparable
156
157
  diffs.symbolize_keys
157
158
  end
158
159
 
160
+ private
161
+
159
162
  # Returns an hash of comparable attribute mappings given the passed in options.
160
163
  #
161
164
  # == Arguments
162
165
  # * options - a hash of options. All options for ActiveRecord::Acts::Comparable::ClassMethods.acts_as_comparable apply.
163
166
  def comparable_attributes_for( options )
164
- attrs = options ? self.class.send( :comparable_options_to_attributes_hash, options ) : self.class.comparable_attributes
167
+ self.class.send( :comparable_options_to_attributes_hash, options || self.comparable_options )
165
168
  end
166
169
  end
167
170
 
@@ -2,29 +2,33 @@ require File.join( File.dirname( __FILE__ ), "test_helper")
2
2
 
3
3
  class ActsAsComparableTest < Test::Unit::TestCase
4
4
 
5
+ def setup
6
+ DummyModel.send :acts_as_comparable
7
+ end
8
+
5
9
  def test_models_are_different_when_both_records_are_new
6
- model1 = MockModel.new :name=>"dog"
7
- model2 = MockModel.new :name=>"cat"
10
+ model1 = DummyModel.new :name=>"dog"
11
+ model2 = DummyModel.new :name=>"cat"
8
12
 
9
13
  assert model1.different?(model2), "models should have been different!"
10
14
  assert !model1.same?(model2), "models shouldn't have been the same!"
11
15
  end
12
16
 
13
17
  def test_models_are_different_when_one_records_is_new
14
- model1 = MockModel.new :name=>"dog"
18
+ model1 = DummyModel.new :name=>"dog"
15
19
  model1.instance_variable_set "@new_record", false
16
20
 
17
- model2 = MockModel.new :name=>"cat"
21
+ model2 = DummyModel.new :name=>"cat"
18
22
 
19
23
  assert model1.different?(model2), "models should have been different!"
20
24
  assert !model1.same?(model2), "models shouldn't have been the same!"
21
25
  end
22
26
 
23
27
  def test_models_are_different_when_neither_record_is_new
24
- model1 = MockModel.new :name=>"dog"
28
+ model1 = DummyModel.new :name=>"dog"
25
29
  model1.instance_variable_set "@new_record", false
26
30
 
27
- model2 = MockModel.new :name=>"cat"
31
+ model2 = DummyModel.new :name=>"cat"
28
32
  model2.instance_variable_set "@new_record", false
29
33
 
30
34
  assert model1.different?(model2), "models should have been different!"
@@ -32,28 +36,28 @@ class ActsAsComparableTest < Test::Unit::TestCase
32
36
  end
33
37
 
34
38
  def test_models_are_not_different_when_both_records_are_new
35
- model1 = MockModel.new :name=>"cat"
36
- model2 = MockModel.new :name=>"cat"
39
+ model1 = DummyModel.new :name=>"cat"
40
+ model2 = DummyModel.new :name=>"cat"
37
41
 
38
42
  assert model1.same?(model2), "models should have been the same!"
39
43
  assert !model1.different?(model2), "models shouldn't have been different!"
40
44
  end
41
45
 
42
46
  def test_models_are_not_different_when_one_record_is_new
43
- model1 = MockModel.new :name=>"cat"
47
+ model1 = DummyModel.new :name=>"cat"
44
48
  model1.instance_variable_set "@new_record", false
45
49
 
46
- model2 = MockModel.new :name=>"cat"
50
+ model2 = DummyModel.new :name=>"cat"
47
51
 
48
52
  assert model1.same?(model2), "models should have been the same!"
49
53
  assert !model1.different?(model2), "models shouldn't have been different!"
50
54
  end
51
55
 
52
56
  def test_models_are_not_different_when_neither_record_is_new
53
- model1 = MockModel.new :name=>"cat"
57
+ model1 = DummyModel.new :name=>"cat"
54
58
  model1.instance_variable_set "@new_record", false
55
59
 
56
- model2 = MockModel.new :name=>"cat"
60
+ model2 = DummyModel.new :name=>"cat"
57
61
  model2.instance_variable_set "@new_record", false
58
62
 
59
63
  assert model1.same?(model2), "models should have been the same!"
@@ -61,20 +65,20 @@ class ActsAsComparableTest < Test::Unit::TestCase
61
65
  end
62
66
 
63
67
  def test_differences_with_new_records
64
- model1 = MockModel.new :id=>nil, :name=>"dog", :value=>"Tiny"
65
- model2 = MockModel.new :id=>nil, :name=>"cat", :value=>"Norm"
68
+ model1 = DummyModel.new :id=>nil, :name=>"dog", :value=>"Tiny"
69
+ model2 = DummyModel.new :id=>nil, :name=>"cat", :value=>"Norm"
66
70
  model1.instance_variable_set "@new_record", false
67
71
 
68
72
  differences = model1.differences(model2)
69
73
 
70
74
  assert differences[:name] == [ "dog", "cat" ], "Name was expected to be different!"
71
75
  assert differences[:value] == [ "Tiny", "Norm" ], "Value was expected to be different!"
72
- assert 2 == differences.size, "Wrong number of differences!"
76
+ assert_equal 2, differences.size, "Wrong number of differences!"
73
77
  end
74
78
 
75
79
  def test_differences_with_one_new_record
76
- model1 = MockModel.new :id=>nil, :name=>"dog", :value=>"Tiny"
77
- model2 = MockModel.new :id=>5, :name=>"cat", :value=>"Norm"
80
+ model1 = DummyModel.new :id=>nil, :name=>"dog", :value=>"Tiny"
81
+ model2 = DummyModel.new :id=>5, :name=>"cat", :value=>"Norm"
78
82
  model2.instance_variable_set "@new_record", false
79
83
 
80
84
  differences = model1.differences(model2)
@@ -82,14 +86,14 @@ class ActsAsComparableTest < Test::Unit::TestCase
82
86
  assert differences[:id] == [ nil, 5 ], "ID was expected to be different!"
83
87
  assert differences[:name] == [ "dog", "cat" ], "Name was expected to be different!"
84
88
  assert differences[:value] == [ "Tiny", "Norm" ], "Value was expected to be different!"
85
- assert 3 == differences.size, "Wrong number of differences!"
89
+ assert_equal 3, differences.size, "Wrong number of differences!"
86
90
  end
87
91
 
88
92
  def test_differences_with_existing_records
89
- model1 = MockModel.new :id=>1, :name=>"dog", :value=>"Tiny"
93
+ model1 = DummyModel.new :id=>1, :name=>"dog", :value=>"Tiny"
90
94
  model1.instance_variable_set "@new_record", false
91
95
 
92
- model2 = MockModel.new :id=>5, :name=>"cat", :value=>"Norm"
96
+ model2 = DummyModel.new :id=>5, :name=>"cat", :value=>"Norm"
93
97
  model1.instance_variable_set "@new_record", false
94
98
 
95
99
  differences = model1.differences(model2)
@@ -97,40 +101,37 @@ class ActsAsComparableTest < Test::Unit::TestCase
97
101
  assert differences[:id] == [ 1, 5 ], "ID was expected to be different!"
98
102
  assert differences[:name] == [ "dog", "cat" ], "Name was expected to be different!"
99
103
  assert differences[:value] == [ "Tiny", "Norm" ], "Value was expected to be different!"
100
- assert 3 == differences.size, "Wrong number of differences!"
104
+ assert_equal 3, differences.size, "Wrong number of differences!"
101
105
  end
102
106
 
103
- def test_comparable_attributes_for_without_options
104
- comparable_attributes = MockModel.new.comparable_attributes_for( {} )
105
- expected_comparable_attributes = { :id=>:id, :name=>:name, :value=>:value }
107
+ def test_differences_when_using_only_option
108
+ DummyModel.send :acts_as_comparable, :only=>[:name]
109
+
110
+ model1 = DummyModel.new :id=>1, :name=>"dog", :value=>"Tiny"
111
+ model1.instance_variable_set "@new_record", false
106
112
 
107
- assert expected_comparable_attributes == comparable_attributes, "Expected attributes were not comparable!"
108
- end
109
-
110
- def test_comparable_attributes_for_with_only_option
111
- comparable_attributes = MockModel.new.comparable_attributes_for(
112
- :only=>[:value]
113
- )
114
- expected_comparable_attributes = { :value=>:value }
113
+ model2 = DummyModel.new :id=>5, :name=>"cat", :value=>"Norm"
114
+ model1.instance_variable_set "@new_record", false
115
115
 
116
- assert expected_comparable_attributes == comparable_attributes, "Expected attributes were not comparable!"
117
- end
118
-
119
- def test_comparable_attributes_for_with_except_option
120
- comparable_attributes = MockModel.new.comparable_attributes_for(
121
- :except=>[:value]
122
- )
123
- expected_comparable_attributes = { :id=>:id, :name=>:name }
116
+ differences = model1.differences(model2)
124
117
 
125
- assert expected_comparable_attributes == comparable_attributes, "Expected attributes were not comparable!"
118
+ assert differences[:name] == [ "dog", "cat" ], "Name was expected to be different!"
119
+ assert_equal 1, differences.size, "Wrong number of differences!"
126
120
  end
127
121
 
128
- def test_comparable_attributes_for_with_attrs_map_option
129
- comparable_attributes = MockModel.new.comparable_attributes_for(
130
- :attrs_map=> {:name=>:value, :value=>:id}
131
- )
132
- expected_comparable_attributes = { :id=>:id, :name=>:value, :value=>:id }
133
- assert expected_comparable_attributes == comparable_attributes, "Expected attributes were not comparable!"
134
- end
122
+ def test_differences_when_using_except_option
123
+ DummyModel.send :acts_as_comparable, :except=>[:id, :value]
124
+
125
+ model1 = DummyModel.new :id=>1, :name=>"dog", :value=>"Tiny"
126
+ model1.instance_variable_set "@new_record", false
127
+
128
+ model2 = DummyModel.new :id=>5, :name=>"cat", :value=>"Norm"
129
+ model1.instance_variable_set "@new_record", false
130
+
131
+ differences = model1.differences(model2)
132
+
133
+ assert differences[:name] == [ "dog", "cat" ], "Name was expected to be different!"
134
+ assert_equal 1, differences.size, "Wrong number of differences!"
135
+ end
135
136
 
136
137
  end
data/test/test_helper.rb CHANGED
@@ -13,7 +13,7 @@ require 'mocha'
13
13
 
14
14
  ###### SETUP a partially mocked out Model for testing #######
15
15
 
16
- class MockModel < ActiveRecord::Base
16
+ class DummyModel < ActiveRecord::Base
17
17
  class << self
18
18
  def column_names
19
19
  %w[ id name value ]
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: acts_as_comparable
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.1"
6
+ version: "1.2"
7
7
  date: 2007-04-20 00:00:00 -04:00
8
8
  summary: Adds ActiveRecord model comparison functionality.
9
9
  require_paths: