Skip to content

Validate .update method #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def destroy(options={})
# @return [Hash] The Elasticsearch response as a Hash
#
def update(attributes={}, options={})
unless options.delete(:validate) == false
return false unless valid?
end
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?

run_callbacks :update do
Expand Down
37 changes: 37 additions & 0 deletions elasticsearch-persistence/test/unit/model_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,43 @@ def valid?; false; end;
assert subject.update( {}, { script: 'EXEC' } )
end

should "not update an invalid model" do
subject.expects(:persisted?).returns(true)
subject.expects(:id).returns('abc123').at_least_once

@gateway
.expects(:update)
.never

subject.instance_eval do
def valid?; false; end;
end

assert ! subject.update
assert ! subject.persisted?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you run the test? subject.expects(:persisted?).returns(true) ... :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test suite of this project has never worked for me sadly so I haven't been able to outside of Travis CI

end

should "skip the validation with the :validate option" do
subject.expects(:persisted?).returns(true)
subject.expects(:id).returns('abc123').at_least_once

@gateway
.expects(:update)
.with do |object, options|
assert_equal subject, object
assert_equal nil, options[:id]
true
end
.returns({'_id' => 'abc123'})

subject.instance_eval do
def valid?; false; end;
end

assert subject.update validate: false
assert subject.persisted?
end

should "pass the options to gateway" do
subject.expects(:persisted?).returns(true)

Expand Down