Permalink
Browse files

Added options to save to make it more compatible with ActiveRecord:

:validate => false
  • Loading branch information...
1 parent 28dbfe5 commit 60271f0b6e6b48ce98e6d623508120683e981665 @benjackson benjackson committed Nov 8, 2010
Showing with 23 additions and 11 deletions.
  1. +11 −9 lib/neo4j/rails/model.rb
  2. +2 −2 spec/rails/model_spec.rb
  3. +10 −0 spec/support/shared_model_examples.rb
View
@@ -155,9 +155,9 @@ def reject_if?(proc_or_symbol, attr)
end
end
- def save
+ def save(*args)
_run_save_callbacks do
- if create_or_update_node
+ if create_or_update_node(*args)
true
else
# if not valid we should rollback the transaction so that the changes does not take place.
@@ -168,11 +168,13 @@ def save
end
end
- def create_or_update_node
- if valid?(:save)
+ def create_or_update_node(options = {})
+ options.reverse_merge!({ :validate => true })
+
+ if options[:validate] == false || valid?(:save)
if new_record?
_run_create_callbacks do
- if valid?(:create)
+ if options[:validate] == false || valid?(:create)
node = Neo4j::Node.new(props)
return false unless _java_node.save_nested(node)
init_on_load(node)
@@ -184,7 +186,7 @@ def create_or_update_node
end
else
_run_update_callbacks do
- if valid?(:update)
+ if options[:validate] == false || valid?(:update)
clear_changes
self.updated_at = DateTime.now if Neo4j::Config[:timestamps] && respond_to?(:updated_at)
true
@@ -204,8 +206,8 @@ def reload(options = nil)
reload_from_database or set_deleted_properties and return self
end
- def save!
- raise RecordInvalidError.new(self) unless save
+ def save!(*args)
+ raise RecordInvalidError.new(self) unless save(*args)
end
# Returns if the record is persisted, i.e. it’s not a new record and it was not destroyed
@@ -241,7 +243,7 @@ def destroyed?()
@_deleted
end
- tx_methods :destroy, :create_or_update_node, :update_nested_attributes
+ tx_methods :destroy, :create_or_update_node, :update_attributes, :update_attributes!, :update_nested_attributes
# --------------------------------------
# Class Methods
View
@@ -318,7 +318,7 @@ def mark_saved
end
end
- describe "Neo4j::UniquenessValidator" do
+ describe "Neo4j::Rails::Validations::UniquenessValidator" do
before(:all) do
class ValidThing < Neo4j::Model
index :email
@@ -328,7 +328,7 @@ class ValidThing < Neo4j::Model
end
it "have correct kind" do
- Neo4j::Validations::UniquenessValidator.kind.should == :uniqueness
+ Neo4j::Rails::Validations::UniquenessValidator.kind.should == :uniqueness
end
it "should not allow to create two nodes with unique fields" do
a = @klass.create(:email => 'abc@se.com')
@@ -109,6 +109,16 @@
subject.id.should be_nil
end
end
+
+ context "without validation" do
+ it "should save ok" do
+ subject.save(:validate => false).should == true
+ end
+
+ it "shouldn't cause an exception while saving" do
+ lambda { subject.save!(:validate => false) }.should_not raise_error
+ end
+ end
end
share_examples_for "a destroyable model" do

0 comments on commit 60271f0

Please sign in to comment.