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
- #has_selected_range_limit?(solr_field) ⇒ Boolean
- #range_display(solr_field, my_params = params) ⇒ 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/helpers/range_limit_helper.rb', line 83 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
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/helpers/range_limit_helper.rb', line 69 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" # Need to ensure there's a search_field to trick Blacklight # into displaying results, not placeholder page. Kind of hacky, # but works for now. my_params["search_field"] ||= "dummy_range" my_params end |
#has_selected_range_limit?(solr_field) ⇒ Boolean
101 102 103 104 105 106 107 108 109 |
# File 'app/helpers/range_limit_helper.rb', line 101 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/helpers/range_limit_helper.rb', line 29 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 BlacklightRangeLimit.labels[:missing] elsif hash["begin"] || hash["end"] if hash["begin"] == hash["end"] return "<span class='single'>#{h(hash["begin"])}</span>".html_safe else return "<span class='from'>#{h(hash['begin'])}</span> to <span class='to'>#{h(hash['end'])}</span>".html_safe end end return "" 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.
18 19 20 21 22 23 24 25 26 27 |
# File 'app/helpers/range_limit_helper.rb', line 18 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’
5 6 7 8 9 10 11 12 13 |
# File 'app/helpers/range_limit_helper.rb', line 5 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 = label_tag("range[#{solr_field}][#{type}]", input_label, class: 'sr-only') if input_label.present? html ||= ''.html_safe html += text_field_tag("range[#{solr_field}][#{type}]", default, :maxlength=>maxlength, :class => "form-control range_#{type}") end |
#selected_missing_for_range_limit?(solr_field) ⇒ Boolean
111 112 113 |
# File 'app/helpers/range_limit_helper.rb', line 111 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.
52 53 54 55 56 57 58 59 |
# File 'app/helpers/range_limit_helper.rb', line 52 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
61 62 63 |
# File 'app/helpers/range_limit_helper.rb', line 61 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
65 66 67 |
# File 'app/helpers/range_limit_helper.rb', line 65 def stats_for_field?(solr_field) stats_for_field(solr_field).present? end |