Class: Shiboru::FilterSet
- Inherits:
-
Object
- Object
- Shiboru::FilterSet
- Defined in:
- lib/shiboru/filter_set.rb
Constant Summary collapse
Class Attribute Summary collapse
-
._custom_filters ⇒ Object
readonly
Returns the value of attribute _custom_filters.
-
._fields ⇒ Object
readonly
Returns the value of attribute _fields.
-
._orderable ⇒ Object
readonly
Returns the value of attribute _orderable.
-
._related_fields ⇒ Object
readonly
Returns the value of attribute _related_fields.
Class Method Summary collapse
- .custom_filters ⇒ Object
-
.fields(*names) ⇒ Object
— Sweet DSL —.
- .fields_set ⇒ Object
- .filter(name, &blk) ⇒ Object
-
.model ⇒ Object
Infer model from Filter class: Profiles::UserFilter -> Profiles::User.
- .orderable_fields(*names) ⇒ Object
- .orderable_set ⇒ Object
- .related_fields(*names) ⇒ Object
- .related_set ⇒ Object
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(scope, params) ⇒ FilterSet
constructor
A new instance of FilterSet.
Constructor Details
#initialize(scope, params) ⇒ FilterSet
Returns a new instance of FilterSet.
46 47 48 49 |
# File 'lib/shiboru/filter_set.rb', line 46 def initialize(scope, params) @scope = scope @params = Shiboru::ParamParser.new(params) end |
Class Attribute Details
._custom_filters ⇒ Object (readonly)
Returns the value of attribute _custom_filters.
6 7 8 |
# File 'lib/shiboru/filter_set.rb', line 6 def _custom_filters @_custom_filters end |
._fields ⇒ Object (readonly)
Returns the value of attribute _fields.
6 7 8 |
# File 'lib/shiboru/filter_set.rb', line 6 def _fields @_fields end |
._orderable ⇒ Object (readonly)
Returns the value of attribute _orderable.
6 7 8 |
# File 'lib/shiboru/filter_set.rb', line 6 def _orderable @_orderable end |
._related_fields ⇒ Object (readonly)
Returns the value of attribute _related_fields.
6 7 8 |
# File 'lib/shiboru/filter_set.rb', line 6 def @_related_fields end |
Class Method Details
.custom_filters ⇒ Object
27 |
# File 'lib/shiboru/filter_set.rb', line 27 def custom_filters = @_custom_filters || {} |
.fields(*names) ⇒ Object
— Sweet DSL —
9 |
# File 'lib/shiboru/filter_set.rb', line 9 def fields(*names) = (@_fields ||= []).concat(names.flatten.map(&:to_s)) |
.fields_set ⇒ Object
24 |
# File 'lib/shiboru/filter_set.rb', line 24 def fields_set = @_fields&.to_set || model.column_names.to_set |
.filter(name, &blk) ⇒ Object
12 |
# File 'lib/shiboru/filter_set.rb', line 12 def filter(name, &blk) = (@_custom_filters ||= {}).store(name.to_s, blk) |
.model ⇒ Object
Infer model from Filter class: Profiles::UserFilter -> Profiles::User
15 16 17 18 19 20 21 22 |
# File 'lib/shiboru/filter_set.rb', line 15 def model @__inferred_model ||= begin base = name.sub(/Filter\z/, "") raise ArgumentError, "Filter class name must end with 'Filter' (got #{name})" if base == name constantize_chain(base) end end |
.orderable_fields(*names) ⇒ Object
11 |
# File 'lib/shiboru/filter_set.rb', line 11 def orderable_fields(*names)= (@_orderable ||= []).concat(names.flatten.map(&:to_s)) |
.orderable_set ⇒ Object
26 |
# File 'lib/shiboru/filter_set.rb', line 26 def orderable_set = @_orderable&.to_set || fields_set |
.related_fields(*names) ⇒ Object
10 |
# File 'lib/shiboru/filter_set.rb', line 10 def (*names) = (@_related_fields ||= []).concat(names.flatten.map(&:to_s)) |
.related_set ⇒ Object
25 |
# File 'lib/shiboru/filter_set.rb', line 25 def = @_related_fields&.to_set || Set.new |
Instance Method Details
#call ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/shiboru/filter_set.rb', line 51 def call rel = apply_filters(@scope) rel = apply_ordering(rel) rel, = apply_pagination(rel) { "count" => [:count], "next" => [:next], "previous" => [:previous], "results" => rel } end |