diff --git a/couchrest.gemspec b/couchrest.gemspec index 30e727f..04130cc 100644 --- a/couchrest.gemspec +++ b/couchrest.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = %q{couchrest} - s.version = "0.15" + s.version = "0.16" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["J. Chris Anderson", "Matt Aimonetti"] diff --git a/lib/couchrest.rb b/lib/couchrest.rb index af40a0b..7b77e6a 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -27,13 +27,13 @@ require 'couchrest/monkeypatches' # = CouchDB, close to the metal module CouchRest - VERSION = '0.15' + VERSION = '0.16' unless self.const_defined?("VERSION") autoload :Server, 'couchrest/core/server' autoload :Database, 'couchrest/core/database' - autoload :Response, 'couchrest/core/response' + autoload :Response, 'couchrest/core/response' autoload :Document, 'couchrest/core/document' - autoload :Design, 'couchrest/core/design' + autoload :Design, 'couchrest/core/design' autoload :View, 'couchrest/core/view' autoload :Model, 'couchrest/core/model' autoload :Pager, 'couchrest/helper/pager' diff --git a/lib/couchrest/core/server.rb b/lib/couchrest/core/server.rb index 8d39998..2f46f86 100644 --- a/lib/couchrest/core/server.rb +++ b/lib/couchrest/core/server.rb @@ -79,7 +79,7 @@ module CouchRest def next_uuid(count = @uuid_batch_count) @uuids ||= [] if @uuids.empty? - @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"] + @uuids = CouchRest.get("#{@uri}/_uuids?count=#{count}")["uuids"] end @uuids.pop end diff --git a/lib/couchrest/mixins/callbacks.rb b/lib/couchrest/mixins/callbacks.rb index 3299d0a..28eca50 100644 --- a/lib/couchrest/mixins/callbacks.rb +++ b/lib/couchrest/mixins/callbacks.rb @@ -325,7 +325,7 @@ module CouchRest end module ClassMethods - CHAINS = {:before => :before, :around => :before, :after => :after} + CHAINS = {:before => :before, :around => :before, :after => :after} unless self.const_defined?("CHAINS") # Make the _run_save_callbacks method. The generated method takes # a block that it'll yield to. It'll call the before and around filters diff --git a/lib/couchrest/mixins/extended_attachments.rb b/lib/couchrest/mixins/extended_attachments.rb index 009abf7..278bc2b 100644 --- a/lib/couchrest/mixins/extended_attachments.rb +++ b/lib/couchrest/mixins/extended_attachments.rb @@ -14,7 +14,7 @@ module CouchRest # reads the data from an attachment def read_attachment(attachment_name) - Base64.decode64(database.fetch_attachment(self.id, attachment_name)) + Base64.decode64(database.fetch_attachment(self, attachment_name)) end # modifies a file attachment on the current doc diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index 95d5d19..ceb78cb 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -1,4 +1,5 @@ begin + # still required for Time parsing and pluralization in the validation require 'extlib' rescue puts "CouchRest::ExtendedDocument still requires extlib (not for much longer). This is left out of the gemspec on purpose." diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 4a83b57..18711ef 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -247,9 +247,11 @@ describe CouchRest::Database do @db.save_doc(@doc) end - it "should get the attachment with the doc's _id" do - @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach - end + # Depreacated + # it "should get the attachment with the doc's _id" do + # @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach + # end + it "should get the attachment with the doc itself" do @db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach end @@ -266,7 +268,8 @@ describe CouchRest::Database do it "should save the attachment to a new doc" do r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'}) r['ok'].should == true - attachment = @db.fetch_attachment("attach-this","couchdb.png") + doc = @db.get("attach-this") + attachment = @db.fetch_attachment(doc,"couchdb.png") attachment.should == image end end @@ -274,7 +277,7 @@ describe CouchRest::Database do describe "PUT document with attachment" do before(:each) do @attach = "My Doc

Has words.

