Class: Searcher::Configuration

Inherits:
Object
  • Object
show all
Defined in:
app/models/searcher/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(searcher, &block) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
15
16
17
18
# File 'app/models/searcher/configuration.rb', line 10

def initialize(searcher, &block)
  self.searcher = searcher
  self.scopes = {}
  self.facets = {}
  self.models = []
  scope :runtime
  scope :default
  instance_eval &block if block
end

Instance Attribute Details

#facetsObject

Returns the value of attribute facets.



4
5
6
# File 'app/models/searcher/configuration.rb', line 4

def facets
  @facets
end

#models(*names_or_clases) ⇒ Object



27
28
29
# File 'app/models/searcher/configuration.rb', line 27

def models(*names_or_clases)
  self.models = [*names_or_clases]
end

#scopesObject

Returns the value of attribute scopes.



2
3
4
# File 'app/models/searcher/configuration.rb', line 2

def scopes
  @scopes
end

#searcherObject

Returns the value of attribute searcher.



2
3
4
# File 'app/models/searcher/configuration.rb', line 2

def searcher
  @searcher
end

Instance Method Details

#facet(name, &block) ⇒ Object



77
78
79
# File 'app/models/searcher/configuration.rb', line 77

def facet(name, &block)
  facets[name] = block
end

#group(name) ⇒ Object



87
88
89
90
91
# File 'app/models/searcher/configuration.rb', line 87

def group(name)
  scope do
    group(name)
  end
end

#keywords(field, options = {}, &block) ⇒ Object



20
21
22
23
24
25
# File 'app/models/searcher/configuration.rb', line 20

def keywords(field, options={}, &block)
  search_object.create_field(field, options)
  scope do |sunspot|
    sunspot.fulltext(search_object.send(field), &block) if search_object.send(field).presence
  end
end

#more_like_this(*args) ⇒ Object



93
94
95
# File 'app/models/searcher/configuration.rb', line 93

def more_like_this(*args)
  @more_like_this = args
end

#more_like_this?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/searcher/configuration.rb', line 97

def more_like_this?
  !!@more_like_this
end

#more_like_this_paramsObject



101
102
103
# File 'app/models/searcher/configuration.rb', line 101

def more_like_this_params
  @more_like_this
end

#property(field, options = {}, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/searcher/configuration.rb', line 37

def property(field, options={}, &block)
  modificators = [*options[:modificator]] + [*options[:modificators]]

  modificators.each do |modificator|

    request_field = [field, modificator].compact.join('_')

    search_object.create_field(request_field, options)

    if block
      scope &block
    else
      scope do |sunspot|
        sunspot.with(field).send(modificator, search_object.send(request_field)) if search_object.send(request_field).presence
      end
    end
  end

  if modificators.empty?
    search_object.create_field(field, options)
    if block
      scope &block
    else
      scope do |sunspot|
        if (value = search_object.send(field)).present?
          if value.is_a? Array
            sunspot.all_of do |all_of|
              value.each do |v|
                all_of.with(field, v)
              end
            end
          else
            sunspot.with(field, value)
          end
        end
      end
    end
  end
end

#scope(name = :default, &block) ⇒ Object



81
82
83
84
85
# File 'app/models/searcher/configuration.rb', line 81

def scope(name=:default, &block)
  scopes[name] ||= []
  scopes[name] << block if block
  searcher
end

#search_modelsObject



31
32
33
34
35
# File 'app/models/searcher/configuration.rb', line 31

def search_models
  @search_models ||= @models.map do |name_or_class|
    name_or_class.is_a?(Class) ? name_or_class : name_or_class.to_s.classify.constantize
  end
end