Class: Warped::Queries::Filter
- Inherits:
-
Object
- Object
- Warped::Queries::Filter
- Defined in:
- lib/warped/queries/filter.rb
Overview
Filter a scope by a set of conditions
This class provides a simple interface for filtering a scope by a set of conditions. The conditions are passed as an array of hashes, where each hash contains the following keys:
-
relation
: the relation to use for the filter (e.g. “eq”, “gt”, “in”, etc.) -
field
: the field to filter by -
value
: the value to filter by
To see the list of available relations, check the RELATIONS
constant.
Constant Summary collapse
Class Method Summary collapse
-
.call(scope, filter_conditions:) ⇒ ActiveRecord::Relation
The filtered scope.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(scope, filter_conditions:) ⇒ ActiveRecord::Relation
constructor
The filtered scope.
Constructor Details
#initialize(scope, filter_conditions:) ⇒ ActiveRecord::Relation
Returns the filtered scope.
40 41 42 43 44 |
# File 'lib/warped/queries/filter.rb', line 40 def initialize(scope, filter_conditions:) super() @scope = scope @filter_conditions = filter_conditions end |
Class Method Details
.call(scope, filter_conditions:) ⇒ ActiveRecord::Relation
Returns the filtered scope.
33 34 35 |
# File 'lib/warped/queries/filter.rb', line 33 def self.call(scope, filter_conditions:) new(scope, filter_conditions:).call end |
Instance Method Details
#call ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/warped/queries/filter.rb', line 46 def call filter_conditions.reduce(scope) do |scope, filter_condition| relation = filter_condition[:relation].to_s field = filter_condition[:field] value = filter_condition[:value] validate_relation!(relation) filtered_scope(scope, relation, field, value) end end |