Skip to content
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 @@ -112,6 +112,7 @@ def destroy(options={})
# @return [Hash] The Elasticsearch response as a Hash
#
def update(attributes={}, options={})
attributes = self.attributes.merge(attributes)
Copy link

Choose a reason for hiding this comment

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

This should be self.attributes = self.attributes.merge(attributes)

unless options.delete(:validate) == false
return false unless valid?
end
Expand Down
33 changes: 24 additions & 9 deletions elasticsearch-persistence/test/unit/model_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DummyStoreModel
attribute :count, Integer, default: 0
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }

validates :title, presence: true
end

setup do
Expand Down Expand Up @@ -65,7 +67,7 @@ class DummyStoreModel
$stderr.expects(:puts).with('CREATED')
$stderr.expects(:puts).with('SAVED')

DummyStoreModelWithCallback.create name: 'test'
DummyStoreModelWithCallback.create title: 'test'
end
end

Expand Down Expand Up @@ -163,7 +165,7 @@ def valid?; false; end;
DummyStoreModelWithCallback.after_save { $stderr.puts "SAVED" }

$stderr.expects(:puts).with('SAVED')
d = DummyStoreModelWithCallback.new name: 'Test'
d = DummyStoreModelWithCallback.new title: 'Test'
d.save
end

Expand All @@ -176,7 +178,7 @@ def valid?; false; end;
end
.returns({'_id' => 'abc'})

d = DummyStoreModel.new name: 'Test'
d = DummyStoreModel.new title: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.save
Expand All @@ -191,7 +193,7 @@ def valid?; false; end;
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})

d = DummyStoreModel.new name: 'Test'
d = DummyStoreModel.new title: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.save
Expand Down Expand Up @@ -311,7 +313,7 @@ def valid?; false; end;
assert subject.update( {}, { script: 'EXEC' } )
end

should "not update an invalid model" do
should "not update an already invalid model" do
@gateway
.expects(:update)
.never
Expand All @@ -323,6 +325,19 @@ def valid?; false; end;
assert ! subject.update(title: 'INVALID')
end

should "not update an invalid model" do

subject.instance_eval do
def persisted?; true; end;
end

@gateway
.expects(:update)
.never

assert ! subject.update(title: nil)
end

should "skip the validation with the :validate option" do
subject.expects(:persisted?).returns(true).at_least_once
subject.expects(:id).returns('abc123').at_least_once
Expand Down Expand Up @@ -376,7 +391,7 @@ def valid?; false; end;
DummyStoreModelWithCallback.after_update { $stderr.puts "UPDATED" }

$stderr.expects(:puts).with('UPDATED')
d = DummyStoreModelWithCallback.new name: 'Test'
d = DummyStoreModelWithCallback.new title: 'Test'
d.expects(:persisted?).returns(true)
d.update name: 'Update'
end
Expand All @@ -390,7 +405,7 @@ def valid?; false; end;
end
.returns({'_id' => 'abc'})

d = DummyStoreModel.new name: 'Test'
d = DummyStoreModel.new title: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.expects(:persisted?).returns(true)
Expand All @@ -407,12 +422,12 @@ def valid?; false; end;
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})

d = DummyStoreModel.new name: 'Test'
d = DummyStoreModel.new title: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.expects(:persisted?).returns(true)

d.update name: 'Update'
d.update title: 'Update'

assert_equal 'foo', d._index
assert_equal 'bar', d._type
Expand Down