Class: Shiboru::FilterSet

Inherits:
Object
  • Object
show all
Defined in:
lib/shiboru/filter_set.rb

Constant Summary collapse

DEFAULT_OPERATORS =
Shiboru::Operators::DEFAULT

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_filtersObject (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

._fieldsObject (readonly)

Returns the value of attribute _fields.



6
7
8
# File 'lib/shiboru/filter_set.rb', line 6

def _fields
  @_fields
end

._orderableObject (readonly)

Returns the value of attribute _orderable.



6
7
8
# File 'lib/shiboru/filter_set.rb', line 6

def _orderable
  @_orderable
end

Returns the value of attribute _related_fields.



6
7
8
# File 'lib/shiboru/filter_set.rb', line 6

def _related_fields
  @_related_fields
end

Class Method Details

.custom_filtersObject



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_setObject



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)

.modelObject

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_setObject



26
# File 'lib/shiboru/filter_set.rb', line 26

def orderable_set = @_orderable&.to_set || fields_set


10
# File 'lib/shiboru/filter_set.rb', line 10

def related_fields(*names) = (@_related_fields ||= []).concat(names.flatten.map(&:to_s))


25
# File 'lib/shiboru/filter_set.rb', line 25

def related_set   = @_related_fields&.to_set || Set.new

Instance Method Details

#callObject



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, meta = apply_pagination(rel)

  {
    "count" => meta[:count],
    "next" => meta[:next],
    "previous" => meta[:previous],
    "results" => rel
  }
end