Module: RangeLimitHelper
- Defined in:
- app/helpers/range_limit_helper.rb
Overview
Additional helper methods used by view templates inside this plugin.
Instance Method Summary collapse
- #add_range(solr_field, from, to, my_params = params) ⇒ Object
- #add_range_missing(solr_field, my_params = params) ⇒ Object
-
#format_range_display_value(value, _solr_field) ⇒ Object
A method that is meant to be overridden downstream to format how a range label might be displayed to a user.
- #has_selected_range_limit?(solr_field) ⇒ Boolean
- #range_display(solr_field, my_params = params) ⇒ Object
- #range_limit_panel_url(options = {}) ⇒ Object
- #range_limit_url(options = {}) ⇒ Object
-
#range_results_endpoint(solr_field, type) ⇒ Object
type is ‘min’ or ‘max’ Returns smallest and largest value in current result set, if available from stats component response.
-
#render_range_input(solr_field, type, input_label = nil, maxlength = 4) ⇒ Object
type is ‘begin’ or ‘end’.
- #selected_missing_for_range_limit?(solr_field) ⇒ Boolean
-
#should_show_limit(solr_field) ⇒ Object
Show the limit area if: 1) we have a limit already set OR 2) stats show max > min, OR 3) count > 0 if no stats available.
- #stats_for_field(solr_field) ⇒ Object
- #stats_for_field?(solr_field) ⇒ Boolean
Instance Method Details
#add_range(solr_field, from, to, my_params = params) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/helpers/range_limit_helper.rb', line 102 def add_range(solr_field, from, to, my_params = params) my_params = Blacklight::SearchState.new(my_params.except(:page), blacklight_config).to_h my_params["range"] ||= {} my_params["range"][solr_field] ||= {} my_params["range"][solr_field]["begin"] = from my_params["range"][solr_field]["end"] = to my_params["range"][solr_field].delete("missing") # eliminate temporary range status params that were just # for looking things up my_params.delete("range_field") my_params.delete("range_start") my_params.delete("range_end") return my_params end |
#add_range_missing(solr_field, my_params = params) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'app/helpers/range_limit_helper.rb', line 93 def add_range_missing(solr_field, my_params = params) my_params = Blacklight::SearchState.new(my_params.except(:page), blacklight_config).to_h my_params["range"] ||= {} my_params["range"][solr_field] ||= {} my_params["range"][solr_field]["missing"] = "true" my_params end |
#format_range_display_value(value, _solr_field) ⇒ Object
A method that is meant to be overridden downstream to format how a range label might be displayed to a user. By default it just returns the value.
67 68 69 |
# File 'app/helpers/range_limit_helper.rb', line 67 def format_range_display_value(value, _solr_field) value end |
#has_selected_range_limit?(solr_field) ⇒ Boolean
120 121 122 123 124 125 126 127 128 |
# File 'app/helpers/range_limit_helper.rb', line 120 def has_selected_range_limit?(solr_field) params["range"] && params["range"][solr_field] && ( params["range"][solr_field]["begin"].present? || params["range"][solr_field]["end"].present? || params["range"][solr_field]["missing"] ) end |
#range_display(solr_field, my_params = params) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/helpers/range_limit_helper.rb', line 36 def range_display(solr_field, my_params = params) return "" unless my_params[:range] && my_params[:range][solr_field] hash = my_params[:range][solr_field] if hash["missing"] return t('blacklight.range_limit.missing') elsif hash["begin"] || hash["end"] if hash["begin"] == hash["end"] return t( 'blacklight.range_limit.single_html', begin: format_range_display_value(hash['begin'], solr_field), begin_value: hash['begin'] ) else return t( 'blacklight.range_limit.range_html', begin: format_range_display_value(hash['begin'], solr_field), begin_value: hash['begin'], end: format_range_display_value(hash['end'], solr_field), end_value: hash['end'] ) end end '' end |
#range_limit_panel_url(options = {}) ⇒ Object
7 8 9 |
# File 'app/helpers/range_limit_helper.rb', line 7 def range_limit_panel_url( = {}) main_app.url_for(search_state.to_h.merge(action: 'range_limit_panel').merge()) end |
#range_limit_url(options = {}) ⇒ Object
3 4 5 |
# File 'app/helpers/range_limit_helper.rb', line 3 def range_limit_url( = {}) main_app.url_for(search_state.to_h.merge(action: 'range_limit').merge()) end |
#range_results_endpoint(solr_field, type) ⇒ Object
type is ‘min’ or ‘max’ Returns smallest and largest value in current result set, if available from stats component response.
25 26 27 28 29 30 31 32 33 34 |
# File 'app/helpers/range_limit_helper.rb', line 25 def range_results_endpoint(solr_field, type) stats = stats_for_field(solr_field) return nil unless stats # StatsComponent returns weird min/max when there are in # fact no values return nil if @response.total == stats["missing"] return stats[type].to_s.gsub(/\.0+/, '') end |
#render_range_input(solr_field, type, input_label = nil, maxlength = 4) ⇒ Object
type is ‘begin’ or ‘end’
12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/range_limit_helper.rb', line 12 def render_range_input(solr_field, type, input_label = nil, maxlength=4) type = type.to_s default = params["range"][solr_field][type] if params["range"] && params["range"][solr_field] && params["range"][solr_field][type] html = text_field_tag("range[#{solr_field}][#{type}]", default, :maxlength=>maxlength, :class => "form-control text-center range_#{type}") html += label_tag("range[#{solr_field}][#{type}]", input_label, class: 'sr-only') if input_label.present? html end |
#selected_missing_for_range_limit?(solr_field) ⇒ Boolean
130 131 132 |
# File 'app/helpers/range_limit_helper.rb', line 130 def selected_missing_for_range_limit?(solr_field) params["range"] && params["range"][solr_field] && params["range"][solr_field]["missing"] end |
#should_show_limit(solr_field) ⇒ Object
Show the limit area if: 1) we have a limit already set OR 2) stats show max > min, OR 3) count > 0 if no stats available.
76 77 78 79 80 81 82 83 |
# File 'app/helpers/range_limit_helper.rb', line 76 def should_show_limit(solr_field) stats = stats_for_field(solr_field) (params["range"] && params["range"][solr_field]) || ( stats && stats["max"] > stats["min"]) || ( !stats && @response.total > 0 ) end |
#stats_for_field(solr_field) ⇒ Object
85 86 87 |
# File 'app/helpers/range_limit_helper.rb', line 85 def stats_for_field(solr_field) @response["stats"]["stats_fields"][solr_field] if @response["stats"] && @response["stats"]["stats_fields"] end |
#stats_for_field?(solr_field) ⇒ Boolean
89 90 91 |
# File 'app/helpers/range_limit_helper.rb', line 89 def stats_for_field?(solr_field) stats_for_field(solr_field).present? end |