Class: FilterParam::Definition
- Inherits:
-
Object
- Object
- FilterParam::Definition
- 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
-
#define(&block) ⇒ self
Allows whitelisting columns using a block.
-
#field(name, **options) ⇒ self
Whitelist a column.
-
#field_names ⇒ Array<String>
Returns the declared Field names.
-
#fields(*names, **options) ⇒ self
Whitelist multiple columns with the same column options.
-
#filter!(ar_relation, expression) ⇒ Object
Filters an :ar_relation by the filter :expression.
-
#find_field!(field_name) ⇒ Field
Returns the declared Field instance.
-
#find_scope!(scope_name) ⇒ Field
Returns the declared Scope instance.
-
#scope(name, **options) ⇒ Object
Whitelist a scope name.
-
#scope_names ⇒ Array<String>
Returns the declared Scope names.
-
#scopes(*names, **options) ⇒ self
Whitelist multiple scope names with the same scope options.
Instance Method Details
#define(&block) ⇒ self
Allows whitelisting columns using a block
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
31 32 33 34 35 36 37 38 |
# File 'lib/filter_param/definition.rb', line 31 def field(name, **) name = name.to_s return if name.blank? fields_hash[name] = Field.new(name, [:type], ) self end |
#field_names ⇒ Array<String>
Returns the declared Field names
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.
49 50 51 52 53 54 55 |
# File 'lib/filter_param/definition.rb', line 49 def fields(*names, **) restrict_string_rename!([:rename]) names.each { |name| field(name, **) } self end |
#filter!(ar_relation, expression) ⇒ Object
Filters an :ar_relation by the 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
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
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
64 65 66 67 68 69 70 71 |
# File 'lib/filter_param/definition.rb', line 64 def scope(name, **) name = name.to_s return if name.blank? scopes_hash[name] = Scope.new(name, ) self end |
#scope_names ⇒ Array<String>
Returns the declared Scope names
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.
82 83 84 85 86 87 88 |
# File 'lib/filter_param/definition.rb', line 82 def scopes(*names, **) restrict_string_rename!([:rename]) names.each { |name| scope(name, **) } self end |