" - @doc = { + doc = { "_id" => "mydocwithattachment", "field" => ["some value"], "_attachments" => { @@ -284,14 +287,14 @@ describe CouchRest::Database do } } } - @db.save_doc(@doc) + @db.save_doc(doc) + @doc = @db.get("mydocwithattachment") end it "should save and be indicated" do - doc = @db.get("mydocwithattachment") - doc['_attachments']['test.html']['length'].should == @attach.length + @doc['_attachments']['test.html']['length'].should == @attach.length end it "should be there" do - attachment = @db.fetch_attachment("mydocwithattachment","test.html") + attachment = @db.fetch_attachment(@doc,"test.html") attachment.should == @attach end end @@ -309,14 +312,14 @@ describe CouchRest::Database do } } @db.save_doc(doc) - doc = @db.get('mydocwithattachment') doc['field'] << 'another value' - @db.save_doc(doc) + @db.save_doc(doc).should be_true end it 'should be there' do - attachment = @db.fetch_attachment('mydocwithattachment', 'test.html') - attachment.should == @attach + doc = @db.get('mydocwithattachment') + attachment = @db.fetch_attachment(doc, 'test.html') + Base64.decode64(attachment).should == @attach end end @@ -339,18 +342,18 @@ describe CouchRest::Database do } } @db.save_doc(@doc) + @doc = @db.get("mydocwithattachment") end it "should save and be indicated" do - doc = @db.get("mydocwithattachment") - doc['_attachments']['test.html']['length'].should == @attach.length - doc['_attachments']['other.html']['length'].should == @attach2.length + @doc['_attachments']['test.html']['length'].should == @attach.length + @doc['_attachments']['other.html']['length'].should == @attach2.length end it "should be there" do - attachment = @db.fetch_attachment("mydocwithattachment","test.html") + attachment = @db.fetch_attachment(@doc,"test.html") attachment.should == @attach end it "should be there" do - attachment = @db.fetch_attachment("mydocwithattachment","other.html") + attachment = @db.fetch_attachment(@doc,"other.html") attachment.should == @attach2 end end @@ -370,9 +373,9 @@ describe CouchRest::Database do @doc = @db.get('mydocwithattachment') end it "should delete the attachment" do - lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should_not raise_error + lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error @db.delete_attachment(@doc, "test.html") - lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should raise_error(RestClient::ResourceNotFound) + lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound) end end @@ -395,7 +398,8 @@ describe CouchRest::Database do doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length end it "should be there" do - attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff") + doc = @db.get(@docid) + attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff") attachment.should == @attach end end @@ -737,4 +741,4 @@ describe CouchRest::Database do end -end +end \ No newline at end of file diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index fadb676..f36394a 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -439,12 +439,10 @@ describe "ExtendedDocument" do result.should == true end it "should be resavable" do - pending "TO FIX" do - @dobj.destroy - @dobj.rev.should be_nil - @dobj.id.should be_nil - @dobj.save.should == true - end + @dobj.destroy + @dobj.rev.should be_nil + @dobj.id.should be_nil + @dobj.save.should == true end it "should make it go away" do @dobj.destroy diff --git a/spec/fixtures/more/article.rb b/spec/fixtures/more/article.rb new file mode 100644 index 0000000..02b7944 --- /dev/null +++ b/spec/fixtures/more/article.rb @@ -0,0 +1,34 @@ +class Article < CouchRest::ExtendedDocument + use_database TEST_SERVER.default_database + unique_id :slug + + view_by :date, :descending => true + view_by :user_id, :date + + view_by :tags, + :map => + "function(doc) { + if (doc['couchrest-type'] == 'Article' && doc.tags) { + doc.tags.forEach(function(tag){ + emit(tag, 1); + }); + } + }", + :reduce => + "function(keys, values, rereduce) { + return sum(values); + }" + + property :date + property :slug, :read_only => true + property :title + property :tags + + timestamps! + + save_callback :before, :generate_slug_from_title + + def generate_slug_from_title + self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document? + end +end \ No newline at end of file diff --git a/spec/fixtures/more/course.rb b/spec/fixtures/more/course.rb new file mode 100644 index 0000000..b639d34 --- /dev/null +++ b/spec/fixtures/more/course.rb @@ -0,0 +1,14 @@ +require File.join(FIXTURE_PATH, 'more', 'question') +require File.join(FIXTURE_PATH, 'more', 'person') + +class Course < CouchRest::ExtendedDocument + use_database TEST_SERVER.default_database + + property :title + property :questions, :cast_as => ['Question'] + property :professor, :cast_as => 'Person' + property :final_test_at, :cast_as => 'Time' + + view_by :title + view_by :dept, :ducktype => true +end \ No newline at end of file diff --git a/spec/fixtures/more/event.rb b/spec/fixtures/more/event.rb new file mode 100644 index 0000000..c36527a --- /dev/null +++ b/spec/fixtures/more/event.rb @@ -0,0 +1,6 @@ +class Event < CouchRest::ExtendedDocument + use_database TEST_SERVER.default_database + + property :subject + property :occurs_at, :cast_as => 'Time', :send => 'parse' +end \ No newline at end of file diff --git a/spec/fixtures/more/person.rb b/spec/fixtures/more/person.rb new file mode 100644 index 0000000..ddc1bfd --- /dev/null +++ b/spec/fixtures/more/person.rb @@ -0,0 +1,8 @@ +class Person < Hash + include ::CouchRest::CastedModel + property :name + + def last_name + name.last + end +end \ No newline at end of file diff --git a/spec/fixtures/more/question.rb b/spec/fixtures/more/question.rb new file mode 100644 index 0000000..bec7803 --- /dev/null +++ b/spec/fixtures/more/question.rb @@ -0,0 +1,6 @@ +class Question < Hash + include ::CouchRest::CastedModel + + property :q + property :a +end \ No newline at end of file