Module: Blacklight::RenderConstraintsHelperBehavior

Included in:
RenderConstraintsHelper
Defined in:
app/helpers/blacklight/render_constraints_helper_behavior.rb

Overview

All methods in here are 'api' that may be over-ridden by plugins and local code, so method signatures and semantics should not be changed casually. implementations can be of course.

Includes methods for rendering contraints graphically on the search results page (render_constraints(_*))

Instance Method Summary collapse

Instance Method Details

#query_has_constraints?(localized_params = params) ⇒ Boolean

Check if the query has any constraints defined (a query, facet, etc)

Parameters:

  • localized_params (Hash) (defaults to: params)

    query parameters

Returns:

  • (Boolean)


14
15
16
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 14

def query_has_constraints?(localized_params = params)
  !(localized_params[:q].blank? and localized_params[:f].blank?)
end

#remove_constraint_url(localized_params) ⇒ String

Provide a url for removing a particular constraint. This can be overriden in the case that you want parameters other than the defaults to be removed (e.g. :search_field)

Parameters:

  • localized_params (ActionController::Parameters)

    query parameters

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 50

def remove_constraint_url(localized_params)
  scope = localized_params.delete(:route_set) || self

  unless localized_params.is_a? ActionController::Parameters
    localized_params = ActionController::Parameters.new(localized_params)
  end

  options = localized_params.merge(q: nil, action: 'index')
  options.permit!
  scope.url_for(options)
end

#render_constraint_element(label, value, options = {}) ⇒ String

Render a label/value constraint on the screen. Can be called by plugins and such to get application-defined rendering.

Can be over-ridden locally to render differently if desired, although in most cases you can just change CSS instead.

Can pass in nil label if desired.

Parameters:

  • label (String)

    to display

  • value (String)

    to display

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :remove (String)

    url to execute for a 'remove' action

  • :classes (Array<String>)

    an array of classes to add to container span for constraint.

Returns:

  • (String)


109
110
111
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 109

def render_constraint_element(label, value, options = {})
  render(:partial => "catalog/constraints_element", :locals => {:label => label, :value => value, :options => options})
end

#render_constraints(localized_params = params) ⇒ String

Render the actual constraints, not including header or footer info.

Parameters:

  • localized_params (Hash) (defaults to: params)

    query parameters

Returns:

  • (String)


24
25
26
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 24

def render_constraints(localized_params = params)
  render_constraints_query(localized_params) + render_constraints_filters(localized_params)
end

#render_constraints_filters(localized_params = params) ⇒ String

Render the facet constraints

Parameters:

  • localized_params (Hash) (defaults to: params)

    query parameters

Returns:

  • (String)


66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 66

def render_constraints_filters(localized_params = params)
  return "".html_safe unless localized_params[:f]
  path = controller.search_state_class.new(localized_params, blacklight_config, controller)
  content = []
  localized_params[:f].each_pair do |facet,values|
    content << render_filter_element(facet, values, path)
  end

  safe_join(content.flatten, "\n")
end

#render_constraints_query(localized_params = params) ⇒ String

Render the query constraints

Parameters:

  • localized_params (ActionController::Parameters) (defaults to: params)

    query parameters

Returns:

  • (String)


33
34
35
36
37
38
39
40
41
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 33

def render_constraints_query(localized_params = params)
  # So simple don't need a view template, we can just do it here.
  return "".html_safe if localized_params[:q].blank?

  render_constraint_element(constraint_query_label(localized_params),
        localized_params[:q],
        classes: ["query"],
        remove: remove_constraint_url(localized_params))
end

#render_filter_element(facet, values, path) ⇒ String

Render a single facet's constraint

Parameters:

  • facet (String)

    field

  • values (Array<String>)

    selected facet values

  • path (Blacklight::SearchState)

    query parameters

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/blacklight/render_constraints_helper_behavior.rb', line 83

def render_filter_element(facet, values, path)
  facet_config = facet_configuration_for_field(facet)

  safe_join(Array(values).map do |val|
    next if val.blank? # skip empty string
    render_constraint_element(facet_field_label(facet_config.key),
                              facet_display_value(facet, val),
                              remove: search_action_path(path.remove_facet_params(facet, val)),
                              classes: ["filter", "filter-" + facet.parameterize])
  end, "\n")
end