Class: Haystack::BaseSearcher

Inherits:
Object
  • Object
show all
Defined in:
app/models/haystack/base_searcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseSearcher

performs ActiveRecord-like mass assignment of attributes



8
9
10
# File 'app/models/haystack/base_searcher.rb', line 8

def initialize(attributes = {})
  mass_assign(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)

method_missing will, when passed “_conditions” methods, create an equality- based AR:Relation



55
56
57
58
59
60
61
62
63
64
# File 'app/models/haystack/base_searcher.rb', line 55

def method_missing(meth, *args, &block)
  # NOTE: We are intentionally setting regex_match_data var here, NOT ==
  if regex_match_data = /(.*?)_conditions/.match(meth) # match returns nil
    hash = {}
    hash[regex_match_data[1]] = self.instance_variable_get(("@"+regex_match_data[1]))
    self.class::BASE.send(:where, hash) # self.class::BASE = BASE for the subclass
  else
    super
  end
end

Instance Method Details

#dataObject

Caches the rows



13
14
15
# File 'app/models/haystack/base_searcher.rb', line 13

def data
  @data ||= perform_query
end

#to_sObject



17
18
19
# File 'app/models/haystack/base_searcher.rb', line 17

def to_s
  conditions
end