@@ -101,8 +101,9 @@ def __init__(self, model, client):
101
101
should_index_attr_name = self .should_index
102
102
if isinstance (attr , bool ):
103
103
self .should_index = lambda instance : getattr (instance , should_index_attr_name ) is True
104
+ self .should_index ._is_default = True
104
105
else :
105
- raise AlgoliaIndexError ('{} should be a callable or a boolean attribute.' .format (
106
+ raise AlgoliaIndexError ('{} should be a bound callable or a boolean attribute.' .format (
106
107
self .should_index ))
107
108
else :
108
109
raise AlgoliaIndexError ('{} is not an attribute of {}.' .format (
@@ -194,7 +195,7 @@ def _build_object(self, instance):
194
195
def update_obj_index (self , instance ):
195
196
'''Update the object.'''
196
197
if self .should_index :
197
- if not self .should_index (instance ):
198
+ if not self ._should_really_index (instance ):
198
199
# Should not index, but since we don't now the state of the
199
200
# instance, we need to send a DELETE request to ensure that if
200
201
# the instance was previously indexed, it will be removed.
@@ -206,8 +207,20 @@ def update_obj_index(self, instance):
206
207
logger .debug ('UPDATE %s FROM %s' , obj ['objectID' ], self .model )
207
208
208
209
def _should_index (self , instance ):
209
- '''Return true if the object should be indexed.'''
210
- return self .should_index (instance ) if self .should_index else True
210
+ """Return true if the object should be indexed (including when self.should_index is not set)."""
211
+ if self .should_index :
212
+ return self ._should_really_index (instance )
213
+ else :
214
+ return True
215
+
216
+ def _should_really_index (self , instance ):
217
+ """Return true if according to should_index the object should be indexed."""
218
+ if hasattr (self .should_index , "__self__" ) or hasattr (self .should_index , '_is_default' ):
219
+ # bound method or lambda, let's use instance
220
+ return self .should_index (instance )
221
+ else :
222
+ # unbound method, simply call without arguments
223
+ return self .should_index ()
211
224
212
225
def delete_obj_index (self , instance ):
213
226
'''Delete the object.'''
0 commit comments