class Parelation::Criteria::Query

Constants

QUERY_FORMAT

@return [Regexp] the query format.

Public Class Methods

match?(param) click to toggle source

@param param [String] @return [TrueClass, FalseClass]

# File lib/parelation/criteria/query.rb, line 10
def self.match?(param)
  !!param.match(QUERY_FORMAT)
end

Public Instance Methods

call() click to toggle source

@return [ActiveRecord::Relation]

# File lib/parelation/criteria/query.rb, line 16
def call
  chain.where(*criteria)
end

Private Instance Methods

args() click to toggle source

@return [Array] containing the query, one for each field in {#attributes}.

# File lib/parelation/criteria/query.rb, line 37
def args
  attributes.count.times.map { "%#{query}%" }
end
attributes() click to toggle source

@return [Array<String>] an array of attributes to search in.

# File lib/parelation/criteria/query.rb, line 49
def attributes
  @attributes ||= parts.last
end
criteria() click to toggle source

@return [Array] containing {#chain} criteria.

# File lib/parelation/criteria/query.rb, line 24
def criteria
  [sql] + args
end
parts() click to toggle source

@return [Array] containing the query and attributes.

# File lib/parelation/criteria/query.rb, line 55
def parts
  @parts ||= [value.keys.first, [value.values.first].flatten]
end
query() click to toggle source

@return [String] the “search” query to perform.

# File lib/parelation/criteria/query.rb, line 43
def query
  @query ||= parts.first
end
sql() click to toggle source

@return [String] an SQL statement based on the selected {#attributes}.

# File lib/parelation/criteria/query.rb, line 30
def sql
  template = ->(field) { %Q{"#{chain.arel_table.name}"."#{field}" LIKE ?} }
  attributes.map(&template).join(" OR ")
end