diff --git a/lib/blacklight_range_limit/segment_calculation.rb b/lib/blacklight_range_limit/segment_calculation.rb index f645176..1f49a6f 100644 --- a/lib/blacklight_range_limit/segment_calculation.rb +++ b/lib/blacklight_range_limit/segment_calculation.rb @@ -1,7 +1,6 @@ # Meant to be in a Controller, included in our ControllerOverride module. module BlacklightRangeLimit module SegmentCalculation - protected # Calculates segment facets within a given start and end on a given @@ -12,7 +11,8 @@ module SegmentCalculation # # Changes solr_params passed in. def add_range_segments_to_solr!(solr_params, facet_field, min, max) - raise InvalidRange, "The min date must be before the max date" if min > max + raise InvalidRange, 'The min date must be before the max date' if min > max + field_config = blacklight_config.facet_fields[facet_field.to_s] return solr_params unless field_config @@ -26,7 +26,7 @@ def add_range_segments_to_solr!(solr_params, facet_field, min, max) # Now make the boundaries into actual filter.queries. 0.upto(boundaries.length - 2) do |index| first = boundaries[index] - last = boundaries[index+1].to_i - 1 + last = boundaries[index + 1].to_i - 1 solr_params[:"facet.query"] << "#{field_config.field}:[#{first} TO #{last}]" end @@ -43,7 +43,8 @@ def add_range_segments_to_solr!(solr_params, facet_field, min, max) # be turned into inclusive ranges, the FINAL boundary will be one # unit more than the actual end of the last range later computed. def boundaries_for_range_facets(first, last, num_div) - raise ArgumentError, "The first date must be before the last date" if last < first + raise ArgumentError, 'The first date must be before the last date' if last < first + # arithmetic issues require last to be one more than the actual # last value included in our inclusive range last += 1 @@ -54,58 +55,55 @@ def boundaries_for_range_facets(first, last, num_div) # Don't know what most of these variables mean, just copying # from Flot. - dec = -1 * ( Math.log10(delta) ).floor - magn = (10 ** (-1 * dec)).to_f - norm = (magn == 0) ? delta : (delta / magn) # norm is between 1.0 and 10.0 + dec = -1 * Math.log10(delta).floor + magn = (10**(-1 * dec)).to_f + norm = magn == 0 ? delta : (delta / magn) # norm is between 1.0 and 10.0 size = 10 - if (norm < 1.5) - size = 1 - elsif (norm < 3) - size = 2; - # special case for 2.5, requires an extra decimal - if (norm > 2.25 ) - size = 2.5; - dec = dec + 1 - end - elsif (norm < 7.5) - size = 5 - end - - size = size * magn - - boundaries = [] - - start = floorInBase(first, size) - i = 0 - v = Float::MAX - prev = nil - begin - prev = v - v = start + i * size - boundaries.push(v.to_i) - i += 1 - end while ( v < last && v != prev) - - # Can create dups for small ranges, tighten up - boundaries.uniq! - - # That algorithm i don't entirely understand will sometimes - # extend past our first and last, tighten it up and make sure - # first and last are endpoints. - boundaries.delete_if {|b| b <= first || b >= last} - boundaries.unshift(first) - boundaries.push(last) - - return boundaries + if norm < 1.5 + size = 1 + elsif norm < 3 + size = 2 + # special case for 2.5, requires an extra decimal + if norm > 2.25 + size = 2.5 + dec + 1 + end + elsif norm < 7.5 + size = 5 + end + + size *= magn + + boundaries = [] + + start = floorInBase(first, size) + i = 0 + v = Float::MAX + prev = nil + begin + prev = v + v = start + i * size + boundaries.push(v.to_i) + i += 1 + end while (v < last && v != prev) + + # Can create dups for small ranges, tighten up + boundaries.uniq! + + # That algorithm i don't entirely understand will sometimes + # extend past our first and last, tighten it up and make sure + # first and last are endpoints. + boundaries.delete_if { |b| b <= first || b >= last } + boundaries.unshift(first) + boundaries.push(last) + + boundaries end # Cribbed from Flot. Round to nearby lower multiple of base def floorInBase(n, base) - return base * (n / base).floor + base * (n / base).floor end - - - end end