Module: SearchesHelper
- Included in:
- ExportedCatalogItemPacket
- Defined in:
- app/helpers/searches_helper.rb
Instance Method Summary collapse
- #adv_sortable(column, title = nil, options = {}) ⇒ Object
- #cross_reference_customer_search_collection ⇒ Object
- #current_user_recent_searches(user = current_user) ⇒ Object
- #field_wrap(form, name, display = nil) ⇒ Object
- #get_num_records(search_template, options) ⇒ Object
- #hash_to_form_params(hash) ⇒ Object
- #header_search_selector ⇒ Object
- #line_items_search_fields(f) ⇒ Object
- #permitted_search_types ⇒ Object
- #permitted_search_types_classes ⇒ Object
- #query_template_link(search_klass, name_key = nil, options = {}) ⇒ Object
- #result_search_selector(r) ⇒ Object
- #rma_items_search_fields(f) ⇒ Object
- #sales_rep_fields(f, fields: nil) ⇒ Object
- #search_breadcrumb(search) ⇒ Object
- #search_range_text_field(form, name, display = nil, options = {}) ⇒ Object
- #search_select_field(form, name, display = nil, list = [%w[Yes y], %w[No n]], options = {}) ⇒ Object
- #search_text_field(form, name, display = nil, options = {}) ⇒ Object
- #simple_search_link(search_klass, query_params: {}, selected_columns: nil, num_records: nil, format: nil, title: nil, link_class: nil, auto_count: false) ⇒ Object
- #yes_no_boolean_field(form, name, display) ⇒ Object
Instance Method Details
#adv_sortable(column, title = nil, options = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/helpers/searches_helper.rb', line 126 def adv_sortable(column, title = nil, = {}) title ||= column.to_s.titleize title = title.html_safe direction = 'ASC' # Some of our columns are composite column constituded of other columns, we retrieve them to compare if we should highlight the table header composite_order_columns = @search.all_composite_columns[column.to_sym].try(:[], :select) || [] # Is our table header column present in the current sort columns of the query or is it present and part of a composite set. if sort_direction = @search.sort_columns_to_hash[column.to_sym] || (@search.sort_columns_to_hash.keys & composite_order_columns).present? index = @search.sort_columns_to_hash.keys.index(column.to_sym) title = (content_tag(:sup, " [#{index + 1}] ") + title) if index if %i[desc DESC].include?(sort_direction) title = fa_icon('chevron-down', text: title) else # :asc title = fa_icon('chevron-up', text: title) direction = 'DESC' end style = 'font-weight:bolder' end parameters = request.params.reject { |_k, v| v.blank? } parameters = parameters.symbolize_keys if parameters.present? parameters[:sort] = column parameters[:direction] = direction content_tag :th, link_to(title, parameters), style: style end |
#cross_reference_customer_search_collection ⇒ Object
189 190 191 192 193 |
# File 'app/helpers/searches_helper.rb', line 189 def cross_reference_customer_search_collection CustomerSearch.where(employee_id: @context_user.id) .select('(select count(*) from search_results sr where sr.search_id = searches.id) as selected_results, searches.*') .where('(select count(*) from search_results sr where sr.search_id = searches.id) > 0').map { |s| ["#{s.effective_name} (#{s.selected_results} selected)", s.id] } end |
#current_user_recent_searches(user = current_user) ⇒ Object
4 5 6 |
# File 'app/helpers/searches_helper.rb', line 4 def current_user_recent_searches(user = current_user) user.searches.where(persist: false).order(:updated_at).reverse_order.limit(15) end |
#field_wrap(form, name, display = nil) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'app/helpers/searches_helper.rb', line 151 def field_wrap(form, name, display = nil) display ||= name.to_s.titleize content_tag :div, class: 'field' do form.label(name, "#{display}:") + yield end end |
#get_num_records(search_template, options) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'app/helpers/searches_helper.rb', line 35 def get_num_records(search_template, ) case [:aggregate_method] when :sum num_records = search_template.result_set.sum([:aggregate_column]) if [:aggregate_column].present? when :count num_records = [:num_records] || search_template.result_set_length end num_records end |
#hash_to_form_params(hash) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/helpers/searches_helper.rb', line 104 def hash_to_form_params(hash) cleaned_hash = hash.reject { |_k, v| v.blank? } pairs = cleaned_hash.to_query.split(Rack::Utils::DEFAULT_SEP) flattened_hash_params = {} # This allows duplicate keys which is what we want for array input tags flattened_hash_params.compare_by_identity pairs.map do |pair| key, value = pair.split('=', 2).map { |str| Rack::Utils.unescape(str) } next unless value.present? flattened_hash_params[key] = value end flattened_hash_params end |
#header_search_selector ⇒ Object
118 119 120 |
# File 'app/helpers/searches_helper.rb', line 118 def header_search_selector content_tag :th, check_box_tag(:check_all_results, '1', @results.any? { |r| r.search_selected }, { 'data-href' => record_search_path(@search), :class => 'check_all_results' }) end |
#line_items_search_fields(f) ⇒ Object
164 165 166 |
# File 'app/helpers/searches_helper.rb', line 164 def line_items_search_fields(f) render partial: '/searches/line_items_criteria', locals: { f: f } end |
#permitted_search_types ⇒ Object
14 15 16 |
# File 'app/helpers/searches_helper.rb', line 14 def permitted_search_types Search..select { |so| can?(:read, so[1].constantize.new) } end |
#permitted_search_types_classes ⇒ Object
18 19 20 |
# File 'app/helpers/searches_helper.rb', line 18 def permitted_search_types_classes Search..select { |search_klass| can?(:read, search_klass.new) } end |
#query_template_link(search_klass, name_key = nil, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/searches_helper.rb', line 45 def query_template_link(search_klass, name_key = nil, = {}) [:aggregate_method] ||= :count qt = search_klass.instantiate_query_template(name_key, ) link_names = [] if qt.show_count? || [:num_records].present? num_records = get_num_records qt, if num_records.to_i.positive? || [:show_zero] record_badge = number_to_currency(num_records, unit: [:currency_symbol] || '$', precision: .fetch(:precision, 2)) if [:format] == :currency record_badge ||= num_records.to_i link_names << content_tag(:span, qt&.title&.html_safe) if qt.title.present? if [:counter_class].present? counter_class = [:counter_class] elsif link_names.present? # If we have one title entry, then we display badge style counter_class = 'ms-2 badge bg-primary' end if [:zero_label] && num_records.to_f.zero? record_badge = [:zero_label] counter_class = [counter_class, 'text-secondary'].compact.join(' ') elsif [:mute_zero] && num_records.to_f.zero? counter_class = [counter_class, 'text-secondary'].compact.join(' ') end link_names << content_tag(:span, record_badge, class: counter_class) if record_badge.present? elsif qt.title link_names << content_tag(:span, qt.title) end elsif qt.title link_names << content_tag(:span, qt.title) end # Test if we are passing a class, then that overrides everything, we test for nil since a blank class # might mean we want no default behavior css_classes = [] if [:class].present? css_classes += [:class].to_s.split elsif [:in_menu].to_b # Apply in menu styling css_classes += %w[dropdown-item justify-content-between align-items-center d-flex] else css_classes = %w[btn btn-link p-0 m-0] end css_classes << 'btn' if css_classes.detect { |cc| cc&.starts_with?('btn-') } && !css_classes.include?('btn') if [:link_only] link_element = link_to(num_records, execute_search_url(qt.to_params)) else css_classes = css_classes.uniq.map(&:presence).compact = { method: :post, class: css_classes.join(' '), style: [:style], params: hash_to_form_params(qt.to_params)}.compact link_element = (searches_path, **) { link_names.compact.join.html_safe } end [ link_element, num_records ] end |
#result_search_selector(r) ⇒ Object
122 123 124 |
# File 'app/helpers/searches_helper.rb', line 122 def result_search_selector(r) content_tag :td, check_box_tag(:resource_id, "#{r.class.main_resource_class}|#{r.id}", r.search_selected, { 'data-href' => record_search_path(@search), 'data-search-id' => @search.id, :class => 'resource_tagging' }) end |
#rma_items_search_fields(f) ⇒ Object
168 169 170 |
# File 'app/helpers/searches_helper.rb', line 168 def rma_items_search_fields(f) render partial: '/searches/rma_items_criteria', locals: { f: f } end |
#sales_rep_fields(f, fields: nil) ⇒ Object
160 161 162 |
# File 'app/helpers/searches_helper.rb', line 160 def sales_rep_fields(f, fields: nil) render partial: '/searches/sales_rep_fields', locals: { f: f, fields: fields } end |
#search_breadcrumb(search) ⇒ Object
8 9 10 11 12 |
# File 'app/helpers/searches_helper.rb', line 8 def (search) content_tag(:li, link_to(search.type.to_s.titleize.pluralize, employee_searches_path(@context_user, type: search.type.to_s))) + content_tag(:li, link_to("#{search.type.to_s.titleize} #{search.id}", search_path(search))) end |
#search_range_text_field(form, name, display = nil, options = {}) ⇒ Object
199 200 201 |
# File 'app/helpers/searches_helper.rb', line 199 def search_range_text_field(form, name, display = nil, = {}) form.input name.to_sym, label: display || name, as: [:class] == 'datepicker' ? :between_or_equal_date : :between_or_equal end |
#search_select_field(form, name, display = nil, list = [%w[Yes y], %w[No n]], options = {}) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/helpers/searches_helper.rb', line 172 def search_select_field(form, name, display = nil, list = [%w[Yes y], %w[No n]], = {}) [:multiple] = true if [:multiple].nil? [:include_blank] = true if [:include_blank].nil? form.input name, label: display, as: :tom_select, collection: list, selected: @search.query_params[name], include_blank: [:include_blank], input_html: { multiple: [:multiple], style: [:style] }, required: false, hint: [:hint] end |
#search_text_field(form, name, display = nil, options = {}) ⇒ Object
195 196 197 |
# File 'app/helpers/searches_helper.rb', line 195 def search_text_field(form, name, display = nil, = {}) form.input name, label: display, input_html: { value: @search.query_params[name], class: [:class], style: [:style] }, required: false, hint: [:hint] end |
#simple_search_link(search_klass, query_params: {}, selected_columns: nil, num_records: nil, format: nil, title: nil, link_class: nil, auto_count: false) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/searches_helper.rb', line 22 def simple_search_link(search_klass, query_params: {}, selected_columns: nil, num_records: nil, format: nil, title: nil, link_class: nil, auto_count: false) query_template_link(search_klass, nil, { query_params: query_params, selected_columns: selected_columns, num_records: num_records, format: format, title: title, class: link_class, auto_count: auto_count }.compact)&.first end |
#yes_no_boolean_field(form, name, display) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'app/helpers/searches_helper.rb', line 203 def yes_no_boolean_field(form, name, display) html_opts = { multiple: false, style: 'width:150px' } form.input name, label: display, as: :select, selected: @search.query_params[name], include_blank: true, collection: [%w[Yes true], %w[No false]], input_html: html_opts, required: false end |