Removed model create/update callbacks and integrated with new bulk save infrastructure.

This commit is contained in:
Nolan Darilek 2008-12-15 10:27:53 -06:00
parent d8d5645ebd
commit 84382d8af4
5 changed files with 33 additions and 37 deletions

View file

@ -119,7 +119,7 @@ module CouchRest
if bulk if bulk
@bulk_save_cache << doc @bulk_save_cache << doc
return bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit return bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit
return doc return {"ok" => true} # Compatibility with Document#save
elsif !bulk && @bulk_save_cache.length > 0 elsif !bulk && @bulk_save_cache.length > 0
bulk_save bulk_save
end end

View file

@ -35,9 +35,10 @@ module CouchRest
# Saves the document to the db using create or update. Also runs the :save # Saves the document to the db using create or update. Also runs the :save
# callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on # callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on
# CouchDB's response. # CouchDB's response.
def save # If <tt>bulk</tt> is <tt>true</tt> (defaults to false) the document is cached for bulk save.
def save(bulk = false)
raise ArgumentError, "doc.database required for saving" unless database raise ArgumentError, "doc.database required for saving" unless database
result = database.save self result = database.save self, bulk
result['ok'] result['ok']
end end
@ -57,4 +58,4 @@ module CouchRest
end end
end end

View file

@ -215,8 +215,9 @@ module CouchRest
# on the document whenever saving occurs. CouchRest uses a pretty # on the document whenever saving occurs. CouchRest uses a pretty
# decent time format by default. See Time#to_json # decent time format by default. See Time#to_json
def timestamps! def timestamps!
before(:create) do before(:save) do
self['updated_at'] = self['created_at'] = Time.now self['updated_at'] = Time.now
self['created_at'] = self['updated_at'] if new_document?
end end
before(:update) do before(:update) do
self['updated_at'] = Time.now self['updated_at'] = Time.now
@ -478,19 +479,10 @@ module CouchRest
# for compatibility with old-school frameworks # for compatibility with old-school frameworks
alias :new_record? :new_document? alias :new_record? :new_document?
# We override this to create the create and update callback opportunities. # Overridden to set the unique ID.
# I think we should drop those and just have save. If you care, in your callback, def save bulk = false
# check new_document? set_unique_id if self.respond_to?(:set_unique_id)
def save actually=false super(bulk)
if actually
super()
else
if new_document?
create
else
update
end
end
end end
# Saves the document to the db using create or update. Raises an exception # Saves the document to the db using create or update. Raises an exception
@ -498,10 +490,6 @@ module CouchRest
def save! def save!
raise "#{self.inspect} failed to save" unless self.save raise "#{self.inspect} failed to save" unless self.save
end end
def update
save :actually
end
# Deletes the document from the database. Runs the :delete callbacks. # Deletes the document from the database. Runs the :delete callbacks.
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the # Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
@ -515,12 +503,6 @@ module CouchRest
result['ok'] result['ok']
end end
def create
# can we use the callbacks for this?
set_unique_id if self.respond_to?(:set_unique_id)
save :actually
end
private private
def apply_defaults def apply_defaults
@ -549,9 +531,7 @@ module CouchRest
end end
include ::Extlib::Hook include ::Extlib::Hook
# todo: drop create and update hooks... register_instance_hooks :save, :destroy
# (use new_record? in callbacks if you care)
register_instance_hooks :save, :create, :update, :destroy
end # class Model end # class Model
end # module CouchRest end # module CouchRest

View file

@ -53,6 +53,21 @@ describe CouchRest::Document, "saving using a database" do
end end
end end
describe CouchRest::Document, "bulk saving" do
before :all do
@db = reset_test_db!
end
it "should use the document bulk save cache" do
doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
doc.database = @db
doc.save(true)
lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
doc.database.bulk_save
doc.database.get(doc["_id"])["val"].should == doc["val"]
end
end
describe "getting from a database" do describe "getting from a database" do
before(:all) do before(:all) do
@db = reset_test_db! @db = reset_test_db!
@ -93,4 +108,4 @@ describe "destroying a document from a db" do
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values") @doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
lambda{@doc.destroy}.should raise_error(ArgumentError) lambda{@doc.destroy}.should raise_error(ArgumentError)
end end
end end

View file

@ -71,9 +71,9 @@ class Article < CouchRest::Model
timestamps! timestamps!
before(:create, :generate_slug_from_title) before(:save, :generate_slug_from_title)
def generate_slug_from_title def generate_slug_from_title
self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document?
end end
end end
@ -679,4 +679,4 @@ describe CouchRest::Model do
lambda{Basic.get(@obj.id)}.should raise_error lambda{Basic.get(@obj.id)}.should raise_error
end end
end end
end end