Class: QueryTemplate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options) ⇒ QueryTemplate

Returns a new instance of QueryTemplate.



4
5
6
7
8
9
10
11
12
13
14
# File 'app/models/query_template.rb', line 4

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.



2
3
4
# File 'app/models/query_template.rb', line 2

def options
  @options
end

#searchObject (readonly)

Returns the value of attribute search.



2
3
4
# File 'app/models/query_template.rb', line 2

def search
  @search
end

Instance Method Details

#auto_pin?Boolean

Returns:

  • (Boolean)


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

def auto_pin?
  @options[:auto_pin]
end

#query_paramsObject



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

def query_params
  @options[:query_params]
end

#result_setObject



53
54
55
# File 'app/models/query_template.rb', line 53

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

#result_set_lengthObject



45
46
47
48
49
50
51
# File 'app/models/query_template.rb', line 45

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

#selected_columnsObject



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

def selected_columns
  @options[:selected_columns]
end

#show_count?Boolean

Returns:

  • (Boolean)


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

def show_count?
  @options[:auto_count]
end

#titleObject



25
26
27
# File 'app/models/query_template.rb', line 25

def title
  @options[:title]
end

#to_params(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/models/query_template.rb', line 16

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