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.

Parameters:

  • klass (Class)

    Search subclass this template wraps

  • options (Hash)

Options Hash (options):

  • query_params (Hash)

    query params handed to the
    search

  • selected_columns (Array)

    columns handed to the
    search

  • sort_columns (Array)

    sort columns handed to the
    search

  • sort_direction (String)

    sort direction handed to
    the search

  • set_limit (Integer)

    row limit handed to the search

  • pinned (Boolean) — default: false

    pinned flag handed to the
    search

  • auto_count (Boolean) — default: true

    whether #show_count?
    is on by default



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/query_template.rb', line 24

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)


55
56
57
# File 'app/models/query_template.rb', line 55

def auto_pin?
  @options[:auto_pin]
end

#query_paramsHash

Returns:

  • (Hash)


65
66
67
# File 'app/models/query_template.rb', line 65

def query_params
  @options[:query_params]
end

#result_setArray

Returns:

  • (Array)


84
85
86
# File 'app/models/query_template.rb', line 84

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

#result_set_lengthInteger?

Returns:

  • (Integer, nil)


75
76
77
78
79
80
81
# File 'app/models/query_template.rb', line 75

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

#selected_columnsArray

Returns:

  • (Array)


70
71
72
# File 'app/models/query_template.rb', line 70

def selected_columns
  @options[:selected_columns]
end

#show_count?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/query_template.rb', line 60

def show_count?
  @options[:auto_count]
end

#titleString?

Returns:

  • (String, nil)


50
51
52
# File 'app/models/query_template.rb', line 50

def title
  @options[:title]
end

#to_params(options = {}) ⇒ Hash

Returns flattened, blank-compacted params.

Parameters:

  • options (Hash) (defaults to: {})

    extra params deep-merged into the result

Options Hash (options):

  • :type (String)

    override the search class name

  • :search (Hash)

    override/extend the search attributes

Returns:

  • (Hash)

    flattened, blank-compacted params



40
41
42
43
44
45
46
47
# File 'app/models/query_template.rb', line 40

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