Class: RecordFilter

Inherits:
Object
  • Object
show all
Defined in:
app/services/record_filter.rb

Overview

Handles all of the logic for transforming a user's query parameters into a subset of records

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_params, pagination_params, base_scope, default_order: []) ⇒ RecordFilter

Returns a new instance of RecordFilter

Parameters:

  • filter_params (Hash)
  • pagination_params (Hash)
  • base_scope (ActiveRecord::Relation)

    the basis of all queries; typically this is based on who is logged-in, and whether the query is Coyote-wide (as in a UI index page) or organization-specific (as in an API call)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/record_filter.rb', line 11

def initialize(filter_params, pagination_params, base_scope, default_order: [])
  @filter_params = filter_params.to_hash.with_indifferent_access.tap do |params|
    params.each do |key, value|
      params[key] = value.to_s.split(" ") if key.to_s =~ /_cont_all$/
    end
  end
  @pagination_params = pagination_params
  @default_order = default_order

  filter_scope = @filter_params.delete(:scope)
  Array(filter_scope).each { |scope| @filter_params[scope] = true } if filter_scope.present?

  # TODO: Separate filtering and ordering properly.
  # This applies default ordering unless filter params are present - for now.
  default_order = Array(default_order)
  @base_scope = if default_order.any? && @filter_params.empty?
                  default_order.inject(base_scope) { |scope, filter| scope.send(filter) }
                else
                  base_scope
                end
end

Instance Attribute Details

#record_paginator=(value) ⇒ Object

Sets the attribute record_paginator

Parameters:

  • value

    the value to set the attribute record_paginator to.



4
5
6
# File 'app/services/record_filter.rb', line 4

def record_paginator=(value)
  @record_paginator = value
end

Instance Method Details

A set of links that should be rendered for browser users. The only difference between this and what an API user would see is that we suppress the first page link if the user is already viewing the first page

Returns:

  • (Hash)

    links that point to other available filtered pages



46
47
48
49
50
# File 'app/services/record_filter.rb', line 46

def browser_pagination_link_params
  pagination_link_params.tap do |p|
    p.delete(:first) if records.first_page?
  end
end

Returns links that point to other available filtered pages

Returns:

  • (Hash)

    links that point to other available filtered pages



53
54
55
56
57
# File 'app/services/record_filter.rb', line 53

def pagination_link_params
  base_link_params = {}
  base_link_params[:q] = filter_params.to_hash if filter_params.present?
  record_paginator.pagination_links_for(base_link_params)
end

#recordsActiveRecord::Relation

Returns the filtered collection of records, ready to be enumerated

Returns:

  • (ActiveRecord::Relation)

    the filtered collection of records, ready to be enumerated



39
40
41
# File 'app/services/record_filter.rb', line 39

def records
  @records ||= record_paginator.query
end

#searchRansack::Search

Returns for use with Ransack's simple_form_for form helper

Returns:

  • (Ransack::Search)

    for use with Ransack's simple_form_for form helper



34
35
36
# File 'app/services/record_filter.rb', line 34

def search
  @search ||= base_scope.search(filter_params)
end