Skip to content

Commit 1fd6c0b

Browse files
committed
[MODEL] Added the :validate option to the save method for models
article.save validate: false
1 parent eb47c7d commit 1fd6c0b

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ module InstanceMethods
4444
# @return [Hash,FalseClass] The Elasticsearch response as a Hash or `false`
4545
#
4646
def save(options={})
47-
return false unless valid?
47+
unless options.delete(:validate) == false
48+
return false unless valid?
49+
end
50+
4851
run_callbacks :save do
4952
options.update id: self.id
5053
options.update index: self._index if self._index

elasticsearch-persistence/test/integration/model/model_basic_test.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ::Person
5151

5252
should "save and find the object" do
5353
person = Person.new name: 'John Smith', birthday: Date.parse('1970-01-01')
54-
person.save
54+
assert person.save
5555

5656
assert_not_nil person.id
5757
document = Person.find(person.id)
@@ -63,6 +63,20 @@ class ::Person
6363
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id
6464
end
6565

66+
should "not save an invalid object" do
67+
person = Person.new name: nil
68+
assert ! person.save
69+
end
70+
71+
should "save an invalid object with the :validate option" do
72+
person = Person.new name: nil, salary: 100
73+
assert person.save validate: false
74+
75+
assert_not_nil person.id
76+
document = Person.find(person.id)
77+
assert_equal 100, document.salary
78+
end
79+
6680
should "delete the object" do
6781
person = Person.create name: 'John Smith', birthday: Date.parse('1970-01-01')
6882

elasticsearch-persistence/test/unit/model_store_test.rb

+31-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class DummyStoreModel
8080
end
8181
.returns({'_id' => 'abc123'})
8282

83-
assert ! subject.persisted?
84-
8583
assert subject.save
86-
assert subject.persisted?
8784
end
8885

8986
should "save the model and set the ID" do
@@ -108,6 +105,37 @@ class DummyStoreModel
108105
assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
109106
end
110107

108+
should "not save an invalid model" do
109+
@gateway
110+
.expects(:save)
111+
.never
112+
113+
subject.instance_eval do
114+
def valid?; false; end;
115+
end
116+
117+
assert ! subject.save
118+
assert ! subject.persisted?
119+
end
120+
121+
should "skip the validation with the :validate option" do
122+
@gateway
123+
.expects(:save)
124+
.with do |object, options|
125+
assert_equal subject, object
126+
assert_equal nil, options[:id]
127+
true
128+
end
129+
.returns({'_id' => 'abc123'})
130+
131+
subject.instance_eval do
132+
def valid?; false; end;
133+
end
134+
135+
assert subject.save validate: false
136+
assert subject.persisted?
137+
end
138+
111139
should "pass the options to gateway" do
112140
@gateway
113141
.expects(:save)

0 commit comments

Comments
 (0)