Skip to content
Browse files

Ensure validness on create

Change-Id: Ic4834a774d1ac92db4aa311b127c3978ba05331e
Reviewed-on: http://review.couchbase.org/21775
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
1 parent acc4ad0 commit 6bb168d5cc2dda86ccef9d9d80170e723ececbf2 @avsej committed
Showing with 36 additions and 10 deletions.
  1. +12 −8 lib/couchbase/model.rb
  2. +24 −2 test/test_model.rb
View
20 lib/couchbase/model.rb
@@ -42,7 +42,11 @@ class Error::RecordInvalid < Error::Base
attr_reader :record
def initialize(record)
@record = record
- super(@record.errors.full_messages.join(", "))
+ if @record.errors
+ super(@record.errors.full_messages.join(", "))
+ else
+ super("Record invalid")
+ end
end
end
@@ -113,6 +117,9 @@ class Model
# @since 0.2.0
attr_reader :meta
+ # @since 0.4.5
+ attr_reader :errors
+
# @private Container for all attributes with defaults of all subclasses
@@attributes = ::Hash.new {|hash, key| hash[key] = {}}
@@ -462,6 +469,9 @@ def initialize(attrs = {})
# p.create
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)
+ end
value = @_raw ? @_raw : attributes_with_values
unless @meta
@meta = {}
@@ -491,17 +501,11 @@ def create(options = {})
# 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)
end
- return create(options) unless meta
value = @_raw ? @_raw : attributes_with_values
- unless @meta
- @meta = {}
- if @meta.respond_to?(:with_indifferent_access)
- @meta = @meta.with_indifferent_access
- end
- end
@meta['cas'] = model.bucket.replace(@id, value, model.defaults.merge(options))
self
end
View
26 test/test_model.rb
@@ -24,6 +24,14 @@ class Post < Couchbase::Model
attribute :created_at, :default => lambda { Time.utc("2010-01-01") }
end
+class ValidPost < Couchbase::Model
+ attribute :title
+
+ def valid?
+ title && !title.empty?
+ end
+end
+
class Brewery < Couchbase::Model
attribute :name
end
@@ -37,8 +45,10 @@ class TestModel < MiniTest::Unit::TestCase
def setup
@mock = start_mock
- Post.bucket = Couchbase.connect(:hostname => @mock.host,
- :port => @mock.port)
+ bucket = Couchbase.connect(:hostname => @mock.host, :port => @mock.port)
+ [Post, ValidPost, Brewery, Beer].each do |model|
+ model.bucket = bucket
+ end
end
def teardown
@@ -181,4 +191,16 @@ def test_to_param
assert_equal "the-key", Post.new(:key => ["the", "key"]).to_param
end
+ def test_validation
+ post = ValidPost.create(:title => 'Hello, World!')
+ assert post.valid?, "post with title should be valid"
+ post.title = nil
+ assert_raises(Couchbase::Error::RecordInvalid) do
+ post.save
+ end
+ assert_raises(Couchbase::Error::RecordInvalid) do
+ ValidPost.create(:title => nil)
+ end
+ end
+
end

0 comments on commit 6bb168d

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