Guest User

Untitled

a guest
May 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. # Including this module causes indexing and index removal to occur in
  2. # DelayedJobs. Requires all automatic index/removal code to be enabled (the
  3. # default) to work properly. Include this module after calling searchable.
  4. #
  5. # searchable do # TODO: automatically enable after calling searchable?
  6. # text :name
  7. # end
  8. #
  9. # include DelayedIndex
  10. module DelayedIndex
  11. def self.included(receiver)
  12. receiver.extend ClassMethods
  13. receiver.send :include, InstanceMethods
  14.  
  15. # HACK: Work around some funkiness with remove_from_index, not sure why
  16. # it can't be overidden in InstanceMethods.
  17. receiver.send :alias_method, :remove_from_index, :solr_remove_from_index
  18. end
  19.  
  20. module ClassMethods
  21. # Removes an object from the index by id, the object is already destroyed
  22. # and it's data is gone. Necessary because the remove command needs to be
  23. # sent from a serializable object to work with DelayedJob.
  24. def remove_from_index_by_id(id)
  25. Sunspot.remove_by_id!(self, id)
  26. end
  27.  
  28. end
  29.  
  30. module InstanceMethods
  31. # Override the default method and make it backgrounded and auto-committed.
  32. def solr_remove_from_index
  33. self.class.send_later :remove_from_index_by_id, id
  34. end
  35.  
  36. # Override the default method and make it backgrounded and auto-committed.
  37. def solr_index
  38. send_later :solr_index!
  39. end
  40.  
  41. # Improved maybe_mark_for_auto_indexing.
  42. def maybe_mark_for_auto_indexing
  43. @marked_for_auto_indexing = new_record? ||
  44. (changed.map(&:to_s) - self.class.sunspot_options[:ignore_attribute_changes_of].to_a.map(&:to_s)).present?
  45.  
  46. true
  47. end
  48.  
  49. end
  50.  
  51. end
Add Comment
Please, Sign In to add comment