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 +10 -0
- data/README +4 -0
- data/lib/acts_as_comparable/acts_as_comparable.rb +7 -4
- data/test/acts_as_comparable_test.rb +49 -48
- data/test/test_helper.rb +1 -1
- metadata +1 -1
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
@@ -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 :
|
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
|
-
|
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 =
|
7
|
-
model2 =
|
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 =
|
18
|
+
model1 = DummyModel.new :name=>"dog"
|
15
19
|
model1.instance_variable_set "@new_record", false
|
16
20
|
|
17
|
-
model2 =
|
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 =
|
28
|
+
model1 = DummyModel.new :name=>"dog"
|
25
29
|
model1.instance_variable_set "@new_record", false
|
26
30
|
|
27
|
-
model2 =
|
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 =
|
36
|
-
model2 =
|
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 =
|
47
|
+
model1 = DummyModel.new :name=>"cat"
|
44
48
|
model1.instance_variable_set "@new_record", false
|
45
49
|
|
46
|
-
model2 =
|
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 =
|
57
|
+
model1 = DummyModel.new :name=>"cat"
|
54
58
|
model1.instance_variable_set "@new_record", false
|
55
59
|
|
56
|
-
model2 =
|
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 =
|
65
|
-
model2 =
|
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
|
-
|
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 =
|
77
|
-
model2 =
|
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
|
-
|
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 =
|
93
|
+
model1 = DummyModel.new :id=>1, :name=>"dog", :value=>"Tiny"
|
90
94
|
model1.instance_variable_set "@new_record", false
|
91
95
|
|
92
|
-
model2 =
|
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
|
-
|
104
|
+
assert_equal 3, differences.size, "Wrong number of differences!"
|
101
105
|
end
|
102
106
|
|
103
|
-
def
|
104
|
-
|
105
|
-
|
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
|
-
|
108
|
-
|
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
|
-
|
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
|
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
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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
metadata
CHANGED