File tree 3 files changed +50
-5
lines changed
elasticsearch-persistence
lib/elasticsearch/persistence/model
3 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,10 @@ module InstanceMethods
44
44
# @return [Hash,FalseClass] The Elasticsearch response as a Hash or `false`
45
45
#
46
46
def save ( options = { } )
47
- return false unless valid?
47
+ unless options . delete ( :validate ) == false
48
+ return false unless valid?
49
+ end
50
+
48
51
run_callbacks :save do
49
52
options . update id : self . id
50
53
options . update index : self . _index if self . _index
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ class ::Person
51
51
52
52
should "save and find the object" do
53
53
person = Person . new name : 'John Smith' , birthday : Date . parse ( '1970-01-01' )
54
- person . save
54
+ assert person . save
55
55
56
56
assert_not_nil person . id
57
57
document = Person . find ( person . id )
@@ -63,6 +63,20 @@ class ::Person
63
63
assert_not_nil Elasticsearch ::Persistence . client . get index : 'people' , type : 'person' , id : person . id
64
64
end
65
65
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
+
66
80
should "delete the object" do
67
81
person = Person . create name : 'John Smith' , birthday : Date . parse ( '1970-01-01' )
68
82
Original file line number Diff line number Diff line change @@ -80,10 +80,7 @@ class DummyStoreModel
80
80
end
81
81
. returns ( { '_id' => 'abc123' } )
82
82
83
- assert ! subject . persisted?
84
-
85
83
assert subject . save
86
- assert subject . persisted?
87
84
end
88
85
89
86
should "save the model and set the ID" do
@@ -108,6 +105,37 @@ class DummyStoreModel
108
105
assert_equal Time . parse ( '2014-01-01T00:00:00Z' ) , subject . updated_at
109
106
end
110
107
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
+
111
139
should "pass the options to gateway" do
112
140
@gateway
113
141
. expects ( :save )
You can’t perform that action at this time.
0 commit comments