Class: SearchPresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/search_presenter.rb

Instance Attribute Summary

Attributes inherited from BasePresenter

#current_account, #options, #url_helper

Instance Method Summary collapse

Methods inherited from BasePresenter

#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_nameObject



134
135
136
# File 'app/presenters/search_presenter.rb', line 134

def base_search_class_name
  search.class.base_search_class_name
end


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

#header_select_allObject

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 header_select_all
  h. :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_selectObject



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

#new_search_buttonObject



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

Yields:

  • (presenter)


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: )
  yield presenter if block_given?
  presenter
end

#query_debug_infoObject



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. :code do
      search.perform.to_sql
    end
  end
end

#resultsObject



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_outcomeObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/presenters/search_presenter.rb', line 17

def search_outcome
  # Use Pagy if available, otherwise fall back to manual display
  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_bodyObject



88
89
90
91
92
93
94
# File 'app/presenters/search_presenter.rb', line 88

def table_body
  h. :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 table_footer
  footer_presenter_klass = begin
    "Search::#{base_search_class_name}SummaryPresenter".constantize
  rescue StandardError
    nil
  end
  return unless footer_presenter_klass

  footer_presenter = footer_presenter_klass.new(results, h)
  h. :tfoot do
    h.concat h.(:td, 'Totals')
    search.validated_selected_columns.each do |column_name|
      h.concat h.(:td, footer_presenter.present(column_name), nowrap: true)
    end
  end
end

#table_headersObject

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 table_headers
  h. :thead do
    h. :tr do
      h.concat header_select_all
      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. :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.(:td, "#{val} #{add_icon}".html_safe)
      else
        h.concat h.(:td, "#{val}".html_safe)
      end
    end
  end
end

#total_countObject



29
30
31
32
# File 'app/presenters/search_presenter.rb', line 29

def total_count
  # Return total count from Pagy if available, otherwise from search result_set_size
  pagy&.count || search.result_set_size
end