mattetti-couchrest 0.15 → 0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/couchrest.rb +3 -3
- data/lib/couchrest/core/server.rb +1 -1
- data/lib/couchrest/mixins/callbacks.rb +1 -1
- data/lib/couchrest/mixins/extended_attachments.rb +1 -1
- data/lib/couchrest/more/extended_document.rb +1 -0
- data/spec/couchrest/core/database_spec.rb +26 -22
- data/spec/couchrest/more/extended_doc_spec.rb +4 -6
- data/spec/fixtures/more/article.rb +34 -0
- data/spec/fixtures/more/course.rb +14 -0
- data/spec/fixtures/more/event.rb +6 -0
- data/spec/fixtures/more/person.rb +8 -0
- data/spec/fixtures/more/question.rb +6 -0
- metadata +1 -1
data/lib/couchrest.rb
CHANGED
@@ -27,13 +27,13 @@ require 'couchrest/monkeypatches'
|
|
27
27
|
|
28
28
|
# = CouchDB, close to the metal
|
29
29
|
module CouchRest
|
30
|
-
VERSION = '0.
|
30
|
+
VERSION = '0.16' unless self.const_defined?("VERSION")
|
31
31
|
|
32
32
|
autoload :Server, 'couchrest/core/server'
|
33
33
|
autoload :Database, 'couchrest/core/database'
|
34
|
-
autoload :Response,
|
34
|
+
autoload :Response, 'couchrest/core/response'
|
35
35
|
autoload :Document, 'couchrest/core/document'
|
36
|
-
autoload :Design,
|
36
|
+
autoload :Design, 'couchrest/core/design'
|
37
37
|
autoload :View, 'couchrest/core/view'
|
38
38
|
autoload :Model, 'couchrest/core/model'
|
39
39
|
autoload :Pager, 'couchrest/helper/pager'
|
@@ -79,7 +79,7 @@ module CouchRest
|
|
79
79
|
def next_uuid(count = @uuid_batch_count)
|
80
80
|
@uuids ||= []
|
81
81
|
if @uuids.empty?
|
82
|
-
@uuids = CouchRest.
|
82
|
+
@uuids = CouchRest.get("#{@uri}/_uuids?count=#{count}")["uuids"]
|
83
83
|
end
|
84
84
|
@uuids.pop
|
85
85
|
end
|
@@ -325,7 +325,7 @@ module CouchRest
|
|
325
325
|
end
|
326
326
|
|
327
327
|
module ClassMethods
|
328
|
-
CHAINS = {:before => :before, :around => :before, :after => :after}
|
328
|
+
CHAINS = {:before => :before, :around => :before, :after => :after} unless self.const_defined?("CHAINS")
|
329
329
|
|
330
330
|
# Make the _run_save_callbacks method. The generated method takes
|
331
331
|
# a block that it'll yield to. It'll call the before and around filters
|
@@ -14,7 +14,7 @@ module CouchRest
|
|
14
14
|
|
15
15
|
# reads the data from an attachment
|
16
16
|
def read_attachment(attachment_name)
|
17
|
-
Base64.decode64(database.fetch_attachment(self
|
17
|
+
Base64.decode64(database.fetch_attachment(self, attachment_name))
|
18
18
|
end
|
19
19
|
|
20
20
|
# modifies a file attachment on the current doc
|
@@ -247,9 +247,11 @@ describe CouchRest::Database do
|
|
247
247
|
@db.save_doc(@doc)
|
248
248
|
end
|
249
249
|
|
250
|
-
|
251
|
-
|
252
|
-
|
250
|
+
# Depreacated
|
251
|
+
# it "should get the attachment with the doc's _id" do
|
252
|
+
# @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
|
253
|
+
# end
|
254
|
+
|
253
255
|
it "should get the attachment with the doc itself" do
|
254
256
|
@db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
|
255
257
|
end
|
@@ -266,7 +268,8 @@ describe CouchRest::Database do
|
|
266
268
|
it "should save the attachment to a new doc" do
|
267
269
|
r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
|
268
270
|
r['ok'].should == true
|
269
|
-
|
271
|
+
doc = @db.get("attach-this")
|
272
|
+
attachment = @db.fetch_attachment(doc,"couchdb.png")
|
270
273
|
attachment.should == image
|
271
274
|
end
|
272
275
|
end
|
@@ -274,7 +277,7 @@ describe CouchRest::Database do
|
|
274
277
|
describe "PUT document with attachment" do
|
275
278
|
before(:each) do
|
276
279
|
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
277
|
-
|
280
|
+
doc = {
|
278
281
|
"_id" => "mydocwithattachment",
|
279
282
|
"field" => ["some value"],
|
280
283
|
"_attachments" => {
|
@@ -284,14 +287,14 @@ describe CouchRest::Database do
|
|
284
287
|
}
|
285
288
|
}
|
286
289
|
}
|
287
|
-
@db.save_doc(
|
290
|
+
@db.save_doc(doc)
|
291
|
+
@doc = @db.get("mydocwithattachment")
|
288
292
|
end
|
289
293
|
it "should save and be indicated" do
|
290
|
-
doc
|
291
|
-
doc['_attachments']['test.html']['length'].should == @attach.length
|
294
|
+
@doc['_attachments']['test.html']['length'].should == @attach.length
|
292
295
|
end
|
293
296
|
it "should be there" do
|
294
|
-
attachment = @db.fetch_attachment(
|
297
|
+
attachment = @db.fetch_attachment(@doc,"test.html")
|
295
298
|
attachment.should == @attach
|
296
299
|
end
|
297
300
|
end
|
@@ -309,14 +312,14 @@ describe CouchRest::Database do
|
|
309
312
|
}
|
310
313
|
}
|
311
314
|
@db.save_doc(doc)
|
312
|
-
doc = @db.get('mydocwithattachment')
|
313
315
|
doc['field'] << 'another value'
|
314
|
-
@db.save_doc(doc)
|
316
|
+
@db.save_doc(doc).should be_true
|
315
317
|
end
|
316
318
|
|
317
319
|
it 'should be there' do
|
318
|
-
|
319
|
-
attachment
|
320
|
+
doc = @db.get('mydocwithattachment')
|
321
|
+
attachment = @db.fetch_attachment(doc, 'test.html')
|
322
|
+
Base64.decode64(attachment).should == @attach
|
320
323
|
end
|
321
324
|
end
|
322
325
|
|
@@ -339,18 +342,18 @@ describe CouchRest::Database do
|
|
339
342
|
}
|
340
343
|
}
|
341
344
|
@db.save_doc(@doc)
|
345
|
+
@doc = @db.get("mydocwithattachment")
|
342
346
|
end
|
343
347
|
it "should save and be indicated" do
|
344
|
-
doc
|
345
|
-
doc['_attachments']['
|
346
|
-
doc['_attachments']['other.html']['length'].should == @attach2.length
|
348
|
+
@doc['_attachments']['test.html']['length'].should == @attach.length
|
349
|
+
@doc['_attachments']['other.html']['length'].should == @attach2.length
|
347
350
|
end
|
348
351
|
it "should be there" do
|
349
|
-
attachment = @db.fetch_attachment(
|
352
|
+
attachment = @db.fetch_attachment(@doc,"test.html")
|
350
353
|
attachment.should == @attach
|
351
354
|
end
|
352
355
|
it "should be there" do
|
353
|
-
attachment = @db.fetch_attachment(
|
356
|
+
attachment = @db.fetch_attachment(@doc,"other.html")
|
354
357
|
attachment.should == @attach2
|
355
358
|
end
|
356
359
|
end
|
@@ -370,9 +373,9 @@ describe CouchRest::Database do
|
|
370
373
|
@doc = @db.get('mydocwithattachment')
|
371
374
|
end
|
372
375
|
it "should delete the attachment" do
|
373
|
-
lambda { @db.fetch_attachment(
|
376
|
+
lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
|
374
377
|
@db.delete_attachment(@doc, "test.html")
|
375
|
-
lambda { @db.fetch_attachment(
|
378
|
+
lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
|
376
379
|
end
|
377
380
|
end
|
378
381
|
|
@@ -395,7 +398,8 @@ describe CouchRest::Database do
|
|
395
398
|
doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
|
396
399
|
end
|
397
400
|
it "should be there" do
|
398
|
-
|
401
|
+
doc = @db.get(@docid)
|
402
|
+
attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
|
399
403
|
attachment.should == @attach
|
400
404
|
end
|
401
405
|
end
|
@@ -737,4 +741,4 @@ describe CouchRest::Database do
|
|
737
741
|
end
|
738
742
|
|
739
743
|
|
740
|
-
end
|
744
|
+
end
|
@@ -439,12 +439,10 @@ describe "ExtendedDocument" do
|
|
439
439
|
result.should == true
|
440
440
|
end
|
441
441
|
it "should be resavable" do
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
@dobj.save.should == true
|
447
|
-
end
|
442
|
+
@dobj.destroy
|
443
|
+
@dobj.rev.should be_nil
|
444
|
+
@dobj.id.should be_nil
|
445
|
+
@dobj.save.should == true
|
448
446
|
end
|
449
447
|
it "should make it go away" do
|
450
448
|
@dobj.destroy
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Article < CouchRest::ExtendedDocument
|
2
|
+
use_database TEST_SERVER.default_database
|
3
|
+
unique_id :slug
|
4
|
+
|
5
|
+
view_by :date, :descending => true
|
6
|
+
view_by :user_id, :date
|
7
|
+
|
8
|
+
view_by :tags,
|
9
|
+
:map =>
|
10
|
+
"function(doc) {
|
11
|
+
if (doc['couchrest-type'] == 'Article' && doc.tags) {
|
12
|
+
doc.tags.forEach(function(tag){
|
13
|
+
emit(tag, 1);
|
14
|
+
});
|
15
|
+
}
|
16
|
+
}",
|
17
|
+
:reduce =>
|
18
|
+
"function(keys, values, rereduce) {
|
19
|
+
return sum(values);
|
20
|
+
}"
|
21
|
+
|
22
|
+
property :date
|
23
|
+
property :slug, :read_only => true
|
24
|
+
property :title
|
25
|
+
property :tags
|
26
|
+
|
27
|
+
timestamps!
|
28
|
+
|
29
|
+
save_callback :before, :generate_slug_from_title
|
30
|
+
|
31
|
+
def generate_slug_from_title
|
32
|
+
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document?
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.join(FIXTURE_PATH, 'more', 'question')
|
2
|
+
require File.join(FIXTURE_PATH, 'more', 'person')
|
3
|
+
|
4
|
+
class Course < CouchRest::ExtendedDocument
|
5
|
+
use_database TEST_SERVER.default_database
|
6
|
+
|
7
|
+
property :title
|
8
|
+
property :questions, :cast_as => ['Question']
|
9
|
+
property :professor, :cast_as => 'Person'
|
10
|
+
property :final_test_at, :cast_as => 'Time'
|
11
|
+
|
12
|
+
view_by :title
|
13
|
+
view_by :dept, :ducktype => true
|
14
|
+
end
|