class JsonApiServer::FilterBuilder

Base filter/query builder class. All should inherit from this class.

Attributes

attr[R]

The filter attribute, i.e., filter

column_name[R]

Column name in the database. Specified when the filter name in the query doesn't match the column name in the database.

config[R]

Instance of FilterConfig for the specific attribute/column.

operator[R]

Can be IN, <, >, <=, etc.

value[R]

Casted value(s). If value included a common, an array of casted values.

Public Class Methods

new(attr, value, operator, config) click to toggle source
# File lib/json_api_server/filter_builders.rb, line 16
def initialize(attr, value, operator, config)
  @attr = attr
  @value = value
  @config = config
  @column_name = @config.column_name
  @operator = operator
end

Public Instance Methods

to_query(_model) click to toggle source

Subclasses must implement. Can return an ActiveRecord::Relation or nil.

# File lib/json_api_server/filter_builders.rb, line 26
def to_query(_model)
  raise 'subclasses should implement this method.'
end

Protected Instance Methods

add_wildcards(value) click to toggle source

For queries where wildcards are appropriate. Adds wildcards based on filter's configs.

# File lib/json_api_server/filter_builders.rb, line 43
def add_wildcards(value)
  return value unless value.present?

  case config.wildcard
  when :left
    return "%#{value}"
  when :right
    return "#{value}%"
  when :both
    return "%#{value}%"
  else # none by default
    return value
  end
end
full_column_name(model) click to toggle source
# File lib/json_api_server/filter_builders.rb, line 32
def full_column_name(model)
  "\"#{model.table_name}\".\"#{column_name}\""
end