Module: SunspotAssociation::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/sunspot_association/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#reindex_sunspot_association!(object) ⇒ Object

Reindexes an association using Sunspot.index



101
102
103
104
105
106
# File 'lib/sunspot_association/model.rb', line 101

def reindex_sunspot_association!(object)
  return false unless self.class.sunspot_associate?
  return false unless self.respond_to?(object)

  Sunspot.index self.send(object)
end

#reindex_sunspot_association?(object) ⇒ Boolean

Checks if an association should be reindexed

> reindex_sunspot_association?(:orders) => true

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sunspot_association/model.rb', line 86

def reindex_sunspot_association?(object)
  return false unless self.class.sunspot_associate?

  if (config = (self.class.sunspot_association_configuration || {})[object]).present?
    if config[:fields].present?
      return ! (changed & (config[:fields] || []).map(&:to_s)).empty?
    else
      return changed.any?
    end
  end

  false
end

#reindex_sunspot_associations!(event) ⇒ Object

Main callback method, checks if it should reindex an association



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sunspot_association/model.rb', line 69

def reindex_sunspot_associations!(event)
  return false unless self.class.sunspot_associate?

  self.class.sunspot_association_configuration.each do |object, config|
    case event
    when :create
      reindex_sunspot_association!(object) if config[:on_create]
    when :update
      reindex_sunspot_association!(object) if reindex_sunspot_association?(object)
    when :destroy
      reindex_sunspot_association!(object)
    end
  end
end