Module: BlacklightRangeLimit::ViewHelperOverride

Defined in:
lib/blacklight_range_limit/view_helper_override.rb

Overview

Meant to be applied on top of Blacklight helpers, to over-ride Will add rendering of limit itself in sidebar, and of constraings display.

Instance Method Summary collapse

Instance Method Details

#facet_field_in_params?(field_name) ⇒ Boolean

Over-ride to recognize our custom params for range facets

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 32

def facet_field_in_params?(field_name)
  return super || (
    range_config(field_name) &&
    params[:range] &&
    params[:range][field_name] &&
      ( params[:range][field_name]["begin"].present? ||
        params[:range][field_name]["end"].present? ||
        params[:range][field_name]["missing"].present?
      )
  )
end

#facet_partial_name(display_facet) ⇒ Object



8
9
10
11
12
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 8

def facet_partial_name(display_facet)
  config = range_config(display_facet.name)
  return config[:partial] || 'blacklight_range_limit/range_limit_panel' if config && should_show_limit(display_facet.name)
  super
end

#has_range_limit_parameters?(my_params = params) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 14

def has_range_limit_parameters?(my_params = params)
  my_params[:range] &&
    my_params[:range].to_unsafe_h.any? do |key, v|
      v.present? && v.respond_to?(:'[]') &&
      (v["begin"].present? || v["end"].present? || v["missing"].present?)
    end
end

#has_search_parameters?Boolean

over-ride, call super, but make sure our range limits count too

Returns:

  • (Boolean)


23
24
25
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 23

def has_search_parameters?
  super || has_range_limit_parameters?
end

#query_has_constraints?(my_params = params) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 27

def query_has_constraints?(my_params = params)
  super || has_range_limit_parameters?(my_params)
end

#range_config(solr_field) ⇒ Object



109
110
111
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 109

def range_config(solr_field)
  BlacklightRangeLimit.range_config(blacklight_config, solr_field)
end

#remove_range_param(solr_field, my_params = params) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 80

def remove_range_param(solr_field, my_params = params)
  my_params = Blacklight::SearchState.new(my_params, blacklight_config).to_h
  if ( my_params["range"] )
    my_params = my_params.dup
    my_params["range"] = my_params["range"].dup
    my_params["range"].delete(solr_field)
  end
  return my_params
end

#render_constraints_filters(my_params = params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 44

def render_constraints_filters(my_params = params)
  content = super(my_params)
  # add a constraint for ranges?
  unless my_params[:range].blank?
    my_params[:range].each_pair do |solr_field, hash|

      next unless hash["missing"] || (!hash["begin"].blank?) || (!hash["end"].blank?)
      content << render_constraint_element(
        facet_field_label(solr_field),
        range_display(solr_field, my_params),
        :escape_value => false,
        :remove => remove_range_param(solr_field, my_params)
      )
    end
  end
  return content
end

#render_search_to_s_filters(my_params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 62

def render_search_to_s_filters(my_params)
  content = super(my_params)
  # add a constraint for ranges?
  unless my_params[:range].blank?
    my_params[:range].each_pair do |solr_field, hash|
      next unless hash["missing"] || (!hash["begin"].blank?) || (! hash["end"].blank?)

      content << render_search_to_s_element(
        facet_field_label(solr_field),
        range_display(solr_field, my_params),
        :escape_value => false
      )

    end
  end
  return content
end

#solr_range_queries_to_a(solr_field) ⇒ Object

Looks in the solr @response for [“facet_counts“][solr_field], for elements expressed as ”solr_field:[X to Y]“, turns them into a list of hashes with [:from, :to, :count], sorted by :from. Assumes integers for sorting purposes.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/blacklight_range_limit/view_helper_override.rb', line 94

def solr_range_queries_to_a(solr_field)
  return [] unless @response["facet_counts"] && @response["facet_counts"]["facet_queries"]

  array = []

  @response["facet_counts"]["facet_queries"].each_pair do |query, count|
    if query =~ /#{solr_field}: *\[ *(\d+) *TO *(\d+) *\]/
      array << {:from => $1, :to => $2, :count => count}
    end
  end
  array = array.sort_by {|hash| hash[:from].to_i }

  return array
end