Skip to content

Commit

Permalink
Allow save & save! without performing validations
Browse files Browse the repository at this point in the history
Fixes #17

Change-Id: I2dad010c8739bf7b0e81b02f449877edaa26a16b
Reviewed-on: http://review.couchbase.org/41272
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
  • Loading branch information
Archit Baweja authored and avsej committed Sep 9, 2014
1 parent 734f107 commit c928da8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/couchbase/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,8 @@ 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?
return false
end
return false if failed_validations?(options)

options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
unless @meta
Expand Down Expand Up @@ -567,9 +566,8 @@ def create!(options = {})
#
def save(options = {})
return create(options) unless @meta
if respond_to?(:valid?) && !valid?
return false
end
return false if failed_validations?(options)

options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
@meta['cas'] = model.bucket.replace(@id, value, options)
Expand Down Expand Up @@ -842,6 +840,14 @@ def attributes_with_values

private :attributes_with_values

# @private Returns if validations can be and should be performed,
# and they fail
#
# @since 0.5.5
def failed_validations?(options)
options[:validate] != false && respond_to?(:valid?) && !valid?
end

# Redefine (if exists) #to_key to use #key if #id is missing
def to_key
keys = [id || key]
Expand Down
13 changes: 13 additions & 0 deletions test/test_active_model_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,17 @@ def test_dirty_tracking_previous_values
assert_equal tester.previous_changes[:email], ['joe@example.com', 'bob@example.com']
end

def test_save_without_validations
tester = ActiveUser.new(:email => 'joe@example.com', :role => nil)
assert tester.save(:validate => false), 'Validations not skipped'
end

def test_save_bang_without_validations
tester = ActiveUser.new(:email => 'joe@example.com', :role => nil)
begin
tester.save!(:validate => false)
rescue
assert false, 'Validations not skipped'
end
end
end

0 comments on commit c928da8

Please sign in to comment.