Class: Crm::Core::SearchConfigurator

Inherits:
Object
  • Object
show all
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.

Examples:

search_config = Crm::Contact.
  where('last_name', 'equals', 'Johnson').
  and('locality', 'equals', 'New York').
  and_not('language', 'equals', 'en').
  sort_by('first_name').
  sort_order('desc').
  offset(1).
  limit(3)
# => Crm::Core::SearchConfigurator

results = search_config.perform_search
# => Crm::Core::ItemEnumerator

results.length # => 3
results.total # => 17
results.map(&:first_name) # => ['Tim', 'Joe', 'Ann']

Unlimited search results

search_config = Crm::Contact.
  where('last_name', 'equals', 'Johnson')
# => Crm::Core::SearchConfigurator

results = search_config.perform_search
# => Crm::Core::ItemEnumerator

results.length # => 85
results.total # => 85
results.map(&:first_name) # => an array of 85 first names

Chainable methods collapse

Instance Method Summary collapse

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 with value.

  • contains_words - field contains the words given by value.

  • equals - field exactly corresponds to value (case insensitive).

  • is_blank - field is blank (omit value).

  • is_earlier_than - date time field is earlier than value.

  • is_later_than - date time field is later than value.

  • is_true - field is true (omit value).

Parameters:

  • field (Symbol, String)

    the attribute name.

  • condition (Symbol, String)

    the condition, e.g. :equals.

  • value (Symbol, String, Array<Symbol, String>, nil) (defaults to: nil)

    the value.

Returns:



75
76
77
78
# File 'lib/crm/core/search_configurator.rb', line 75

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.

Parameters:

  • field (Symbol, String)

    the attribute name.

  • condition (Symbol, String)

    the condition, e.g. :equals.

  • value (Symbol, String, Array<Symbol, String>, nil) (defaults to: nil)

    the value.

Returns:



90
91
92
93
# File 'lib/crm/core/search_configurator.rb', line 90

def add_negated_filter(field, condition, value = nil)
  negated_condition = "not_#{condition}"
  add_filter(field, negated_condition, value)
end

#ascSearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the ascending sort order.

Returns:



155
156
157
# File 'lib/crm/core/search_configurator.rb', line 155

def asc
  sort_order('asc')
end

#descSearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the descending sort order.

Returns:



163
164
165
# File 'lib/crm/core/search_configurator.rb', line 163

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.



173
174
175
176
177
# File 'lib/crm/core/search_configurator.rb', line 173

def each(&block)
  return enum_for(:each) unless block_given?

  perform_search.each(&block)
end

#first(n = :undefined) ⇒ Object



183
184
185
186
# File 'lib/crm/core/search_configurator.rb', line 183

def first(n = :undefined)
  return take(n) unless n == :undefined
  limit(1).perform_search.first
end

#limit(new_limit) ⇒ SearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given limit.

Parameters:

  • new_limit (Fixnum)

    the new limit.

Returns:



110
111
112
# File 'lib/crm/core/search_configurator.rb', line 110

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.

Parameters:

  • new_offset (Fixnum)

    the new offset.

Returns:



127
128
129
# File 'lib/crm/core/search_configurator.rb', line 127

def offset(new_offset)
  SearchConfigurator.new(@settings.merge(offset: new_offset))
end

#perform_searchItemEnumerator

Executes the search request based on this configuration.

Returns:



46
47
48
49
50
51
52
53
54
55
# 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],
  )
end

#query(new_query) ⇒ SearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given query.

Parameters:

  • new_query (String)

    the new query.

Returns:



101
102
103
# File 'lib/crm/core/search_configurator.rb', line 101

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.

Parameters:

  • new_sort_by (String)

    See Crm.search for the list of supported sort_by values.

Returns:



137
138
139
# File 'lib/crm/core/search_configurator.rb', line 137

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.

Parameters:

  • new_sort_order (String)

    See Crm.search for the list of supported sort_order values.

Returns:



147
148
149
# File 'lib/crm/core/search_configurator.rb', line 147

def sort_order(new_sort_order)
  SearchConfigurator.new(@settings.merge(sort_order: new_sort_order))
end

#take(n) ⇒ Object



179
180
181
# File 'lib/crm/core/search_configurator.rb', line 179

def take(n)
  limit(n).perform_search.to_a
end

#totalFixnum

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.

Returns:

  • (Fixnum)

    the total.



193
194
195
# File 'lib/crm/core/search_configurator.rb', line 193

def total
  perform_search.total
end

#unlimitedSearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration without limiting the number of search results.

Returns:



118
119
120
# File 'lib/crm/core/search_configurator.rb', line 118

def unlimited
  limit(:none)
end