Module: Sunspot::SubmodelIndex::InstanceMethods

Defined in:
lib/sunspot_submodel_index/submodel_index.rb

Instance Method Summary collapse

Instance Method Details

#call_parent_solr_indexObject


54
55
56
57
58
59
60
61
62
63
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 54

def call_parent_solr_index
  if self.respond_to?(self._sunspot_submodel_options[:parent]) && !self.send(self._sunspot_submodel_options[:parent]).nil?
    self.send(self._sunspot_submodel_options[:parent],true) if self._sunspot_submodel_options[:force_association_reload]
    if self.send(self._sunspot_submodel_options[:parent]).is_a?(Enumerable)
      self.send(self._sunspot_submodel_options[:parent]).each {|item| item.solr_index }
    else
      self.send(self._sunspot_submodel_options[:parent]).solr_index 
    end
  end
end

#check_parent_solr_if_statementObject


65
66
67
68
69
70
71
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 65

def check_parent_solr_if_statement
  if self._sunspot_submodel_options[:if] && self._sunspot_submodel_options[:if].instance_of?(Proc)
    self._sunspot_submodel_options[:if].call(self)
  else
    true
  end
end

#mark_for_parent_solr_indexObject

to run before a save to see if this is a new record, or if fields I care about changed.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 74

def mark_for_parent_solr_index
  #only mark for index if the record is new, or if fields changed that I care about
  #check the if proc first
  return unless check_parent_solr_if_statement
  if self._sunspot_submodel_options[:included_attributes]
    fields_changed = !(self.changed.map {|k| k.to_sym} & self._sunspot_submodel_options[:included_attributes]).empty?
  elsif self._sunspot_submodel_options[:ignored_attributes]
    fields_changed = !(self.changed.map {|k| k.to_sym} - self._sunspot_submodel_options[:ignored_attributes]).empty?
  else
    fields_changed = true
  end
  if new_record? || fields_changed
    @marked_for_parent_solr_indexing = true
  end
end

#parent_solr_indexObject

call reindex if I need too


91
92
93
94
95
96
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 91

def parent_solr_index
  if @marked_for_parent_solr_indexing
    call_parent_solr_index
    remove_instance_variable(:@marked_for_parent_solr_indexing)
  end
end

#parent_solr_index_on_destroyObject

always call reindex on destroy


99
100
101
102
# File 'lib/sunspot_submodel_index/submodel_index.rb', line 99

def parent_solr_index_on_destroy
  return unless check_parent_solr_if_statement
  call_parent_solr_index
end