Class: FilterParam::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_param/definition.rb

Overview

FilterParam definition that whitelists the columns that are allowed to filtered (i.e. used in SQL WHERE condition) and the allowed scopes.

Instance Method Summary collapse

Instance Method Details

#define(&block) ⇒ self

Allows whitelisting columns using a block

Parameters:

  • block (Proc)

    Field definition block

Returns:

  • (self)

    Definition instance

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/filter_param/definition.rb', line 10

def define(&block)
  raise ArgumentError.new("Missing block") unless block_given?

  instance_eval(&block)

  self
end

#field(name, **options) ⇒ self

Whitelist a column

Parameters:

  • name (String, Symbol)

    column name

  • options (Hash)

    column options:

    • type [Symbol] expected field type: :string (default), :int, :decimal :boolean, :date, :datetime

    • rename [String, Proc] rename field in the formatted output. This can be a Proc code block that receives the :name as argument and returns a transformed field name.

    • value [Proc] pre-process literal operand values. This receives the :value argument parsed from the expression string and returns a transformed field value.

Returns:

  • (self)

    Definition instance



31
32
33
34
35
36
37
38
# File 'lib/filter_param/definition.rb', line 31

def field(name, **options)
  name = name.to_s
  return if name.blank?

  fields_hash[name] = Field.new(name, options[:type], options)

  self
end

#field_namesArray<String>

Returns the declared Field names

Returns:

  • (Array<String>)


130
131
132
# File 'lib/filter_param/definition.rb', line 130

def field_names
  fields_hash.keys
end

#fields(*names, **options) ⇒ self

Whitelist multiple columns with the same column options.

Parameters:

  • names (Array<String, Symbol>)

    list of column names

  • options (Hash)

    column configuration options

Returns:

  • (self)

    Definition instance

See Also:



49
50
51
52
53
54
55
# File 'lib/filter_param/definition.rb', line 49

def fields(*names, **options)
  restrict_string_rename!(options[:rename])

  names.each { |name| field(name, **options) }

  self
end

#filter!(ar_relation, expression) ⇒ Object

Filters an :ar_relation by the filter :expression

Parameters:

  • ar_relation (ActiveRecord::Relation)

    Relation to filter

  • expression (String)

    Filter expression.



95
96
97
98
99
100
101
# File 'lib/filter_param/definition.rb', line 95

def filter!(ar_relation, expression)
  transpiler = Transpiler.new(ar_relation, self)

  ar_relation.where(
    transpiler.transpile!(expression)
  )
end

#find_field!(field_name) ⇒ Field

Returns the declared Field instance

Parameters:

  • field_name (String, Symbol)

Returns:

Raises:



108
109
110
111
112
113
# File 'lib/filter_param/definition.rb', line 108

def find_field!(field_name)
  field = fields_hash[field_name.to_s].presence
  return field if field

  raise UnknownField.new("Unknown field: '#{field_name}'")
end

#find_scope!(scope_name) ⇒ Field

Returns the declared Scope instance

Parameters:

  • scope_name (String, Symbol)

Returns:

Raises:



120
121
122
123
124
125
# File 'lib/filter_param/definition.rb', line 120

def find_scope!(scope_name)
  scope = scopes_hash[scope_name.to_s].presence
  return scope if scope

  raise UnknownScope.new("Unknown scope: '#{scope_name}'")
end

#scope(name, **options) ⇒ Object

Whitelist a scope name

Parameters:

  • name (String, Symbol)

    scope name. ar_relation passed to ‘#filter!` must expose this scope.

  • options (Hash)

    column options:

    • rename [String, Proc] rename to actual scope name. This can be a Proc code block that receives the :name as argument and returns a transformed scope name.



64
65
66
67
68
69
70
71
# File 'lib/filter_param/definition.rb', line 64

def scope(name, **options)
  name = name.to_s
  return if name.blank?

  scopes_hash[name] = Scope.new(name, options)

  self
end

#scope_namesArray<String>

Returns the declared Scope names

Returns:

  • (Array<String>)


137
138
139
# File 'lib/filter_param/definition.rb', line 137

def scope_names
  scopes_hash.keys
end

#scopes(*names, **options) ⇒ self

Whitelist multiple scope names with the same scope options.

Parameters:

  • names (Array<String, Symbol>)

    list of scope names

  • options (Hash)

    scope configuration options

Returns:

  • (self)

    Definition instance

See Also:



82
83
84
85
86
87
88
# File 'lib/filter_param/definition.rb', line 82

def scopes(*names, **options)
  restrict_string_rename!(options[:rename])

  names.each { |name| scope(name, **options) }

  self
end