class Pursuit::SearchOptions

Constants

AttributeOptions

@return [Struct] The structure which holds the search options for a single attribute.

Attributes

attributes[R]

@return [Hash<Symbol, AttributeOptions>] The attributes which can be searched.

record_class[R]

@return [Class<ActiveRecord::Base>] The ‘ActiveRecord::Base` child class to search.

relations[R]

@return [Hash<Symbol, Array<Symbol>>] The attribute names of the record’s relatives which can be searched.

Public Class Methods

new(record_class, &block) click to toggle source

Create a new ‘SearchOptions` and call the passed block to setup the options.

@params record_class [Class<ActiveRecord::Base>] @params block [Proc]

# File lib/pursuit/search_options.rb, line 26
def initialize(record_class, &block)
  @record_class = record_class
  @relations = {}
  @attributes = {}

  block.call(self) if block
end

Public Instance Methods

attribute(term_name, attribute_name = nil, keyed: true, unkeyed: true, &block) click to toggle source

Add an attribute to search.

@param term_name [Symbol] The keyed search term (can be an existing attribute, or a custom value when

passing either the `attribute_name` or a block returning an Arel node).

@param attribute_name [Symbol] The attribute name to search (defaults to the keyword, when left blank and no

block is passed).

@param keyed [Boolean] ‘true` when the attribute should be searchable using a keyed term,

`false` otherwise.

@param unkeyed [Boolean] ‘true` when the attribute should be searchable using an unkeyed term,

`false` otherwise.

@param block [Proc] A block which returns the Arel node to query against. When left blank, the

matching attribute from `.arel_table` is queried instead.
# File lib/pursuit/search_options.rb, line 80
def attribute(term_name, attribute_name = nil, keyed: true, unkeyed: true, &block)
  block ||= -> { record_class.arel_table[attribute_name || term_name] }
  attributes[term_name] = AttributeOptions.new(keyed, unkeyed, block)
  nil
end
keyed_attributes() click to toggle source

@return [Hash<Symbol, Proc>] The attributes which can be queried using a keyed term.

# File lib/pursuit/search_options.rb, line 36
def keyed_attributes
  attributes.each_with_object({}) do |(name, options), keyed_attributes|
    keyed_attributes[name] = options.block if options.keyed
  end
end
keys() click to toggle source

@return [Array<String>] The collection of all possible attributes which can be used as a keyed term.

# File lib/pursuit/search_options.rb, line 52
def keys
  keys = relations.keys + attributes.select { |_, a| a.keyed }.keys
  keys.map(&:to_s).uniq
end
relation(name, *attribute_names) click to toggle source

Add a relation to search.

@param name [Symbol] The name of the relationship attribute. @param attribute_names [Splat] The name of the attributes within the relationship to search.

# File lib/pursuit/search_options.rb, line 62
def relation(name, *attribute_names)
  relations[name] = attribute_names
  nil
end
unkeyed_attributes() click to toggle source

@return [Hash<Symbol, Proc>] The attributes which can be queried using an unkeyed term.

# File lib/pursuit/search_options.rb, line 44
def unkeyed_attributes
  attributes.each_with_object({}) do |(name, options), unkeyed_attributes|
    unkeyed_attributes[name] = options.block if options.unkeyed
  end
end