Class: SearchPresenter
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
134
135
136
|
# File 'app/presenters/search_presenter.rb', line 134
def base_search_class_name
search.class.base_search_class_name
end
|
#breadcrumbs ⇒ Object
38
39
40
41
42
43
|
# File 'app/presenters/search_presenter.rb', line 38
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
66
67
68
69
70
71
72
73
74
|
# File 'app/presenters/search_presenter.rb', line 66
def
h.content_tag :th,
h.check_box_tag(:check_all_results, 1,
results.any?(&:search_selected),
{
data: { href: h.record_search_path(search), search_id: search.id },
class: 'check_all_results'
})
end
|
#mass_actions_for_select ⇒ Object
51
52
53
|
# File 'app/presenters/search_presenter.rb', line 51
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
|
45
46
47
48
49
|
# File 'app/presenters/search_presenter.rb', line 45
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
138
139
140
141
142
143
|
# File 'app/presenters/search_presenter.rb', line 138
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
55
56
57
58
59
60
61
62
63
|
# File 'app/presenters/search_presenter.rb', line 55
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
6
7
8
9
10
11
12
13
14
15
|
# File 'app/presenters/search_presenter.rb', line 6
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
17
18
19
20
21
22
23
24
25
26
27
|
# File 'app/presenters/search_presenter.rb', line 17
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
88
89
90
91
92
93
94
|
# File 'app/presenters/search_presenter.rb', line 88
def table_body
h.content_tag :tbody do
results.each do |result_row|
h.concat table_row(result_row)
end
end
end
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'app/presenters/search_presenter.rb', line 117
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
77
78
79
80
81
82
83
84
85
86
|
# File 'app/presenters/search_presenter.rb', line 77
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'app/presenters/search_presenter.rb', line 96
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}".html_safe)
end
end
end
end
|
#total_count ⇒ Object
29
30
31
32
|
# File 'app/presenters/search_presenter.rb', line 29
def total_count
pagy&.count || search.result_set_size
end
|