Renamed new_document? and new_model? to simply new?

This commit is contained in:
Peter Gumeson 2009-06-04 20:44:44 -07:00
parent b4e2250668
commit 76b1563539
8 changed files with 26 additions and 26 deletions

View file

@ -27,7 +27,7 @@ module CouchRest
end end
# returns true if the document has never been saved # returns true if the document has never been saved
def new_document? def new?
!rev !rev
end end
@ -67,7 +67,7 @@ module CouchRest
# Returns the CouchDB uri for the document # Returns the CouchDB uri for the document
def uri(append_rev = false) def uri(append_rev = false)
return nil if new_document? return nil if new?
couch_uri = "http://#{database.uri}/#{CGI.escape(id)}" couch_uri = "http://#{database.uri}/#{CGI.escape(id)}"
if append_rev == true if append_rev == true
couch_uri << "?rev=#{rev}" couch_uri << "?rev=#{rev}"

View file

@ -17,7 +17,7 @@ module CouchRest
end end
def apply_defaults def apply_defaults
return if self.respond_to?(:new_document?) && (new_document? == false) return if self.respond_to?(:new?) && (new? == false)
return unless self.class.respond_to?(:properties) return unless self.class.respond_to?(:properties)
return if self.class.properties.empty? return if self.class.properties.empty?
# TODO: cache the default object # TODO: cache the default object

View file

@ -37,10 +37,10 @@ module CouchRest
# False if the casted model has already # False if the casted model has already
# been saved in the containing document # been saved in the containing document
def new_model? def new?
!@document_saved !@document_saved
end end
alias :new_record? :new_model? alias :new_record? :new?
# Sets the attributes from a hash # Sets the attributes from a hash
def update_attributes_without_saving(hash) def update_attributes_without_saving(hash)

View file

@ -64,7 +64,7 @@ module CouchRest
save_callback :before do |object| save_callback :before do |object|
object['updated_at'] = Time.now object['updated_at'] = Time.now
object['created_at'] = object['updated_at'] if object.new_document? object['created_at'] = object['updated_at'] if object.new?
end end
EOS EOS
end end
@ -145,7 +145,7 @@ module CouchRest
end end
# for compatibility with old-school frameworks # for compatibility with old-school frameworks
alias :new_record? :new_document? alias :new_record? :new?
# Trigger the callbacks (before, after, around) # Trigger the callbacks (before, after, around)
# and create the document # and create the document
@ -166,7 +166,7 @@ module CouchRest
# unlike save, create returns the newly created document # unlike save, create returns the newly created document
def create_without_callbacks(bulk =false) def create_without_callbacks(bulk =false)
raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database
set_unique_id if new_document? && self.respond_to?(:set_unique_id) set_unique_id if new? && self.respond_to?(:set_unique_id)
result = database.save_doc(self, bulk) result = database.save_doc(self, bulk)
(result["ok"] == true) ? self : false (result["ok"] == true) ? self : false
end end
@ -181,7 +181,7 @@ module CouchRest
# only if the document isn't new # only if the document isn't new
def update(bulk = false) def update(bulk = false)
caught = catch(:halt) do caught = catch(:halt) do
if self.new_document? if self.new?
save(bulk) save(bulk)
else else
_run_update_callbacks do _run_update_callbacks do
@ -197,7 +197,7 @@ module CouchRest
# and save the document # and save the document
def save(bulk = false) def save(bulk = false)
caught = catch(:halt) do caught = catch(:halt) do
if self.new_document? if self.new?
_run_save_callbacks do _run_save_callbacks do
save_without_callbacks(bulk) save_without_callbacks(bulk)
end end
@ -211,7 +211,7 @@ module CouchRest
# Returns a boolean value # Returns a boolean value
def save_without_callbacks(bulk = false) def save_without_callbacks(bulk = false)
raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database
set_unique_id if new_document? && self.respond_to?(:set_unique_id) set_unique_id if new? && self.respond_to?(:set_unique_id)
result = database.save_doc(self, bulk) result = database.save_doc(self, bulk)
mark_as_saved if result["ok"] == true mark_as_saved if result["ok"] == true
result["ok"] == true result["ok"] == true

View file

@ -280,7 +280,7 @@ describe CouchRest::CastedModel do
end end
end end
describe "calling new_model? on a casted model" do describe "calling new? on a casted model" do
before :each do before :each do
reset_test_db! reset_test_db!
@cat = Cat.new(:name => 'Sockington') @cat = Cat.new(:name => 'Sockington')
@ -289,35 +289,35 @@ describe CouchRest::CastedModel do
end end
it "should be true on new" do it "should be true on new" do
CatToy.new.new_model?.should be_true CatToy.new.should be_new
CatToy.new.new_record?.should be_true CatToy.new.new_record?.should be_true
end end
it "should be true after assignment" do it "should be true after assignment" do
@cat.favorite_toy.new_model?.should be_true @cat.favorite_toy.should be_new
@cat.toys.first.new_model?.should be_true @cat.toys.first.should be_new
end end
it "should not be true after create or save" do it "should not be true after create or save" do
@cat.create @cat.create
@cat.save @cat.save
@cat.favorite_toy.new_model?.should be_false @cat.favorite_toy.should_not be_new
@cat.toys.first.new_model?.should be_false @cat.toys.first.should_not be_new
end end
it "should not be true after get from the database" do it "should not be true after get from the database" do
@cat.save @cat.save
@cat = Cat.get(@cat.id) @cat = Cat.get(@cat.id)
@cat.favorite_toy.new_model?.should be_false @cat.favorite_toy.should_not be_new
@cat.toys.first.new_model?.should be_false @cat.toys.first.should_not be_new
end end
it "should still be true after a failed create or save" do it "should still be true after a failed create or save" do
@cat.name = nil @cat.name = nil
@cat.create.should be_false @cat.create.should be_false
@cat.save.should be_false @cat.save.should be_false
@cat.favorite_toy.new_model?.should be_true @cat.favorite_toy.should be_new
@cat.toys.first.new_model?.should be_true @cat.toys.first.should be_new
end end
end end

View file

@ -97,12 +97,12 @@ describe "ExtendedDocument" do
it "should be a new_record" do it "should be a new_record" do
@obj = Basic.new @obj = Basic.new
@obj.rev.should be_nil @obj.rev.should be_nil
@obj.should be_a_new_record @obj.should be_new
end end
it "should be a new_document" do it "should be a new_document" do
@obj = Basic.new @obj = Basic.new
@obj.rev.should be_nil @obj.rev.should be_nil
@obj.should be_a_new_document @obj.should be_new
end end
end end
@ -405,7 +405,7 @@ describe "ExtendedDocument" do
end end
it "should be a new document" do it "should be a new document" do
@art.should be_a_new_document @art.should be_new
@art.title.should be_nil @art.title.should be_nil
end end

View file

@ -85,7 +85,7 @@ describe "ExtendedDocument properties" do
@invoice.location = nil @invoice.location = nil
@invoice.should_not be_valid @invoice.should_not be_valid
@invoice.save.should be_false @invoice.save.should be_false
@invoice.should be_new_document @invoice.should be_new
end end
end end

View file

@ -29,6 +29,6 @@ class Article < CouchRest::ExtendedDocument
save_callback :before, :generate_slug_from_title save_callback :before, :generate_slug_from_title
def generate_slug_from_title def generate_slug_from_title
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document? self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new?
end end
end end