11# Meant to be in a Controller, included in our ControllerOverride module.
22module BlacklightRangeLimit
33 module SegmentCalculation
4-
54 protected
65
76 # Calculates segment facets within a given start and end on a given
@@ -12,7 +11,8 @@ module SegmentCalculation
1211 #
1312 # Changes solr_params passed in.
1413 def add_range_segments_to_solr! ( solr_params , facet_field , min , max )
15- raise InvalidRange , "The min date must be before the max date" if min > max
14+ raise InvalidRange , 'The min date must be before the max date' if min > max
15+
1616 field_config = blacklight_config . facet_fields [ facet_field . to_s ]
1717
1818 return solr_params unless field_config
@@ -26,7 +26,7 @@ def add_range_segments_to_solr!(solr_params, facet_field, min, max)
2626 # Now make the boundaries into actual filter.queries.
2727 0 . upto ( boundaries . length - 2 ) do |index |
2828 first = boundaries [ index ]
29- last = boundaries [ index + 1 ] . to_i - 1
29+ last = boundaries [ index + 1 ] . to_i - 1
3030
3131 solr_params [ :"facet.query" ] << "#{ field_config . field } :[#{ first } TO #{ last } ]"
3232 end
@@ -43,7 +43,8 @@ def add_range_segments_to_solr!(solr_params, facet_field, min, max)
4343 # be turned into inclusive ranges, the FINAL boundary will be one
4444 # unit more than the actual end of the last range later computed.
4545 def boundaries_for_range_facets ( first , last , num_div )
46- raise ArgumentError , "The first date must be before the last date" if last < first
46+ raise ArgumentError , 'The first date must be before the last date' if last < first
47+
4748 # arithmetic issues require last to be one more than the actual
4849 # last value included in our inclusive range
4950 last += 1
@@ -54,58 +55,55 @@ def boundaries_for_range_facets(first, last, num_div)
5455
5556 # Don't know what most of these variables mean, just copying
5657 # from Flot.
57- dec = -1 * ( Math . log10 ( delta ) ) . floor
58- magn = ( 10 ** ( -1 * dec ) ) . to_f
59- norm = ( magn == 0 ) ? delta : ( delta / magn ) # norm is between 1.0 and 10.0
58+ dec = -1 * Math . log10 ( delta ) . floor
59+ magn = ( 10 ** ( -1 * dec ) ) . to_f
60+ norm = magn == 0 ? delta : ( delta / magn ) # norm is between 1.0 and 10.0
6061
6162 size = 10
62- if ( norm < 1.5 )
63- size = 1
64- elsif ( norm < 3 )
65- size = 2 ;
66- # special case for 2.5, requires an extra decimal
67- if ( norm > 2.25 )
68- size = 2.5 ;
69- dec = dec + 1
70- end
71- elsif ( norm < 7.5 )
72- size = 5
73- end
74-
75- size = size * magn
76-
77- boundaries = [ ]
78-
79- start = floorInBase ( first , size )
80- i = 0
81- v = Float ::MAX
82- prev = nil
83- begin
84- prev = v
85- v = start + i * size
86- boundaries . push ( v . to_i )
87- i += 1
88- end while ( v < last && v != prev )
89-
90- # Can create dups for small ranges, tighten up
91- boundaries . uniq!
92-
93- # That algorithm i don't entirely understand will sometimes
94- # extend past our first and last, tighten it up and make sure
95- # first and last are endpoints.
96- boundaries . delete_if { |b | b <= first || b >= last }
97- boundaries . unshift ( first )
98- boundaries . push ( last )
99-
100- return boundaries
63+ if norm < 1.5
64+ size = 1
65+ elsif norm < 3
66+ size = 2
67+ # special case for 2.5, requires an extra decimal
68+ if norm > 2.25
69+ size = 2.5
70+ dec + 1
71+ end
72+ elsif norm < 7.5
73+ size = 5
74+ end
75+
76+ size *= magn
77+
78+ boundaries = [ ]
79+
80+ start = floorInBase ( first , size )
81+ i = 0
82+ v = Float ::MAX
83+ prev = nil
84+ begin
85+ prev = v
86+ v = start + i * size
87+ boundaries . push ( v . to_i )
88+ i += 1
89+ end while ( v < last && v != prev )
90+
91+ # Can create dups for small ranges, tighten up
92+ boundaries . uniq!
93+
94+ # That algorithm i don't entirely understand will sometimes
95+ # extend past our first and last, tighten it up and make sure
96+ # first and last are endpoints.
97+ boundaries . delete_if { |b | b <= first || b >= last }
98+ boundaries . unshift ( first )
99+ boundaries . push ( last )
100+
101+ boundaries
101102 end
102103
103104 # Cribbed from Flot. Round to nearby lower multiple of base
104105 def floorInBase ( n , base )
105- return base * ( n / base ) . floor
106+ base * ( n / base ) . floor
106107 end
107-
108-
109-
110108 end
111109end
0 commit comments