couch_potato 1.4.0 → 1.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.travis.yml +12 -8
- data/CHANGES.md +4 -0
- data/Gemfile +1 -1
- data/README.md +396 -276
- data/Rakefile +9 -9
- data/couch_potato-rspec.gemspec +20 -0
- data/couch_potato.gemspec +15 -16
- data/{active_support_4_0 → gemfiles/active_support_4_0} +3 -3
- data/{active_support_3_2 → gemfiles/active_support_4_1} +3 -2
- data/gemfiles/active_support_4_2 +11 -0
- data/lib/couch_potato-rspec.rb +3 -0
- data/lib/couch_potato.rb +3 -1
- data/lib/couch_potato/database.rb +42 -39
- data/lib/couch_potato/persistence/magic_timestamps.rb +5 -5
- data/lib/couch_potato/persistence/properties.rb +8 -2
- data/lib/couch_potato/persistence/simple_property.rb +11 -9
- data/lib/couch_potato/persistence/type_caster.rb +1 -1
- data/lib/couch_potato/railtie.rb +2 -0
- data/lib/couch_potato/version.rb +2 -1
- data/lib/couch_potato/view/base_view_spec.rb +18 -8
- data/lib/couch_potato/view/view_query.rb +2 -3
- data/spec/attachments_spec.rb +3 -3
- data/spec/callbacks_spec.rb +193 -113
- data/spec/conflict_handling_spec.rb +4 -4
- data/spec/create_spec.rb +5 -5
- data/spec/default_property_spec.rb +6 -6
- data/spec/destroy_spec.rb +5 -5
- data/spec/property_spec.rb +71 -61
- data/spec/rails_spec.rb +3 -3
- data/spec/railtie_spec.rb +12 -13
- data/spec/spec_helper.rb +3 -3
- data/spec/unit/active_model_compliance_spec.rb +16 -16
- data/spec/unit/attributes_spec.rb +36 -34
- data/spec/unit/base_view_spec_spec.rb +82 -35
- data/spec/unit/callbacks_spec.rb +2 -2
- data/spec/unit/couch_potato_spec.rb +3 -3
- data/spec/unit/create_spec.rb +12 -12
- data/spec/unit/custom_views_spec.rb +1 -1
- data/spec/unit/database_spec.rb +95 -95
- data/spec/unit/date_spec.rb +3 -3
- data/spec/unit/deep_dirty_attributes_spec.rb +104 -104
- data/spec/unit/dirty_attributes_spec.rb +19 -19
- data/spec/unit/forbidden_attributes_protection_spec.rb +4 -4
- data/spec/unit/initialize_spec.rb +37 -19
- data/spec/unit/json_spec.rb +4 -4
- data/spec/unit/lists_spec.rb +8 -8
- data/spec/unit/model_view_spec_spec.rb +14 -14
- data/spec/unit/persistence_spec.rb +6 -6
- data/spec/unit/properties_view_spec_spec.rb +4 -4
- data/spec/unit/rspec_matchers_spec.rb +73 -73
- data/spec/unit/rspec_stub_db_spec.rb +43 -42
- data/spec/unit/string_spec.rb +1 -1
- data/spec/unit/time_spec.rb +2 -2
- data/spec/unit/validation_spec.rb +1 -1
- data/spec/unit/view_query_spec.rb +54 -59
- data/spec/update_spec.rb +5 -5
- data/spec/view_updates_spec.rb +4 -4
- data/spec/views_spec.rb +43 -43
- metadata +18 -22
- data/lib/couch_potato/rspec.rb +0 -2
- data/lib/couch_potato/rspec/matchers.rb +0 -56
- data/lib/couch_potato/rspec/matchers/json2.js +0 -482
- data/lib/couch_potato/rspec/matchers/list_as_matcher.rb +0 -53
- data/lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb +0 -166
- data/lib/couch_potato/rspec/matchers/map_to_matcher.rb +0 -61
- data/lib/couch_potato/rspec/matchers/reduce_to_matcher.rb +0 -48
- data/lib/couch_potato/rspec/stub_db.rb +0 -57
@@ -14,9 +14,11 @@ module CouchPotato
|
|
14
14
|
|
15
15
|
assert_valid_view_parameters normalized_view_parameters
|
16
16
|
@klass = klass
|
17
|
-
@design_document = translate_to_design_doc_name(klass.to_s, view_name, @list_name)
|
18
|
-
@view_name = view_name
|
19
17
|
@options = options
|
18
|
+
@view_name = compute_view_name(view_name,
|
19
|
+
options.key?(:digest_view_name) ? options[:digest_view_name] : Config.digest_view_names)
|
20
|
+
@design_document = translate_to_design_doc_name(klass.to_s, @view_name, @list_name)
|
21
|
+
@list_params = normalized_view_parameters.delete :list_params
|
20
22
|
|
21
23
|
@list_function = klass.lists(@list_name) if @list_name
|
22
24
|
@view_parameters = {}
|
@@ -24,6 +26,7 @@ module CouchPotato
|
|
24
26
|
@view_parameters[key] = options[key] if options.include?(key)
|
25
27
|
end
|
26
28
|
@view_parameters.merge!(normalized_view_parameters)
|
29
|
+
@view_parameters.merge!(@list_params) if @list_params
|
27
30
|
end
|
28
31
|
|
29
32
|
def process_results(results)
|
@@ -36,8 +39,15 @@ module CouchPotato
|
|
36
39
|
|
37
40
|
private
|
38
41
|
|
42
|
+
def compute_view_name(view_name, digest)
|
43
|
+
if digest
|
44
|
+
"#{view_name}-#{Digest::MD5.hexdigest(map_function + reduce_function.to_s)}"
|
45
|
+
else
|
46
|
+
view_name
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
39
50
|
def normalize_view_parameters(params)
|
40
|
-
normalized_params = params.dup
|
41
51
|
hash = wrap_in_hash params
|
42
52
|
remove_nil_stale(replace_range_key(hash))
|
43
53
|
end
|
@@ -65,19 +75,19 @@ module CouchPotato
|
|
65
75
|
|
66
76
|
def assert_valid_view_parameters(params)
|
67
77
|
params.keys.each do |key|
|
68
|
-
|
78
|
+
fail ArgumentError, "invalid view parameter: #{key}" unless valid_view_parameters.include?(key.to_s)
|
69
79
|
end
|
70
80
|
end
|
71
81
|
|
72
82
|
def valid_view_parameters
|
73
|
-
%w(key keys startkey startkey_docid endkey endkey_docid limit stale descending skip group group_level reduce include_docs inclusive_end)
|
83
|
+
%w(list_params key keys startkey startkey_docid endkey endkey_docid limit stale descending skip group group_level reduce include_docs inclusive_end)
|
74
84
|
end
|
75
85
|
|
76
86
|
def translate_to_design_doc_name(klass_name, view_name, list_name)
|
77
87
|
klass_name = klass_name.dup
|
78
|
-
klass_name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
79
|
-
klass_name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
80
|
-
klass_name.tr!(
|
88
|
+
klass_name.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
89
|
+
klass_name.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
90
|
+
klass_name.tr!('-', '_')
|
81
91
|
doc_name = klass_name.downcase
|
82
92
|
|
83
93
|
if CouchPotato::Config.split_design_documents_per_view
|
@@ -20,7 +20,7 @@ module CouchPotato
|
|
20
20
|
update_view unless view_has_been_updated?
|
21
21
|
begin
|
22
22
|
query_view parameters
|
23
|
-
rescue
|
23
|
+
rescue CouchRest::NotFound
|
24
24
|
update_view
|
25
25
|
retry
|
26
26
|
end
|
@@ -82,7 +82,7 @@ module CouchPotato
|
|
82
82
|
|
83
83
|
def query_view(parameters)
|
84
84
|
if @list_name
|
85
|
-
|
85
|
+
@database.connection.get CouchRest.paramify_url("/#{@database.name}/_design/#{@design_document_name}/_list/#{@list_name}/#{@view_name}", parameters)
|
86
86
|
else
|
87
87
|
@database.view view_url, parameters
|
88
88
|
end
|
@@ -91,7 +91,6 @@ module CouchPotato
|
|
91
91
|
def view_url
|
92
92
|
"#{@design_document_name}/#{@view_name}"
|
93
93
|
end
|
94
|
-
|
95
94
|
end
|
96
95
|
end
|
97
96
|
end
|
data/spec/attachments_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe CouchPotato, 'attachments' do
|
|
5
5
|
comment = Comment.new :title => 'nil'
|
6
6
|
comment._attachments['body'] = {'data' => 'a useful comment', 'content_type' => 'text/plain'}
|
7
7
|
CouchPotato.database.save! comment
|
8
|
-
CouchPotato.couchrest_database.fetch_attachment(comment.to_hash, 'body').to_s.
|
8
|
+
expect(CouchPotato.couchrest_database.fetch_attachment(comment.to_hash, 'body').to_s).to eq('a useful comment')
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should give me information about the attachments of a document" do
|
@@ -13,11 +13,11 @@ describe CouchPotato, 'attachments' do
|
|
13
13
|
comment._attachments = {'body' => {'data' => 'a useful comment', 'content_type' => 'text/plain'}}
|
14
14
|
CouchPotato.database.save! comment
|
15
15
|
comment_reloaded = CouchPotato.database.load comment.id
|
16
|
-
comment_reloaded._attachments["body"].
|
16
|
+
expect(comment_reloaded._attachments["body"]).to include({"content_type" => "text/plain", "stub" => true, "length" => 16})
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should have an empty array for a new object" do
|
20
|
-
Comment.new._attachments.
|
20
|
+
expect(Comment.new._attachments).to eq({})
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
data/spec/callbacks_spec.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class CallbackRecorderWithNoRequiredProperties
|
4
4
|
include CouchPotato::Persistence
|
5
|
-
|
5
|
+
|
6
6
|
property :required_property
|
7
|
-
|
8
|
-
validates_presence_of :required_property
|
9
|
-
|
7
|
+
|
10
8
|
[:before_validation, :before_validation_on_create,
|
11
|
-
:before_validation_on_save, :before_validation_on_update,
|
9
|
+
:before_validation_on_save, :before_validation_on_update,
|
12
10
|
:before_save, :before_create, :before_create,
|
13
11
|
:after_save, :after_create, :after_create,
|
14
12
|
:before_update, :after_update,
|
@@ -19,19 +17,23 @@ class CallbackRecorder
|
|
19
17
|
end
|
20
18
|
self.send callback, callback
|
21
19
|
end
|
22
|
-
|
20
|
+
|
23
21
|
view :all, :key => :required_property
|
24
|
-
|
22
|
+
|
25
23
|
def callbacks
|
26
24
|
@callbacks ||= []
|
27
25
|
end
|
28
|
-
|
26
|
+
|
29
27
|
private
|
30
|
-
|
28
|
+
|
31
29
|
def method_callback_with_argument(db)
|
32
30
|
db.view CallbackRecorder.all
|
33
31
|
end
|
34
|
-
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class CallbackRecorder < CallbackRecorderWithNoRequiredProperties
|
36
|
+
validates_presence_of :required_property
|
35
37
|
end
|
36
38
|
|
37
39
|
describe "multiple callbacks at once" do
|
@@ -39,224 +41,302 @@ describe "multiple callbacks at once" do
|
|
39
41
|
class Monkey
|
40
42
|
include CouchPotato::Persistence
|
41
43
|
attr_accessor :eaten_banana, :eaten_apple
|
42
|
-
|
44
|
+
|
43
45
|
before_create :eat_apple, :eat_banana
|
44
|
-
|
46
|
+
|
45
47
|
private
|
46
|
-
|
48
|
+
|
47
49
|
def eat_banana
|
48
50
|
self.eaten_banana = true
|
49
51
|
end
|
50
|
-
|
52
|
+
|
51
53
|
def eat_apple
|
52
54
|
self.eaten_apple = true
|
53
55
|
end
|
54
56
|
end
|
55
|
-
|
57
|
+
|
56
58
|
it "should run all callback methods given to the callback method call" do
|
57
59
|
monkey = Monkey.new
|
58
60
|
monkey.run_callbacks :create
|
59
|
-
monkey.eaten_banana.
|
60
|
-
monkey.eaten_apple.
|
61
|
+
expect(monkey.eaten_banana).to be_truthy
|
62
|
+
expect(monkey.eaten_apple).to be_truthy
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
64
66
|
describe 'create callbacks' do
|
65
|
-
|
67
|
+
|
66
68
|
before(:each) do
|
67
69
|
@recorder = CallbackRecorder.new
|
68
|
-
couchrest_database =
|
70
|
+
couchrest_database = double 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}, :view => {'rows' => []}, :info => nil
|
69
71
|
@db = CouchPotato::Database.new(couchrest_database)
|
70
72
|
end
|
71
|
-
|
73
|
+
|
72
74
|
describe "successful create" do
|
73
75
|
before(:each) do
|
74
76
|
@recorder.required_property = 1
|
75
77
|
end
|
76
|
-
|
78
|
+
|
79
|
+
it "should call before_validation" do
|
80
|
+
@recorder.valid?
|
81
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should call before_validation_on_create" do
|
85
|
+
@db.save_document! @recorder
|
86
|
+
expect(@recorder.callbacks).to include(:before_validation_on_create)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should call before_validation_on_save" do
|
90
|
+
@db.save_document! @recorder
|
91
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should call before_save" do
|
95
|
+
@db.save_document! @recorder
|
96
|
+
expect(@recorder.callbacks).to include(:before_save)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should call after_save" do
|
100
|
+
@db.save_document! @recorder
|
101
|
+
expect(@recorder.callbacks).to include(:after_save)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should call before_create" do
|
105
|
+
@db.save_document! @recorder
|
106
|
+
expect(@recorder.callbacks).to include(:before_create)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should call after_create" do
|
110
|
+
@db.save_document! @recorder
|
111
|
+
expect(@recorder.callbacks).to include(:after_create)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "successful create with no changes (object not dirty)" do
|
116
|
+
before(:each) do
|
117
|
+
@recorder = CallbackRecorderWithNoRequiredProperties.new
|
118
|
+
end
|
119
|
+
|
77
120
|
it "should call before_validation" do
|
78
121
|
@recorder.valid?
|
79
|
-
@recorder.callbacks.
|
122
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
80
123
|
end
|
81
|
-
|
124
|
+
|
82
125
|
it "should call before_validation_on_create" do
|
83
126
|
@db.save_document! @recorder
|
84
|
-
@recorder.callbacks.
|
127
|
+
expect(@recorder.callbacks).to include(:before_validation_on_create)
|
85
128
|
end
|
86
|
-
|
129
|
+
|
87
130
|
it "should call before_validation_on_save" do
|
88
131
|
@db.save_document! @recorder
|
89
|
-
@recorder.callbacks.
|
132
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
90
133
|
end
|
91
|
-
|
134
|
+
|
92
135
|
it "should call before_save" do
|
93
136
|
@db.save_document! @recorder
|
94
|
-
@recorder.callbacks.
|
137
|
+
expect(@recorder.callbacks).to include(:before_save)
|
95
138
|
end
|
96
|
-
|
139
|
+
|
97
140
|
it "should call after_save" do
|
98
141
|
@db.save_document! @recorder
|
99
|
-
@recorder.callbacks.
|
142
|
+
expect(@recorder.callbacks).to include(:after_save)
|
100
143
|
end
|
101
|
-
|
144
|
+
|
102
145
|
it "should call before_create" do
|
103
146
|
@db.save_document! @recorder
|
104
|
-
@recorder.callbacks.
|
147
|
+
expect(@recorder.callbacks).to include(:before_create)
|
105
148
|
end
|
106
|
-
|
149
|
+
|
107
150
|
it "should call after_create" do
|
108
151
|
@db.save_document! @recorder
|
109
|
-
@recorder.callbacks.
|
152
|
+
expect(@recorder.callbacks).to include(:after_create)
|
110
153
|
end
|
111
|
-
|
154
|
+
|
112
155
|
end
|
113
|
-
|
156
|
+
|
114
157
|
describe "failed create" do
|
115
|
-
|
158
|
+
|
116
159
|
it "should call before_validation" do
|
117
160
|
@recorder.valid?
|
118
|
-
@recorder.callbacks.
|
161
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
119
162
|
end
|
120
|
-
|
163
|
+
|
121
164
|
it "should call before_validation_on_create" do
|
122
165
|
@db.save_document @recorder
|
123
|
-
@recorder.callbacks.
|
166
|
+
expect(@recorder.callbacks).to include(:before_validation_on_create)
|
124
167
|
end
|
125
|
-
|
168
|
+
|
126
169
|
it "should call before_validation_on_save" do
|
127
170
|
@db.save_document @recorder
|
128
|
-
@recorder.callbacks.
|
171
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
129
172
|
end
|
130
|
-
|
173
|
+
|
131
174
|
it "should not call before_save" do
|
132
175
|
@db.save_document @recorder
|
133
|
-
@recorder.callbacks.
|
176
|
+
expect(@recorder.callbacks).not_to include(:before_save)
|
134
177
|
end
|
135
|
-
|
178
|
+
|
136
179
|
it "should not call after_save" do
|
137
180
|
@db.save_document @recorder
|
138
|
-
@recorder.callbacks.
|
181
|
+
expect(@recorder.callbacks).not_to include(:after_save)
|
139
182
|
end
|
140
|
-
|
183
|
+
|
141
184
|
it "should not call before_create" do
|
142
185
|
@db.save_document @recorder
|
143
|
-
@recorder.callbacks.
|
186
|
+
expect(@recorder.callbacks).not_to include(:before_create)
|
144
187
|
end
|
145
|
-
|
188
|
+
|
146
189
|
it "should not call after_create" do
|
147
190
|
@db.save_document @recorder
|
148
|
-
@recorder.callbacks.
|
191
|
+
expect(@recorder.callbacks).not_to include(:after_create)
|
149
192
|
end
|
150
193
|
end
|
151
194
|
end
|
152
195
|
|
153
196
|
describe "update callbacks" do
|
154
|
-
|
197
|
+
|
155
198
|
before(:each) do
|
156
199
|
@recorder = CallbackRecorder.new :required_property => 1
|
157
|
-
|
158
|
-
couchrest_database =
|
200
|
+
|
201
|
+
couchrest_database = double 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}, :view => {'rows' => []}, :info => nil
|
159
202
|
@db = CouchPotato::Database.new(couchrest_database)
|
160
203
|
@db.save_document! @recorder
|
161
|
-
|
162
|
-
@recorder.required_property = 2
|
204
|
+
|
163
205
|
@recorder.callbacks.clear
|
164
206
|
end
|
165
|
-
|
207
|
+
|
166
208
|
describe "successful update" do
|
167
|
-
|
209
|
+
|
168
210
|
before(:each) do
|
211
|
+
@recorder.required_property = 2
|
169
212
|
@db.save_document! @recorder
|
170
213
|
end
|
171
|
-
|
214
|
+
|
172
215
|
it "should call before_validation" do
|
173
|
-
@recorder.callbacks.
|
216
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
174
217
|
end
|
175
|
-
|
218
|
+
|
176
219
|
it "should call before_validation_on_update" do
|
177
|
-
@recorder.callbacks.
|
220
|
+
expect(@recorder.callbacks).to include(:before_validation_on_update)
|
178
221
|
end
|
179
|
-
|
222
|
+
|
180
223
|
it "should call before_validation_on_save" do
|
181
|
-
@recorder.callbacks.
|
224
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
182
225
|
end
|
183
|
-
|
226
|
+
|
184
227
|
it "should call before_save" do
|
185
|
-
@recorder.callbacks.
|
228
|
+
expect(@recorder.callbacks).to include(:before_save)
|
186
229
|
end
|
187
|
-
|
230
|
+
|
188
231
|
it "should call after_save" do
|
189
|
-
@recorder.callbacks.
|
232
|
+
expect(@recorder.callbacks).to include(:after_save)
|
190
233
|
end
|
191
|
-
|
234
|
+
|
192
235
|
it "should call before_update" do
|
193
|
-
@recorder.callbacks.
|
236
|
+
expect(@recorder.callbacks).to include(:before_update)
|
194
237
|
end
|
195
|
-
|
238
|
+
|
196
239
|
it "should call after_update" do
|
197
|
-
@recorder.callbacks.
|
240
|
+
expect(@recorder.callbacks).to include(:after_update)
|
198
241
|
end
|
199
|
-
|
242
|
+
|
200
243
|
end
|
201
|
-
|
244
|
+
|
245
|
+
describe "successful update with no changes (object is not dirty)" do
|
246
|
+
|
247
|
+
before(:each) do
|
248
|
+
@db.save_document! @recorder
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should call before_validation" do
|
252
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should call before_validation_on_update" do
|
256
|
+
expect(@recorder.callbacks).to include(:before_validation_on_update)
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should call before_validation_on_save" do
|
260
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should call before_save" do
|
264
|
+
expect(@recorder.callbacks).to include(:before_save)
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should call after_save" do
|
268
|
+
expect(@recorder.callbacks).to include(:after_save)
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should call before_update" do
|
272
|
+
expect(@recorder.callbacks).to include(:before_update)
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should call after_update" do
|
276
|
+
expect(@recorder.callbacks).to include(:after_update)
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
|
202
282
|
describe "failed update" do
|
203
|
-
|
283
|
+
|
204
284
|
before(:each) do
|
205
285
|
@recorder.required_property = nil
|
206
286
|
@db.save_document @recorder
|
207
287
|
end
|
208
|
-
|
288
|
+
|
209
289
|
it "should call before_validation" do
|
210
|
-
@recorder.callbacks.
|
290
|
+
expect(@recorder.callbacks).to include(:before_validation)
|
211
291
|
end
|
212
|
-
|
292
|
+
|
213
293
|
it "should call before_validation_on_update" do
|
214
|
-
@recorder.callbacks.
|
294
|
+
expect(@recorder.callbacks).to include(:before_validation_on_update)
|
215
295
|
end
|
216
|
-
|
296
|
+
|
217
297
|
it "should call before_validation_on_save" do
|
218
|
-
@recorder.callbacks.
|
298
|
+
expect(@recorder.callbacks).to include(:before_validation_on_save)
|
219
299
|
end
|
220
|
-
|
300
|
+
|
221
301
|
it "should not call before_save" do
|
222
|
-
@recorder.callbacks.
|
302
|
+
expect(@recorder.callbacks).not_to include(:before_save)
|
223
303
|
end
|
224
|
-
|
304
|
+
|
225
305
|
it "should not call after_save" do
|
226
|
-
@recorder.callbacks.
|
306
|
+
expect(@recorder.callbacks).not_to include(:after_save)
|
227
307
|
end
|
228
|
-
|
308
|
+
|
229
309
|
it "should not call before_update" do
|
230
|
-
@recorder.callbacks.
|
310
|
+
expect(@recorder.callbacks).not_to include(:before_update)
|
231
311
|
end
|
232
|
-
|
312
|
+
|
233
313
|
it "should not call after_update" do
|
234
|
-
@recorder.callbacks.
|
314
|
+
expect(@recorder.callbacks).not_to include(:after_update)
|
235
315
|
end
|
236
|
-
|
316
|
+
|
237
317
|
end
|
238
|
-
|
318
|
+
|
239
319
|
end
|
240
320
|
|
241
321
|
describe "destroy callbacks" do
|
242
|
-
|
322
|
+
|
243
323
|
before(:each) do
|
244
324
|
@recorder = CallbackRecorder.new :required_property => 1
|
245
|
-
couchrest_database =
|
325
|
+
couchrest_database = double 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}, :delete_doc => nil, :view => {'rows' => []}, :info => nil
|
246
326
|
@db = CouchPotato::Database.new(couchrest_database)
|
247
327
|
@db.save_document! @recorder
|
248
|
-
|
328
|
+
|
249
329
|
@recorder.callbacks.clear
|
250
330
|
end
|
251
|
-
|
331
|
+
|
252
332
|
it "should call before_destroy" do
|
253
333
|
@db.destroy_document @recorder
|
254
|
-
@recorder.callbacks.
|
334
|
+
expect(@recorder.callbacks).to include(:before_destroy)
|
255
335
|
end
|
256
|
-
|
336
|
+
|
257
337
|
it "should call after_destroy" do
|
258
338
|
@db.destroy_document @recorder
|
259
|
-
@recorder.callbacks.
|
339
|
+
expect(@recorder.callbacks).to include(:after_destroy)
|
260
340
|
end
|
261
341
|
end
|
262
342
|
|
@@ -275,24 +355,24 @@ describe "validation callbacks" do
|
|
275
355
|
|
276
356
|
it "should keep error messages set in custom before_validation filters" do
|
277
357
|
user = ValidatedUser.new(:name => "john")
|
278
|
-
user.valid
|
279
|
-
user.errors[:name].
|
358
|
+
expect(user.valid?).to eq(false)
|
359
|
+
expect(user.errors[:name]).to eq(["should be Paul"])
|
280
360
|
end
|
281
361
|
|
282
362
|
it "should combine the errors from validations and callbacks" do
|
283
363
|
user = ValidatedUser.new(:name => nil)
|
284
|
-
user.valid
|
285
|
-
user.errors[:name].any? {|msg| msg =~ /can't be (empty|blank)/ }.
|
286
|
-
user.errors[:name].any? {|msg| msg == "should be Paul" }.
|
287
|
-
user.errors[:name].size.
|
364
|
+
expect(user.valid?).to eq(false)
|
365
|
+
expect(user.errors[:name].any? {|msg| msg =~ /can't be (empty|blank)/ }).to eq(true)
|
366
|
+
expect(user.errors[:name].any? {|msg| msg == "should be Paul" }).to eq(true)
|
367
|
+
expect(user.errors[:name].size).to eq(2)
|
288
368
|
end
|
289
369
|
|
290
370
|
it "should clear the errors on subsequent calls to valid?" do
|
291
371
|
user = ValidatedUser.new(:name => nil)
|
292
|
-
user.valid
|
372
|
+
expect(user.valid?).to eq(false)
|
293
373
|
user.name = 'Paul'
|
294
|
-
user.valid
|
295
|
-
user.errors[:name].
|
374
|
+
expect(user.valid?).to eq(true)
|
375
|
+
expect(user.errors[:name]).to eq([])
|
296
376
|
end
|
297
377
|
end
|
298
378
|
|
@@ -360,26 +440,26 @@ describe "validation callbacks and filter halt" do
|
|
360
440
|
|
361
441
|
it "should keep error messages set in custom before_validation if an update filter returns false" do
|
362
442
|
@user = FilterValidationUpdateUser.new(:name => "Paul")
|
363
|
-
@db.save_document(@user).
|
443
|
+
expect(@db.save_document(@user)).to eq(true)
|
364
444
|
@user.name = 'Bert'
|
365
|
-
@db.save_document(@user).
|
445
|
+
expect(@db.save_document(@user)).to eq(false)
|
366
446
|
end
|
367
447
|
|
368
448
|
it "should keep error messages set in custom before_validation if a create filter returns false" do
|
369
449
|
@user = FilterValidationCreateUser.new(:name => "Bert")
|
370
|
-
@db.save_document(@user).
|
450
|
+
expect(@db.save_document(@user)).to eq(false)
|
371
451
|
end
|
372
452
|
|
373
453
|
it "should return false on saving a document when a before update filter returned false" do
|
374
454
|
@user = FilterSaveUpdateUser.new(:name => "Paul")
|
375
|
-
@db.save_document(@user).
|
455
|
+
expect(@db.save_document(@user)).to eq(true)
|
376
456
|
@user.name = 'Bert'
|
377
|
-
@db.save_document(@user).
|
457
|
+
expect(@db.save_document(@user)).to eq(false)
|
378
458
|
end
|
379
459
|
|
380
460
|
it "should return false on saving a document when a before save or before create filter returned false" do
|
381
461
|
@user = FilterSaveCreateUser.new(:name => "Bert")
|
382
|
-
@db.save_document(@user).
|
462
|
+
expect(@db.save_document(@user)).to eq(false)
|
383
463
|
end
|
384
464
|
|
385
465
|
end
|