Class: ElasticRansack::Search

Inherits:
Object
  • Object
show all
Includes:
Naming, Enumerable
Defined in:
lib/elastic_ransack/search.rb

Constant Summary collapse

DATETIME_REGEXP =
/\d{2}\.\d{2}.\d{4} \d{2}\:\d{2}/
DATE_REGEXP =
/\d{2}\.\d{2}.\d{4}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Naming

included, #persisted?, #to_key, #to_model, #to_param

Constructor Details

#initialize(model, options, search_options) ⇒ Search



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elastic_ransack/search.rb', line 17

def initialize(model, options, search_options)
  search_options ||= {}
  search_options.reverse_merge!(globalize: true)
  @model = model
  @options = options.stringify_keys
  @search_options = search_options || {}
  @sorts = []
  @globalize = @search_options.delete(:globalize)
  sorting = @options.delete('s')
  if sorting.blank?
    add_sort('id', 'desc')
  else
    sorting_split = sorting.split(/\s+/, 2)
    add_sort(sorting_split[0], sorting_split[1] || 'asc')
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



99
100
101
# File 'lib/elastic_ransack/search.rb', line 99

def method_missing(*args, &block)
  @model.send(*args, &block)
end

Instance Attribute Details

#globalizeObject (readonly)

Returns the value of attribute globalize.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def globalize
  @globalize
end

#modelObject (readonly) Also known as: klass

Returns the value of attribute model.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def options
  @options
end

#search_optionsObject (readonly)

Returns the value of attribute search_options.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def search_options
  @search_options
end

#search_resultsObject (readonly)

Returns the value of attribute search_results.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def search_results
  @search_results
end

#sortsObject (readonly)

Returns the value of attribute sorts.



6
7
8
# File 'lib/elastic_ransack/search.rb', line 6

def sorts
  @sorts
end

Instance Method Details

#add_sort(name, dir) ⇒ Object



34
35
36
# File 'lib/elastic_ransack/search.rb', line 34

def add_sort(name, dir)
  @sorts << OpenStruct.new(name: name, dir: dir)
end

#format_value(v) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/elastic_ransack/search.rb', line 89

def format_value(v)
  if v =~ DATETIME_REGEXP
    Date.parse(v)
  elsif v =~ DATE_REGEXP
    Time.parse(v)
  else
    v
  end
end

#searchObject



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
76
77
78
79
80
81
82
# File 'lib/elastic_ransack/search.rb', line 38

def search
  @search_results ||= begin
    that = self
    query_string = []
    tire.search(@search_options) do
      and_filters = []
      sort do
        that.sorts.each do |s|
          by s.name, s.dir
        end
      end

      that.options.each do |k, v|
        next if v.blank?

        if k == 'q_cont' || k == 'q_eq'
          query_string << "#{v.lucene_escape}" if v.present?
          next
        end

        if k =~ /^(.+)_cont$/
          attr = $1
          attr = "#{$1.sub(/^translations_/, '')}_#{I18n.locale}" if that.globalize && attr =~ /^translations_(.+)/
          attr_query = [
              v.split.map { |part| "#{attr}:*#{part.lucene_escape}*" }.join(' AND '),
              v.split.map { |part| "#{attr}:\"#{part.lucene_escape}\"" }.join(' AND ')
          ]
          query_string << attr_query.map { |q| "(#{q})" }.join(' OR ')
          next
        else
          v = that.format_value(v)
        end

        ElasticRansack.predicates.each do |predicate|
          if k =~ predicate.regexp
            and_filters << predicate.query.call($1, v)
          end
        end
      end

      query { string query_string.join(' ') } unless query_string.blank?
      filter(:and, filters: and_filters) unless and_filters.blank?
    end
  end
end

#translate(*args) ⇒ Object



85
86
87
# File 'lib/elastic_ransack/search.rb', line 85

def translate(*args)
  model.human_attribute_name(args.first)
end