Class: SearchPresenter
Overview
Presenter: search presenter.
Instance Attribute Summary
#current_account, #options, #url_helper
Instance Method Summary
collapse
#can?, #capture, #concat, #content_tag, #fa_icon, #h, #initialize, #link_to, #number_to_currency, #present, presents, #r, #safe_present, #simple_format, #u
Constructor Details
This class inherits a constructor from BasePresenter
Instance Method Details
#base_search_class_name ⇒ Object
141
142
143
|
# File 'app/presenters/search_presenter.rb', line 141
def base_search_class_name
search.class.base_search_class_name
end
|
#breadcrumbs ⇒ Object
39
40
41
42
43
44
|
# File 'app/presenters/search_presenter.rb', line 39
def breadcrumbs
h.capture do
h.concat h.link_to('Advanced Searches', h.search_types_searches_path, class: 'breadcrumb-item')
h.concat h.link_to(search.type.titleize.pluralize, h.searches_path(type: search.type), class: 'breadcrumb-item')
end
end
|
Generate a cell containing the select all option.
check-all cascades the checked state into the rows matching
data-selector. The wrapping _search_results.html.erb div wires a
Stimulus outlet from check-all to checkbox-persist, so after the
cascade the diff is handed off and POSTed to SearchesController#record
in one batched request.
73
74
75
76
77
78
79
80
81
|
# File 'app/presenters/search_presenter.rb', line 73
def
h.content_tag :th,
h.check_box_tag(:check_all_results, 1,
results.any?(&:search_selected),
data: {
selector: '.search-result-row',
action: 'change->check-all#toggle'
})
end
|
#mass_actions_for_select ⇒ Object
52
53
54
|
# File 'app/presenters/search_presenter.rb', line 52
def mass_actions_for_select
@current_account.has_role?(%w[admin sales_director sales_manager]) ? search.class.mass_actions_for_select : search.class.mass_actions_for_select.delete_if { |ma| ma[0] == 'Mass delete on lead qualify and guest state' }
end
|
46
47
48
49
50
|
# File 'app/presenters/search_presenter.rb', line 46
def new_search_button
return unless h.can?(:create, search)
h.link_to(h.fa_icon('plus-circle', text: "New #{search.search_name}"), h.new_search_path(type: search.class.search_type), class: 'btn btn-outline-primary')
end
|
#present_result(result) {|presenter| ... } ⇒ Object
145
146
147
148
149
150
|
# File 'app/presenters/search_presenter.rb', line 145
def present_result(result)
klass = "Search::#{base_search_class_name}Presenter".constantize
presenter = klass.new(result, h, search: @model, current_account: current_account)
yield presenter if block_given?
presenter
end
|
#query_debug_info ⇒ Object
56
57
58
59
60
61
62
63
64
|
# File 'app/presenters/search_presenter.rb', line 56
def query_debug_info
return unless h.user_has_role? 'admin'
h.panel 'Query Debug Info', false, panel_class: 'mt-3' do
h.content_tag :code do
search.perform.to_sql
end
end
end
|
#results ⇒ Object
7
8
9
10
11
12
13
14
15
16
|
# File 'app/presenters/search_presenter.rb', line 7
def results
return @results if defined?(@results)
controller_results = h.instance_variable_get(:@results)
@results = if controller_results.nil? && !h.instance_variable_defined?(:@results)
search.perform(page, sort, direction)
else
controller_results
end
end
|
#search_outcome ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# File 'app/presenters/search_presenter.rb', line 18
def search_outcome
if pagy
pagy.info_tag(item_name: search.class.main_resource_class.downcase).html_safe
elsif results.present?
item_name = search.class.main_resource_class.downcase.pluralize
"Found #{search.result_set_size} #{item_name}"
else
'No results found'
end
end
|
#table_body ⇒ Object
95
96
97
98
99
100
101
|
# File 'app/presenters/search_presenter.rb', line 95
def table_body
h.content_tag :tbody do
results.each do |result_row|
h.concat table_row(result_row)
end
end
end
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'app/presenters/search_presenter.rb', line 124
def
= begin
"Search::#{base_search_class_name}SummaryPresenter".constantize
rescue StandardError
nil
end
return unless
= .new(results, h)
h.content_tag :tfoot do
h.concat h.content_tag(:td, 'Totals')
search.validated_selected_columns.each do |column_name|
h.concat h.content_tag(:td, .present(column_name), nowrap: true)
end
end
end
|
Generate a table header containing all result columns with clickable sortable link
84
85
86
87
88
89
90
91
92
93
|
# File 'app/presenters/search_presenter.rb', line 84
def
h.content_tag :thead do
h.content_tag :tr do
h.concat
search.validated_selected_columns.each do |column_name|
h.concat h.adv_sortable(column_name, search.class.friendly_column_name(column_name), remote_links: options[:remote_links])
end
end
end
end
|
#table_row(result_row) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'app/presenters/search_presenter.rb', line 103
def table_row(result_row)
row_presenter = present_result(result_row)
h.content_tag :tr do
h.concat h.result_search_selector(result_row)
search.validated_selected_columns.each do |column_name|
val = row_presenter.send(column_name)
if column_name == :future_release_date
add_icon = ''
if val && ((val - Date.current).round <= 0)
add_icon = h.fa_icon('exclamation-triangle')
elsif val && ((val - Date.current).round <= 2)
add_icon = h.fa_icon('clock')
end
h.concat h.content_tag(:td, "#{val} #{add_icon}".html_safe)
else
h.concat h.content_tag(:td, val.to_s.html_safe)
end
end
end
end
|
#total_count ⇒ Object
30
31
32
33
|
# File 'app/presenters/search_presenter.rb', line 30
def total_count
pagy&.count || search.result_set_size
end
|