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

Instance Method Details

#add_range(solr_field, from, to, my_params = params) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/range_limit_helper.rb', line 85

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



76
77
78
79
80
81
82
83
# File 'app/helpers/range_limit_helper.rb', line 76

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

#has_selected_range_limit?(solr_field) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# File 'app/helpers/range_limit_helper.rb', line 103

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
# 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: h(hash['begin']))
    else
      return t('blacklight.range_limit.range_html', begin: h(hash['begin']), end: h(hash['end']))
    end
  end

  return ""
end

#range_limit_panel_url(options = {}) ⇒ Object



7
8
9
# File 'app/helpers/range_limit_helper.rb', line 7

def range_limit_panel_url(options = {})
  main_app.url_for(search_state.to_h.merge(action: 'range_limit_panel').merge(options))
end

#range_limit_url(options = {}) ⇒ Object



3
4
5
# File 'app/helpers/range_limit_helper.rb', line 3

def range_limit_url(options = {})
  main_app.url_for(search_state.to_h.merge(action: 'range_limit').merge(options))
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

Returns:

  • (Boolean)


113
114
115
# File 'app/helpers/range_limit_helper.rb', line 113

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.



59
60
61
62
63
64
65
66
# File 'app/helpers/range_limit_helper.rb', line 59

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



68
69
70
# File 'app/helpers/range_limit_helper.rb', line 68

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

Returns:

  • (Boolean)


72
73
74
# File 'app/helpers/range_limit_helper.rb', line 72

def stats_for_field?(solr_field)
  stats_for_field(solr_field).present?
end