mongoid-history 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,240 +1,240 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- class MyModel
4
- include Mongoid::Document
5
- include Mongoid::History::Trackable
6
- field :foo
7
- end
8
-
9
- class HistoryTracker
10
- include Mongoid::History::Tracker
11
- end
12
-
13
- describe Mongoid::History::Trackable do
14
- it "should have #track_history" do
15
- MyModel.should respond_to :track_history
16
- end
17
-
18
- it "should append trackable_class_options ONLY when #track_history is called" do
19
- Mongoid::History.trackable_class_options.should be_blank
20
- MyModel.track_history
21
- Mongoid::History.trackable_class_options.keys.should == [:my_model]
22
- end
23
-
24
- describe "#track_history" do
25
- before :all do
26
- MyModel.track_history
27
- @persisted_history_options = Mongoid::History.trackable_class_options
28
- end
29
- before(:each){ Mongoid::History.trackable_class_options = @persisted_history_options }
30
- let(:expected_option) do
31
- { :on => :all,
32
- :modifier_field => :modifier,
33
- :version_field => :version,
34
- :changes_method => :changes,
35
- :scope => :my_model,
36
- :except => ["created_at", "updated_at"],
37
- :track_create => false,
38
- :track_update => true,
39
- :track_destroy => false }
40
- end
41
- let(:regular_fields){ ["foo"] }
42
- let(:reserved_fields){ ["_id", "version", "modifier_id"] }
43
-
44
- it "should have default options" do
45
- Mongoid::History.trackable_class_options[:my_model].should == expected_option
46
- end
47
-
48
- it "should define callback function #track_update" do
49
- MyModel.new.private_methods.collect(&:to_sym).should include(:track_update)
50
- end
51
-
52
- it "should define callback function #track_create" do
53
- MyModel.new.private_methods.collect(&:to_sym).should include(:track_create)
54
- end
55
-
56
- it "should define callback function #track_destroy" do
57
- MyModel.new.private_methods.collect(&:to_sym).should include(:track_destroy)
58
- end
59
-
60
- it "should define #history_trackable_options" do
61
- MyModel.history_trackable_options.should == expected_option
62
- end
63
-
64
- describe "#tracked_fields" do
65
- it "should return the tracked field list" do
66
- MyModel.tracked_fields.should == regular_fields
67
- end
68
- end
69
-
70
- describe "#reserved_tracked_fields" do
71
- it "should return the protected field list" do
72
- MyModel.reserved_tracked_fields.should == reserved_fields
73
- end
74
- end
75
-
76
- describe "#tracked_fields_for_action" do
77
- it "should include the reserved fields for destroy" do
78
- MyModel.tracked_fields_for_action(:destroy).should == regular_fields + reserved_fields
79
- end
80
- it "should not include the reserved fields for update" do
81
- MyModel.tracked_fields_for_action(:update).should == regular_fields
82
- end
83
- it "should not include the reserved fields for create" do
84
- MyModel.tracked_fields_for_action(:create).should == regular_fields
85
- end
86
- end
87
-
88
- describe "#tracked_field?" do
89
- it "should not include the reserved fields by default" do
90
- MyModel.tracked_field?(:_id).should be_false
91
- end
92
- it "should include the reserved fields for destroy" do
93
- MyModel.tracked_field?(:_id, :destroy).should be_true
94
- end
95
- it "should allow field aliases" do
96
- MyModel.tracked_field?(:id, :destroy).should be_true
97
- end
98
- end
99
-
100
- context "sub-model" do
101
- before :each do
102
- class MySubModel < MyModel
103
- end
104
- end
105
-
106
- it "should have default options" do
107
- Mongoid::History.trackable_class_options[:my_model].should == expected_option
108
- end
109
-
110
- it "should define #history_trackable_options" do
111
- MySubModel.history_trackable_options.should == expected_option
112
- end
113
- end
114
-
115
- describe "#track_history?" do
116
-
117
- context "when tracking is globally enabled" do
118
-
119
- it "should be enabled on the current thread" do
120
- Mongoid::History.enabled?.should == true
121
- MyModel.new.track_history?.should == true
122
- end
123
-
124
- it "should be disabled within disable_tracking" do
125
- MyModel.disable_tracking do
126
- Mongoid::History.enabled?.should == true
127
- MyModel.new.track_history?.should == false
128
- end
129
- end
130
-
131
- it "should be rescued if an exception occurs" do
132
- begin
133
- MyModel.disable_tracking do
134
- raise "exception"
135
- end
136
- rescue
137
- end
138
- Mongoid::History.enabled?.should == true
139
- MyModel.new.track_history?.should == true
140
- end
141
-
142
- it "should be disabled only for the class that calls disable_tracking" do
143
- class MyModel2
144
- include Mongoid::Document
145
- include Mongoid::History::Trackable
146
- track_history
147
- end
148
-
149
- MyModel.disable_tracking do
150
- Mongoid::History.enabled?.should == true
151
- MyModel2.new.track_history?.should == true
152
- end
153
- end
154
- end
155
-
156
- context "when tracking is globally disabled" do
157
-
158
- around(:each) do |example|
159
- Mongoid::History.disable do
160
- example.run
161
- end
162
- end
163
-
164
- it "should be disabled by the global disablement" do
165
- Mongoid::History.enabled?.should == false
166
- MyModel.new.track_history?.should == false
167
- end
168
-
169
- it "should be disabled within disable_tracking" do
170
- MyModel.disable_tracking do
171
- Mongoid::History.enabled?.should == false
172
- MyModel.new.track_history?.should == false
173
- end
174
- end
175
-
176
- it "should be rescued if an exception occurs" do
177
- begin
178
- MyModel.disable_tracking do
179
- raise "exception"
180
- end
181
- rescue
182
- end
183
- Mongoid::History.enabled?.should == false
184
- MyModel.new.track_history?.should == false
185
- end
186
-
187
- it "should be disabled only for the class that calls disable_tracking" do
188
- class MyModel2
189
- include Mongoid::Document
190
- include Mongoid::History::Trackable
191
- track_history
192
- end
193
-
194
- MyModel.disable_tracking do
195
- Mongoid::History.enabled?.should == false
196
- MyModel2.new.track_history?.should == false
197
- end
198
- end
199
- end
200
-
201
- it "should rescue errors through both local and global tracking scopes" do
202
- begin
203
- Mongoid::History.disable do
204
- MyModel.disable_tracking do
205
- raise "exception"
206
- end
207
- end
208
- rescue
209
- end
210
- Mongoid::History.enabled?.should == true
211
- MyModel.new.track_history?.should == true
212
- end
213
- end
214
-
215
- describe ":changes_method" do
216
-
217
- it "should default to :changes" do
218
- m = MyModel.create
219
- m.should_receive(:changes).exactly(3).times.and_call_original
220
- m.should_not_receive(:my_changes)
221
- m.save
222
- end
223
-
224
- it "should allow an alternate method to be specified" do
225
- class MyModel3 < MyModel
226
- track_history :changes_method => :my_changes
227
-
228
- def my_changes
229
- {}
230
- end
231
- end
232
-
233
- m = MyModel3.create
234
- m.should_receive(:changes).twice.and_call_original
235
- m.should_receive(:my_changes).once.and_call_original
236
- m.save
237
- end
238
- end
239
- end
240
- end
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class MyModel
4
+ include Mongoid::Document
5
+ include Mongoid::History::Trackable
6
+ field :foo
7
+ end
8
+
9
+ class HistoryTracker
10
+ include Mongoid::History::Tracker
11
+ end
12
+
13
+ describe Mongoid::History::Trackable do
14
+ it "should have #track_history" do
15
+ MyModel.should respond_to :track_history
16
+ end
17
+
18
+ it "should append trackable_class_options ONLY when #track_history is called" do
19
+ Mongoid::History.trackable_class_options.should be_blank
20
+ MyModel.track_history
21
+ Mongoid::History.trackable_class_options.keys.should == [:my_model]
22
+ end
23
+
24
+ describe "#track_history" do
25
+ before :all do
26
+ MyModel.track_history
27
+ @persisted_history_options = Mongoid::History.trackable_class_options
28
+ end
29
+ before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
30
+ let(:expected_option) do
31
+ { on: :all,
32
+ modifier_field: :modifier,
33
+ version_field: :version,
34
+ changes_method: :changes,
35
+ scope: :my_model,
36
+ except: ["created_at", "updated_at"],
37
+ track_create: false,
38
+ track_update: true,
39
+ track_destroy: false }
40
+ end
41
+ let(:regular_fields) { ["foo"] }
42
+ let(:reserved_fields) { ["_id", "version", "modifier_id"] }
43
+
44
+ it "should have default options" do
45
+ Mongoid::History.trackable_class_options[:my_model].should == expected_option
46
+ end
47
+
48
+ it "should define callback function #track_update" do
49
+ MyModel.new.private_methods.collect(&:to_sym).should include(:track_update)
50
+ end
51
+
52
+ it "should define callback function #track_create" do
53
+ MyModel.new.private_methods.collect(&:to_sym).should include(:track_create)
54
+ end
55
+
56
+ it "should define callback function #track_destroy" do
57
+ MyModel.new.private_methods.collect(&:to_sym).should include(:track_destroy)
58
+ end
59
+
60
+ it "should define #history_trackable_options" do
61
+ MyModel.history_trackable_options.should == expected_option
62
+ end
63
+
64
+ describe "#tracked_fields" do
65
+ it "should return the tracked field list" do
66
+ MyModel.tracked_fields.should == regular_fields
67
+ end
68
+ end
69
+
70
+ describe "#reserved_tracked_fields" do
71
+ it "should return the protected field list" do
72
+ MyModel.reserved_tracked_fields.should == reserved_fields
73
+ end
74
+ end
75
+
76
+ describe "#tracked_fields_for_action" do
77
+ it "should include the reserved fields for destroy" do
78
+ MyModel.tracked_fields_for_action(:destroy).should == regular_fields + reserved_fields
79
+ end
80
+ it "should not include the reserved fields for update" do
81
+ MyModel.tracked_fields_for_action(:update).should == regular_fields
82
+ end
83
+ it "should not include the reserved fields for create" do
84
+ MyModel.tracked_fields_for_action(:create).should == regular_fields
85
+ end
86
+ end
87
+
88
+ describe "#tracked_field?" do
89
+ it "should not include the reserved fields by default" do
90
+ MyModel.tracked_field?(:_id).should be_false
91
+ end
92
+ it "should include the reserved fields for destroy" do
93
+ MyModel.tracked_field?(:_id, :destroy).should be_true
94
+ end
95
+ it "should allow field aliases" do
96
+ MyModel.tracked_field?(:id, :destroy).should be_true
97
+ end
98
+ end
99
+
100
+ context "sub-model" do
101
+ before :each do
102
+ class MySubModel < MyModel
103
+ end
104
+ end
105
+
106
+ it "should have default options" do
107
+ Mongoid::History.trackable_class_options[:my_model].should == expected_option
108
+ end
109
+
110
+ it "should define #history_trackable_options" do
111
+ MySubModel.history_trackable_options.should == expected_option
112
+ end
113
+ end
114
+
115
+ describe "#track_history?" do
116
+
117
+ context "when tracking is globally enabled" do
118
+
119
+ it "should be enabled on the current thread" do
120
+ Mongoid::History.enabled?.should == true
121
+ MyModel.new.track_history?.should == true
122
+ end
123
+
124
+ it "should be disabled within disable_tracking" do
125
+ MyModel.disable_tracking do
126
+ Mongoid::History.enabled?.should == true
127
+ MyModel.new.track_history?.should == false
128
+ end
129
+ end
130
+
131
+ it "should be rescued if an exception occurs" do
132
+ begin
133
+ MyModel.disable_tracking do
134
+ raise "exception"
135
+ end
136
+ rescue
137
+ end
138
+ Mongoid::History.enabled?.should == true
139
+ MyModel.new.track_history?.should == true
140
+ end
141
+
142
+ it "should be disabled only for the class that calls disable_tracking" do
143
+ class MyModel2
144
+ include Mongoid::Document
145
+ include Mongoid::History::Trackable
146
+ track_history
147
+ end
148
+
149
+ MyModel.disable_tracking do
150
+ Mongoid::History.enabled?.should == true
151
+ MyModel2.new.track_history?.should == true
152
+ end
153
+ end
154
+ end
155
+
156
+ context "when tracking is globally disabled" do
157
+
158
+ around(:each) do |example|
159
+ Mongoid::History.disable do
160
+ example.run
161
+ end
162
+ end
163
+
164
+ it "should be disabled by the global disablement" do
165
+ Mongoid::History.enabled?.should == false
166
+ MyModel.new.track_history?.should == false
167
+ end
168
+
169
+ it "should be disabled within disable_tracking" do
170
+ MyModel.disable_tracking do
171
+ Mongoid::History.enabled?.should == false
172
+ MyModel.new.track_history?.should == false
173
+ end
174
+ end
175
+
176
+ it "should be rescued if an exception occurs" do
177
+ begin
178
+ MyModel.disable_tracking do
179
+ raise "exception"
180
+ end
181
+ rescue
182
+ end
183
+ Mongoid::History.enabled?.should == false
184
+ MyModel.new.track_history?.should == false
185
+ end
186
+
187
+ it "should be disabled only for the class that calls disable_tracking" do
188
+ class MyModel2
189
+ include Mongoid::Document
190
+ include Mongoid::History::Trackable
191
+ track_history
192
+ end
193
+
194
+ MyModel.disable_tracking do
195
+ Mongoid::History.enabled?.should == false
196
+ MyModel2.new.track_history?.should == false
197
+ end
198
+ end
199
+ end
200
+
201
+ it "should rescue errors through both local and global tracking scopes" do
202
+ begin
203
+ Mongoid::History.disable do
204
+ MyModel.disable_tracking do
205
+ raise "exception"
206
+ end
207
+ end
208
+ rescue
209
+ end
210
+ Mongoid::History.enabled?.should == true
211
+ MyModel.new.track_history?.should == true
212
+ end
213
+ end
214
+
215
+ describe ":changes_method" do
216
+
217
+ it "should default to :changes" do
218
+ m = MyModel.create
219
+ m.should_receive(:changes).exactly(3).times.and_call_original
220
+ m.should_not_receive(:my_changes)
221
+ m.save
222
+ end
223
+
224
+ it "should allow an alternate method to be specified" do
225
+ class MyModel3 < MyModel
226
+ track_history changes_method: :my_changes
227
+
228
+ def my_changes
229
+ {}
230
+ end
231
+ end
232
+
233
+ m = MyModel3.create
234
+ m.should_receive(:changes).twice.and_call_original
235
+ m.should_receive(:my_changes).once.and_call_original
236
+ m.save
237
+ end
238
+ end
239
+ end
240
+ end
data/spec/tracker_spec.rb CHANGED
@@ -1,10 +1,10 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe Mongoid::History::Tracker do
4
- it "should set tracker_class_name when included" do
5
- class MyTracker
6
- include Mongoid::History::Tracker
7
- end
8
- Mongoid::History.tracker_class_name.should == :my_tracker
9
- end
10
- end
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Mongoid::History::Tracker do
4
+ it "should set tracker_class_name when included" do
5
+ class MyTracker
6
+ include Mongoid::History::Tracker
7
+ end
8
+ Mongoid::History.tracker_class_name.should == :my_tracker
9
+ end
10
+ end