Class: QueryTemplate

Inherits:
Object
  • Object
show all
Defined in:
app/models/query_template.rb

Overview

Pre-canned saved-query template — a named bundle of query params,
selected columns, sort, and limit — used by the CRM's "Saved
Queries" surface to spin up consistent reports without rebuilding
the search form each time. Wraps an arbitrary Search subclass.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options) ⇒ QueryTemplate

Returns a new instance of QueryTemplate.



9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/query_template.rb', line 9

def initialize(klass, options)
  @search_class = klass
  @options = options.dup || {}
  @options[:auto_count] = true if @options[:auto_count].nil?
  @search = klass.new(query_params: @options[:query_params],
                      selected_columns: @options[:selected_columns],
                      sort_columns: @options[:sort_columns].presence || [],
                      sort_direction: @options[:sort_direction],
                      set_limit: @options[:set_limit],
                      pinned: @options[:pinned] || false)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/models/query_template.rb', line 7

def options
  @options
end

#searchObject (readonly)

Returns the value of attribute search.



7
8
9
# File 'app/models/query_template.rb', line 7

def search
  @search
end

Instance Method Details

#auto_pin?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/query_template.rb', line 34

def auto_pin?
  @options[:auto_pin]
end

#query_paramsObject



42
43
44
# File 'app/models/query_template.rb', line 42

def query_params
  @options[:query_params]
end

#result_setObject



58
59
60
# File 'app/models/query_template.rb', line 58

def result_set
  @search.perform(nil, nil, nil, nil, nil, false, true)
end

#result_set_lengthObject



50
51
52
53
54
55
56
# File 'app/models/query_template.rb', line 50

def result_set_length
  begin
    result_set.size
  rescue StandardError
    nil
  end || result_set.size
end

#selected_columnsObject



46
47
48
# File 'app/models/query_template.rb', line 46

def selected_columns
  @options[:selected_columns]
end

#show_count?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/query_template.rb', line 38

def show_count?
  @options[:auto_count]
end

#titleObject



30
31
32
# File 'app/models/query_template.rb', line 30

def title
  @options[:title]
end

#to_params(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/query_template.rb', line 21

def to_params(options = {})
  hsh = {
    type: @search_class.name,
    search: @search.attributes
  }
  hsh.deep_merge(options)
  Hash[*hsh.compact_blank.flatten] # Only non nil
end