Class: ElasticRansack::Search
- Inherits:
-
Object
- Object
- ElasticRansack::Search
- 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
-
#globalize ⇒ Object
readonly
Returns the value of attribute globalize.
-
#model ⇒ Object
(also: #klass)
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#search_options ⇒ Object
readonly
Returns the value of attribute search_options.
-
#search_results ⇒ Object
readonly
Returns the value of attribute search_results.
-
#sorts ⇒ Object
readonly
Returns the value of attribute sorts.
Instance Method Summary collapse
- #add_sort(name, dir) ⇒ Object
- #format_value(v) ⇒ Object
-
#initialize(model, options, search_options) ⇒ Search
constructor
A new instance of Search.
- #method_missing(*args, &block) ⇒ Object
- #search ⇒ Object
- #translate(*args) ⇒ Object
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, , ) ||= {} .reverse_merge!(globalize: true) @model = model = .stringify_keys = || {} @sorts = [] @globalize = .delete(:globalize) sorting = .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
#globalize ⇒ Object (readonly)
Returns the value of attribute globalize.
6 7 8 |
# File 'lib/elastic_ransack/search.rb', line 6 def globalize @globalize end |
#model ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/elastic_ransack/search.rb', line 6 def end |
#search_options ⇒ Object (readonly)
Returns the value of attribute search_options.
6 7 8 |
# File 'lib/elastic_ransack/search.rb', line 6 def end |
#search_results ⇒ Object (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 |
#sorts ⇒ Object (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 |
#search ⇒ Object
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() do and_filters = [] sort do that.sorts.each do |s| by s.name, s.dir end end that..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 |