Skip to content
Browse files

Introduce save! and create! methods and raise RecordInvalid only from…

… them

Change-Id: Ica64ca5f69dd433f6a1c31f9e2cad84d14bf19ab
Reviewed-on: http://review.couchbase.org/22774
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Andrey Koleshko <ka8725@gmail.com>
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
  • Loading branch information...
1 parent 20188e3 commit a027591d87df588be9e5131cc5c7f82e5d498def @avsej committed Nov 23, 2012
Showing with 43 additions and 8 deletions.
  1. +37 −5 lib/couchbase/model.rb
  2. +2 −1 lib/couchbase/railtie.rb
  3. +4 −2 test/test_model.rb
View
42 lib/couchbase/model.rb
@@ -430,11 +430,20 @@ def self.find_by_id(id)
# @since 0.0.1
#
# @param [Hash] args attribute-value pairs for the object
- # @return [Couchbase::Model] an instance of the model
+ # @return [Couchbase::Model, false] an instance of the model
def self.create(*args)
new(*args).create
end
+ # Creates an object just like {{Model.create} but raises an exception if
+ # the record is invalid.
+ #
+ # @since 0.5.1
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
+ def self.create!(*args)
+ new(*args).create!
+ end
+
# Constructor for all subclasses of Couchbase::Model
#
# @since 0.0.1
@@ -473,7 +482,7 @@ def initialize(attrs = {})
#
# @since 0.0.1
#
- # @return [Couchbase::Model] newly created object
+ # @return [Couchbase::Model, false] newly created object
#
# @raise [Couchbase::Error::KeyExists] if model with the same +id+
# exists in the bucket
@@ -484,7 +493,7 @@ def initialize(attrs = {})
def create(options = {})
@id ||= Couchbase::Model::UUID.generator.next(1, model.thread_storage[:uuid_algorithm])
if respond_to?(:valid?) && !valid?
- raise Couchbase::Error::RecordInvalid.new(self)
+ return false
end
options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
@@ -498,13 +507,25 @@ def create(options = {})
self
end
+ # Creates an object just like {{Model#create} but raises an exception if
+ # the record is invalid.
+ #
+ # @since 0.5.1
+ #
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
+ def create!(options = {})
+ create(options) || raise(Couchbase::Error::RecordInvalid.new(self))
+ end
+
# Create or update this object based on the state of #new?.
#
# @since 0.0.1
#
# @param [Hash] options options for operation, see
# {{Couchbase::Bucket#set}}
- # @return [Couchbase::Model] The saved object
+ #
+ # @return [Couchbase::Model, false] saved object or false if there
+ # are validation errors
#
# @example Update the Post model
# p = Post.find('hello-world')
@@ -515,17 +536,28 @@ def create(options = {})
# p = Post.find('hello-world')
# p.draft = false
# p.save('cas' => p.meta['cas'])
+ #
def save(options = {})
return create(options) unless @meta
if respond_to?(:valid?) && !valid?
- raise Couchbase::Error::RecordInvalid.new(self)
+ return false
end
options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
@meta['cas'] = model.bucket.replace(@id, value, options)
self
end
+ # Creates an object just like {{Model#save} but raises an exception if
+ # the record is invalid.
+ #
+ # @since 0.5.1
+ #
+ # @raise [Couchbase::Error::RecordInvalid] if the instance is invalid
+ def save!(options = {})
+ save(options) || raise(Couchbase::Error::RecordInvalid.new(self))
+ end
+
# Update this object, optionally accepting new attributes.
#
# @since 0.0.1
View
3 lib/couchbase/railtie.rb
@@ -50,7 +50,8 @@ def self.generator
def self.rescue_responses
{
"Couchbase::Error::NotFound" => :not_found,
- "Couchbase::Error::NotStored" => :unprocessable_entity
+ "Couchbase::Error::NotStored" => :unprocessable_entity,
+ "Couchbase::Error::RecordInvalid" => :unprocessable_entity
}
end
View
6 test/test_model.rb
@@ -225,11 +225,13 @@ def test_validation
post = ValidPost.create(:title => 'Hello, World!')
assert post.valid?, "post with title should be valid"
post.title = nil
+ refute post.save
assert_raises(Couchbase::Error::RecordInvalid) do
- post.save
+ post.save!
end
+ refute ValidPost.create(:title => nil)
assert_raises(Couchbase::Error::RecordInvalid) do
- ValidPost.create(:title => nil)
+ ValidPost.create!(:title => nil)
end
end

0 comments on commit a027591

Please sign in to comment.
Something went wrong with that request. Please try again.