Module: Sunspot::SubmodelIndex

Defined in:
lib/sunspot_submodel_index/submodel_index.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 4

def self.included(klass)
  klass.class_eval do
    
    # Solr Index a parent model when this model is saved or destroyed.
    #
    # ==== Options (+options+)
    # 
    # :parent<Symbol>::
    #   Method to call to access the parent to Solr index.
    # :if<Proc>::
    #   A Proc that is called before the parent index and is passed an instance of the object.
    #   Will block Solr index of the parent if false is returned.
    # :force_association_reload<Boolean>::
    #   Force a reload on the parent association for Solr index is called on the parent.
    # :include_attributes<Array>::
    #   Define only those attributes whose change should trigger a reindex of the parent.
    # :ignore_attributes<Array>::
    #   Define attributes, that should not trigger a reindex of the parent.
    #
    # ==== Example
    #
    #   class Company < ActiveRecord::Base
    #     has_many :people
    #     sunspot_submodel_index :parent => :people, :included_attributes => [:name]
    #   end
    #
    def self.sunspot_submodel_index(options = {})
      include Sunspot::SubmodelIndex::InstanceMethods
      extend Sunspot::SubmodelIndex::ClassMethods
      class_inheritable_hash :_sunspot_submodel_options
      
      options[:parent] = options[:parent].to_sym
      options[:included_attributes] =  false if options[:included_attributes].blank? #set to false if empty sent
      options[:ignored_attributes] =  false if options[:ignored_attributes].blank? #set to false if empty sent
      options[:force_association_reload] =  false if options[:force_association_reload].blank? #set to false if empty sent
      
      self._sunspot_submodel_options = options
      
      #add call backs
      before_save :mark_for_parent_solr_index
      after_save :parent_solr_index
      after_destroy :parent_solr_index_on_destroy
      
    end
    
  end
end