Class: Plutonium::Resource::QueryObject

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/resource/query_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_class, params, request_path) {|_self| ... } ⇒ QueryObject

Initializes a QueryObject with the given resource_class and parameters.

Parameters:

  • resource_class (Object)

    The resource class.

  • params (Hash)

    The parameters for initialization.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
18
19
20
# File 'lib/plutonium/resource/query_object.rb', line 11

def initialize(resource_class, params, request_path, &)
  @resource_class = resource_class
  @params = params
  @request_path = request_path

  define_standard_queries
  yield self if block_given?
  extract_filter_params
  extract_sort_params
end

Instance Attribute Details

#default_sort_configObject

Returns the value of attribute default_sort_config.



5
6
7
# File 'lib/plutonium/resource/query_object.rb', line 5

def default_sort_config
  @default_sort_config
end

#search_filterObject (readonly)

Returns the value of attribute search_filter.



4
5
6
# File 'lib/plutonium/resource/query_object.rb', line 4

def search_filter
  @search_filter
end

#search_queryObject (readonly)

Returns the value of attribute search_query.



4
5
6
# File 'lib/plutonium/resource/query_object.rb', line 4

def search_query
  @search_query
end

Instance Method Details

#apply(scope, params) ⇒ Object

Applies the defined filters and sorts to the given scope.

Parameters:

  • scope (Object)

    The initial scope to which filters and sorts are applied.

Returns:

  • (Object)

    The modified scope.



86
87
88
89
90
91
92
# File 'lib/plutonium/resource/query_object.rb', line 86

def apply(scope, params)
  params = deep_compact(params.with_indifferent_access)
  scope = search_filter.apply(scope, search: params[:search]) if search_filter && params[:search]
  scope = scope_definitions[params[:scope]].apply(scope, **{}) if scope_definitions[params[:scope]]
  scope = apply_sorts(scope, params)
  apply_filters(scope, params)
end

#build_url(**options) ⇒ String

Builds a URL with the given options for search and sorting.

Parameters:

  • options (Hash)

    The options for building the URL.

Returns:

  • (String)

    The constructed URL with query parameters.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/plutonium/resource/query_object.rb', line 67

def build_url(**options)
  q = {}

  q[:search] = options.key?(:search) ? options[:search].presence : search_query
  q[:scope] = options.key?(:scope) ? options[:scope].presence : selected_scope_filter

  q[:sort_directions] = selected_sort_directions.dup
  q[:sort_fields] = selected_sort_fields.dup
  handle_sort_options!(q, options)

  q.merge! params.slice(*filter_definitions.keys)
  query_params = deep_compact({q: q}).to_param
  "#{@request_path}?#{query_params}"
end

#define_filter(name, body) ⇒ Object

Defines a filter with the given name and body.

Parameters:

  • name (Symbol)

    The name of the filter.

  • body (Proc, nil)

    The body of the filter.



26
27
28
# File 'lib/plutonium/resource/query_object.rb', line 26

def define_filter(name, body, &)
  filter_definitions[name] = build_query(body, &)
end

#define_scope(name, body = nil) ⇒ Object

Defines a scope with the given name and body.

Parameters:

  • name (Symbol)

    The name of the scope.

  • body (Proc, nil) (defaults to: nil)

    The body of the scope.



34
35
36
37
# File 'lib/plutonium/resource/query_object.rb', line 34

def define_scope(name, body = nil)
  body ||= name
  scope_definitions[name] = build_query(body)
end

#define_search(body) ⇒ Object

Defines a search filter with the given body.

Parameters:

  • body (Proc, Symbol)

    The body of the search filter.



57
58
59
60
61
# File 'lib/plutonium/resource/query_object.rb', line 57

def define_search(body)
  @search_filter = build_query(body) do |query|
    query.input :search
  end
end

#define_sorter(name, body = nil, using: nil) ⇒ Object

Defines a sort with the given name and body.

Parameters:

  • name (Symbol)

    The name of the sort.

  • body (Proc, nil) (defaults to: nil)

    The body of the sort.



43
44
45
46
47
48
49
50
51
52
# File 'lib/plutonium/resource/query_object.rb', line 43

def define_sorter(name, body = nil, using: nil)
  if body.nil?
    sort_field = using || determine_sort_field(name)
    body = ->(scope, direction:) { scope.order(sort_field => direction) }
  end

  sort_definitions[name] = build_query(body) do |query|
    query.input :direction
  end
end

#filter_definitionsObject



96
# File 'lib/plutonium/resource/query_object.rb', line 96

def filter_definitions = @filter_definitions ||= {}.with_indifferent_access

#scope_definitionsObject



94
# File 'lib/plutonium/resource/query_object.rb', line 94

def scope_definitions = @scope_definitions ||= {}.with_indifferent_access

#sort_definitionsObject



98
# File 'lib/plutonium/resource/query_object.rb', line 98

def sort_definitions = @sort_definitions ||= {}.with_indifferent_access

#sort_params_for(name) ⇒ Hash?

Provides sorting parameters for the given field name.

Parameters:

  • name (Symbol, String)

    The name of the field to sort.

Returns:

  • (Hash, nil)

    The sorting parameters including URL and direction.



104
105
106
107
108
109
110
111
112
113
# File 'lib/plutonium/resource/query_object.rb', line 104

def sort_params_for(name)
  return unless sort_definitions[name]

  {
    url: build_url(sort: name),
    reset_url: build_url(sort: name, reset: true),
    position: selected_sort_fields.index(name.to_s),
    direction: selected_sort_directions[name]
  }
end