class JsonApiServer::FilterConfig

Configuration for a filter jsonapi.org/format/#fetching-filtering.

Example filter configuration:

filter_options = [
     { id: { type: 'Integer' } },
     { tags: { builder: :pg_jsonb_ilike_array } },
     :body,
     { title: { wildcard: :right }},
     { search: { builder: :model_query, method: :search } },
     { created: { col_name: :created_at, type: 'DateTime' } }
 ]

Attributes

attr[R]

Attribute used in queries. i.e., /path?filter=bar => foo

builder[R]

(optional) Symbol - the builder class to use for all queries for the attribute.

column_name[R]

(optional) If the fitler name is not the same as the database column name, map it. i.e., map :created to :created_at. { created: { col_name: :created_at, type: 'DateTime' } }

comparison[R]

(optional) Symbol - the builder class to use for '=', '<', '>', '>=', '<=', '=', '!<', '!>', '<>' queries. Defaults to :sql_comparison.

default[R]

(optional) Symbol - the builder class to use for all other queries. Defaults to :sql_eql.

in[R]

(optional) Symbol - the builder class to use for IN queries. Defaults to :sql_in if not specified.

like[R]

(optional) Symbol - the builder class to use for LIKE queries. Defaults to :sql_like if not specified.

method[R]

(optional) Use with ModelQuery builder which calls a class method on the model.

type[R]

(optional) Data type. Specify data type class as string. i.e., 'String', 'DateTime', 'Time', 'BigDecimal', etc. Defaults to 'String'.

wildcard[R]

(optional) Symbol - :left, :right or :none. Defaults to wildcarding beginning end of string, i.e., “%#{value}%”,

Public Class Methods

default_type() click to toggle source

Default data type is String unless a filter config specifies.

# File lib/json_api_server/filter_config.rb, line 67
def self.default_type
  String
end
new(config) click to toggle source
# File lib/json_api_server/filter_config.rb, line 45
def initialize(config)
  if config.respond_to?(:keys)
    # i.e, c.filter_options = { permitted: [{created: {attr: :created_at, type: DateTime}}] }
    key, value = config.first
    @attr = key
    @column_name = value[:col_name] || @attr
    @type = value[:type] || self.class.default_type
    @like = value[:like]
    @in = value[:in]
    @comparison = value[:comparison]
    @default = value[:default]
    @builder = value[:builder]
    @wildcard = value[:wildcard]
    @method = value[:method]
  else
    # i.e., c.filter_options = { permitted: [:body] }
    @attr = @column_name = config
    @type = self.class.default_type
  end
end