Class: JsonApiServer::FilterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_server/filter_config.rb

Overview

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' } }
 ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FilterConfig

Returns a new instance of FilterConfig.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Attribute Details

#attrObject (readonly)

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



17
18
19
# File 'lib/json_api_server/filter_config.rb', line 17

def attr
  @attr
end

#builderObject (readonly)

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



38
39
40
# File 'lib/json_api_server/filter_config.rb', line 38

def builder
  @builder
end

#column_nameObject (readonly)

(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’ } }



21
22
23
# File 'lib/json_api_server/filter_config.rb', line 21

def column_name
  @column_name
end

#comparisonObject (readonly)

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



33
34
35
# File 'lib/json_api_server/filter_config.rb', line 33

def comparison
  @comparison
end

#defaultObject (readonly)

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



36
37
38
# File 'lib/json_api_server/filter_config.rb', line 36

def default
  @default
end

#inObject (readonly)

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



30
31
32
# File 'lib/json_api_server/filter_config.rb', line 30

def in
  @in
end

#likeObject (readonly)

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



27
28
29
# File 'lib/json_api_server/filter_config.rb', line 27

def like
  @like
end

#methodObject (readonly)

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



40
41
42
# File 'lib/json_api_server/filter_config.rb', line 40

def method
  @method
end

#typeObject (readonly)

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



24
25
26
# File 'lib/json_api_server/filter_config.rb', line 24

def type
  @type
end

#wildcardObject (readonly)

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



43
44
45
# File 'lib/json_api_server/filter_config.rb', line 43

def wildcard
  @wildcard
end

Class Method Details

.default_typeObject

Default data type is String unless a filter config specifies.



67
68
69
# File 'lib/json_api_server/filter_config.rb', line 67

def self.default_type
  String
end