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:



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.

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:



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

#ascSearchConfigurator

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

Returns:



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

def asc
  sort_order('asc')
end

#descSearchConfigurator

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

Returns:



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_deletedSearchConfigurator

Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and excluding deleted items.

Returns:



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.

Parameters:

  • new_include_deleted (Boolean) (defaults to: true)

    whether to include deleted items in the results.

Returns:



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.

Parameters:

  • new_limit (Fixnum)

    the new limit.

Returns:



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.

Parameters:

  • new_offset (Fixnum)

    the new offset.

Returns:



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_searchItemEnumerator

Executes the search request based on this configuration.

Returns:



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.

Parameters:

  • new_query (String)

    the new query.

Returns:



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.

Parameters:

  • new_sort_by (String)

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

Returns:



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.

Parameters:

  • new_sort_order (String)

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

Returns:



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

#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.



211
212
213
# File 'lib/crm/core/search_configurator.rb', line 211

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:



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

def unlimited
  limit(:none)
end