Permalink
Browse files

Added option for bypassing validation on save

  • Loading branch information...
1 parent 5670d29 commit 7047b61493d97dfbced1034ec1abac7b65a2b46a @chicks chicks committed Nov 26, 2011
Showing with 22 additions and 8 deletions.
  1. +6 −3 lib/sugarcrm/attributes/attribute_methods.rb
  2. +8 −5 lib/sugarcrm/base.rb
  3. +8 −0 test/test_sugarcrm.rb
@@ -164,9 +164,12 @@ def attribute_for_inspect(attr_name)
# Wrapper for invoking save on modified_attributes
# sets the id if it's a new record
- def save_modified_attributes!
- # Complain if we aren't valid
- raise InvalidRecord, @errors.full_messages.join(", ") unless valid?
+ def save_modified_attributes!(opts={})
+ options = { :validate => true }.merge(opts)
+ if options[:validate]
+ # Complain if we aren't valid
+ raise InvalidRecord, @errors.full_messages.join(", ") unless valid?
+ end
# Send the save request
response = self.class.session.connection.set_entry(self.class._module.name, serialize_modified_attributes)
# Complain if we don't get a parseable response back
View
@@ -157,11 +157,14 @@ def to_s
# Saves the current object, checks that required fields are present.
# returns true or false
- def save
+ def save(opts={})
+ options = { :validate => true }.merge(opts)
return false if !(new_record? || changed?)
- return false if !valid?
+ if options[:validate]
+ return false if !valid?
+ end
begin
- save!
+ save!(options)
rescue
return false
end
@@ -170,8 +173,8 @@ def save
# Saves the current object, and any modified associations.
# Raises an exceptions if save fails for any reason.
- def save!
- save_modified_attributes!
+ def save!(opts={})
+ save_modified_attributes!(opts)
save_modified_associations!
true
end
View
@@ -191,6 +191,13 @@ class TestSugarCRM < ActiveSupport::TestCase
should "respond to #blank?" do
assert !SugarCRM::User.first.blank?
end
+
+ should "bypass validation when #save(:validate => false)" do
+ u = SugarCRM::User.new
+ u.last_name = "doe"
+ assert u.save({:validate => false})
+ assert u.delete
+ end
end
# TODO: Fix this test so it creates the Note properly before asserting.
@@ -202,4 +209,5 @@ class TestSugarCRM < ActiveSupport::TestCase
assert_equal note.parent_type.singularize, parent.class.to_s.split('::').last
end
end
+
end

0 comments on commit 7047b61

Please sign in to comment.