Class: Crm::Core::SearchConfigurator
- Inherits:
-
Object
- Object
- Crm::Core::SearchConfigurator
- Includes:
- Enumerable
- Defined in:
- lib/crm/core/search_configurator.rb
Overview
SearchConfigurator
provides methods to incrementally configure a search request using chainable methods and to perform this search.
Chainable methods collapse
-
#add_filter(field, condition, value = nil) ⇒ SearchConfigurator
(also: #and)
Returns a new SearchConfigurator constructed by combining this configuration and the new filter.
-
#add_negated_filter(field, condition, value = nil) ⇒ SearchConfigurator
(also: #and_not)
Returns a new SearchConfigurator constructed by combining this configuration and the new negated filter.
-
#asc ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the ascending sort order.
-
#desc ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the descending sort order.
-
#exclude_deleted ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration and excluding deleted items.
-
#include_deleted(new_include_deleted = true) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given
include_deleted
flag. -
#limit(new_limit) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given limit.
-
#offset(new_offset) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given offset.
-
#query(new_query) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given query.
-
#sort_by(new_sort_by) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given sort criterion.
-
#sort_order(new_sort_order) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given sort order.
-
#unlimited ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration without limiting the number of search results.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over the search results.
- #first(n = :undefined) ⇒ Object
-
#initialize(settings = {}) ⇒ SearchConfigurator
constructor
A new instance of SearchConfigurator.
-
#perform_search ⇒ ItemEnumerator
Executes the search request based on this configuration.
- #take(n) ⇒ Object
-
#total ⇒ Fixnum
Returns the total number of items that match this search configuration.
Constructor Details
#initialize(settings = {}) ⇒ SearchConfigurator
Returns a new instance of SearchConfigurator.
37 38 39 40 41 |
# File 'lib/crm/core/search_configurator.rb', line 37 def initialize(settings = {}) @settings = { filters: [], }.merge(settings) end |
Instance Method Details
#add_filter(field, condition, value = nil) ⇒ SearchConfigurator Also known as: and
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and the new filter.
Supported conditions:
-
contains_word_prefixes
-field
contains words starting withvalue
. -
contains_words
-field
contains the words given byvalue
. -
equals
-field
exactly corresponds tovalue
(case insensitive). -
is_blank
-field
is blank (omitvalue
). -
is_earlier_than
- date timefield
is earlier thanvalue
. -
is_later_than
- date timefield
is later thanvalue
. -
is_true
-field
is true (omitvalue
).
76 77 78 79 |
# File 'lib/crm/core/search_configurator.rb', line 76 def add_filter(field, condition, value = nil) new_filter = Array(@settings[:filters]) + [{field: field, condition: condition, value: value}] SearchConfigurator.new(@settings.merge(filters: new_filter)) end |
#add_negated_filter(field, condition, value = nil) ⇒ SearchConfigurator Also known as: and_not
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and the new negated filter.
All filters (and their conditions) passed to #add_filter can be negated.
91 92 93 94 |
# File 'lib/crm/core/search_configurator.rb', line 91 def add_negated_filter(field, condition, value = nil) negated_condition = "not_#{condition}" add_filter(field, negated_condition, value) end |
#asc ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the ascending sort order.
156 157 158 |
# File 'lib/crm/core/search_configurator.rb', line 156 def asc sort_order('asc') end |
#desc ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the descending sort order.
164 165 166 |
# File 'lib/crm/core/search_configurator.rb', line 164 def desc sort_order('desc') end |
#each(&block) ⇒ Object
Iterates over the search results. Implicitly triggers #perform_search and caches its result. See ItemEnumerator#each for details.
191 192 193 194 195 |
# File 'lib/crm/core/search_configurator.rb', line 191 def each(&block) return enum_for(:each) unless block_given? perform_search.each(&block) end |
#exclude_deleted ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and excluding deleted items.
181 182 183 |
# File 'lib/crm/core/search_configurator.rb', line 181 def exclude_deleted include_deleted(false) end |
#first(n = :undefined) ⇒ Object
201 202 203 204 |
# File 'lib/crm/core/search_configurator.rb', line 201 def first(n = :undefined) return take(n) unless n == :undefined limit(1).perform_search.first end |
#include_deleted(new_include_deleted = true) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given include_deleted
flag.
173 174 175 |
# File 'lib/crm/core/search_configurator.rb', line 173 def include_deleted(new_include_deleted = true) SearchConfigurator.new(@settings.merge(include_deleted: new_include_deleted)) end |
#limit(new_limit) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given limit.
111 112 113 |
# File 'lib/crm/core/search_configurator.rb', line 111 def limit(new_limit) SearchConfigurator.new(@settings.merge(limit: new_limit)) end |
#offset(new_offset) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given offset.
128 129 130 |
# File 'lib/crm/core/search_configurator.rb', line 128 def offset(new_offset) SearchConfigurator.new(@settings.merge(offset: new_offset)) end |
#perform_search ⇒ ItemEnumerator
Executes the search request based on this configuration.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/crm/core/search_configurator.rb', line 46 def perform_search @perform_search ||= Crm.search( filters: @settings[:filters], query: @settings[:query], limit: @settings[:limit], offset: @settings[:offset], sort_by: @settings[:sort_by], sort_order: @settings[:sort_order], include_deleted: @settings[:include_deleted] ) end |
#query(new_query) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given query.
102 103 104 |
# File 'lib/crm/core/search_configurator.rb', line 102 def query(new_query) SearchConfigurator.new(@settings.merge(query: new_query)) end |
#sort_by(new_sort_by) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given sort criterion.
138 139 140 |
# File 'lib/crm/core/search_configurator.rb', line 138 def sort_by(new_sort_by) SearchConfigurator.new(@settings.merge(sort_by: new_sort_by)) end |
#sort_order(new_sort_order) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given sort order.
148 149 150 |
# File 'lib/crm/core/search_configurator.rb', line 148 def sort_order(new_sort_order) SearchConfigurator.new(@settings.merge(sort_order: new_sort_order)) end |
#take(n) ⇒ Object
197 198 199 |
# File 'lib/crm/core/search_configurator.rb', line 197 def take(n) limit(n).perform_search.to_a end |
#total ⇒ Fixnum
Returns the total number of items that match this search configuration. It can be greater than limit
. Implicitly triggers #perform_search and caches its result.
211 212 213 |
# File 'lib/crm/core/search_configurator.rb', line 211 def total perform_search.total end |
#unlimited ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration without limiting the number of search results.
119 120 121 |
# File 'lib/crm/core/search_configurator.rb', line 119 def unlimited limit(:none) end |