Skip to content

Commit

Permalink
Merge pull request #294 from projectblacklight/range_values_clamped
Browse files Browse the repository at this point in the history
Clamp values between config'd min and max
  • Loading branch information
seanaery authored Nov 26, 2024
2 parents 0d16d99 + c6564f9 commit 1bacfec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
<div class="d-flex justify-content-between align-items-end">
<div class="d-flex flex-column mr-1 me-1">
<%= label_tag(begin_input_name, t("blacklight.range_limit.range_begin_short"), class: 'text-muted small mb-1') %>
<%= number_field_tag(begin_input_name, begin_value_default, class: "form-control form-control-sm range_begin") %>
<%= number_field_tag(begin_input_name,
begin_value_default,
min: range_config[:min_value],
max: range_config[:max_value],
class: "form-control form-control-sm range_begin")
%>
</div>

<div class="d-flex flex-column ml-1 ms-1">
<%= label_tag(end_input_name, t("blacklight.range_limit.range_end_short"), class: 'text-muted small mb-1') %>
<%= number_field_tag(end_input_name, end_value_default, class: "form-control form-control-sm range_end") %>
<%= number_field_tag(end_input_name,
end_value_default,
min: range_config[:min_value],
max: range_config[:max_value],
class: "form-control form-control-sm range_end")
%>
</div>
</div>
<div class="d-flex justify-content-end mt-2">
Expand Down
4 changes: 3 additions & 1 deletion lib/blacklight_range_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def self.default_range_config
chart_segment_border_color: 'rgb(54, 162, 235)',
chart_segment_bg_color: 'rgba(54, 162, 235, 0.5)',
chart_aspect_ratio: 2,
assumed_boundaries: nil
assumed_boundaries: nil,
min_value: -2_147_483_648, # solr intfield min and max
max_value: 2_147_483_648
},
filter_class: BlacklightRangeLimit::FilterField,
presenter: BlacklightRangeLimit::FacetFieldPresenter,
Expand Down
33 changes: 26 additions & 7 deletions lib/blacklight_range_limit/range_limit_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ def add_range_limit_params(solr_params)

selected_value = search_state.filter(config.key).values.first

range = if selected_value.is_a? Range
selected_value
elsif range_config[:assumed_boundaries]
Range.new(*range_config[:assumed_boundaries])
else
nil
end
range = bl_create_selected_range_value(selected_value, config)

# If we have both ends of a range
add_range_segments_to_solr!(solr_params, field_key, range.begin, range.end) if range && range.count != Float::INFINITY
Expand Down Expand Up @@ -98,5 +92,30 @@ def facet_value_to_fq_string(facet_field, value, use_local_params: true)
end
end

# @returns Range or nil
#
# Range created from a range value or from assumed boundaries if present, and clamped between min and max
def bl_create_selected_range_value(selected_value, field_config)
range_config = field_config.range_config

range = if selected_value.is_a? Range
selected_value
elsif range_config[:assumed_boundaries]
Range.new(*range_config[:assumed_boundaries])
else
nil
end

# clamp between config'd min and max
min = range_config[:min_value]
max = range_config[:max_value]

range = Range.new(
(range.begin.clamp(min, max) if range.begin),
(range.end.clamp(min, max) if range.end),
) if range

range
end
end
end

0 comments on commit 1bacfec

Please sign in to comment.