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
@bulk_save_cache << doc
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
bulk_save
end

View file

@ -35,9 +35,10 @@ module CouchRest
# 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
# 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
result = database.save self
result = database.save self, bulk
result['ok']
end
@ -57,4 +58,4 @@ module CouchRest
end
end
end

View file

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

View file

@ -71,9 +71,9 @@ class Article < CouchRest::Model
timestamps!
before(:create, :generate_slug_from_title)
before(:save, :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
@ -679,4 +679,4 @@ describe CouchRest::Model do
lambda{Basic.get(@obj.id)}.should raise_error
end
end
end
end