faceted_search 4.0.0.beta1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +31 -0
- data/app/models/faceted_search/facets/boolean.rb +8 -0
- data/app/models/faceted_search/facets/default.rb +5 -0
- data/app/models/faceted_search/facets/default_list.rb +10 -0
- data/app/models/faceted_search/facets/text.rb +5 -0
- data/app/models/faceted_search/facets.rb +44 -0
- data/app/views/faceted_search/_link.html.erb +4 -1
- data/app/views/faceted_search/facets/boolean/_all.html.erb +1 -0
- data/app/views/faceted_search/facets/checkboxes/_all.html.erb +4 -1
- data/app/views/faceted_search/facets/date/_all.html.erb +2 -1
- data/app/views/faceted_search/facets/full_tree/_values.html.erb +2 -1
- data/app/views/faceted_search/facets/list/_all.html.erb +2 -1
- data/app/views/faceted_search/facets/primitive_list/_all.html.erb +4 -1
- data/app/views/faceted_search/facets/tree/_all.html.erb +2 -1
- data/lib/faceted_search/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c688a2fe70c452de1943ddee5edfb3663341e0b416f24488fff082612b93be50
|
|
4
|
+
data.tar.gz: 18513d2d6e29658258d7bb5cfb2efa1d58e1f30eb443a159fbec8cba7f5a9840
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0c6cf3fff62efe5a06047a2248a274e8e35f07d9b3dae701c9a3cb7e8ecc86b70e6a5751c6e6f6e390f246c5e2cb4d010b1c23d5a521ccef6003fa71cf610e7d
|
|
7
|
+
data.tar.gz: 4c9a6f395eae280ba315104f1953c8c80b05bf2dc78210f11e08e36a755b974627a1187508b32d84cd3cef18ff8cc0cc221a995aa6771a78d46478238ffc6fdb
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.1.0
|
|
4
|
+
|
|
5
|
+
Limit of selected values, against robots crawling every combination of filters.
|
|
6
|
+
|
|
7
|
+
- At most 10 values can be selected at once, all facets together (text facets are not counted). At the limit, the partials do not link to other values anymore (they are displayed as text, checkboxes are disabled), and selected values can still be unselected.
|
|
8
|
+
- Change the limit globally or for one facets class, remove it with `nil` (in an initializer):
|
|
9
|
+
|
|
10
|
+
FacetedSearch::Facets.max_selected_values = 20
|
|
11
|
+
Item::Facets.max_selected_values = 5
|
|
12
|
+
FacetedSearch::Facets.max_selected_values = nil
|
|
13
|
+
|
|
14
|
+
- `too_many_selected_values?` refuses the requests beyond the limit: on the facets in a controller (`head :bad_request`), or on the class with raw params in a Rack::Attack blocklist (`Item::Facets.too_many_selected_values?(params, except: [:search])`). `selected_values_count` and `can_select_more?` are available too.
|
|
15
|
+
- In overridden partials, use `facet.value_selectable?(value)` (lists) or `facet.selectable?` (booleans), and pass `selectable:` to the `faceted_search/link` partial.
|
|
16
|
+
|
|
17
|
+
See "Limit of selected values" in the README.
|
|
18
|
+
|
|
19
|
+
### Upgrading from 4.0
|
|
20
|
+
|
|
21
|
+
- 10 values was not a limit before: if your users need more, raise it or remove it.
|
|
22
|
+
- If you overrode the partials of the gem, they still link to every value: use `value_selectable?` to get the same behavior.
|
|
23
|
+
|
|
3
24
|
## 4.0.0
|
|
4
25
|
|
|
5
26
|
Facets are much cheaper for the database: no more `JOIN` + `DISTINCT`, only semi-joins (`WHERE id IN (subquery)`).
|
data/README.md
CHANGED
|
@@ -240,6 +240,36 @@ Links are `rel="nofollow"` (unless the facet is `searchable`), but it is not eno
|
|
|
240
240
|
request.ip if request.query_string.include?('facets')
|
|
241
241
|
end
|
|
242
242
|
|
|
243
|
+
- Limit the number of selected values (see below), and block the requests beyond the limit.
|
|
244
|
+
|
|
245
|
+
### Limit of selected values
|
|
246
|
+
|
|
247
|
+
A human rarely selects more than a few values, robots crawl combinations of dozens.
|
|
248
|
+
By default, 10 values can be selected at once, all facets together (text facets are not counted).
|
|
249
|
+
When the limit is reached, the partials do not link to other values anymore (they are displayed as text, checkboxes are disabled), and selected values can still be unselected.
|
|
250
|
+
|
|
251
|
+
Change the limit, or remove it with `nil`, globally (in an initializer) or for one facets class:
|
|
252
|
+
|
|
253
|
+
FacetedSearch::Facets.max_selected_values = 5
|
|
254
|
+
Item::Facets.max_selected_values = 20
|
|
255
|
+
FacetedSearch::Facets.max_selected_values = nil
|
|
256
|
+
|
|
257
|
+
Requests beyond the limit do not come from the links of the partials. Refuse them in the controller:
|
|
258
|
+
|
|
259
|
+
@facets = Item::Facets.new params[:facets]
|
|
260
|
+
return head :bad_request if @facets.too_many_selected_values?
|
|
261
|
+
|
|
262
|
+
Or earlier, without building the facets nor reaching Rails, with Rack::Attack.
|
|
263
|
+
Values are counted in the raw params, so give the params of the text facets in `except` (a search with commas is not a list):
|
|
264
|
+
|
|
265
|
+
Rack::Attack.blocklist('facets/too many values') do |request|
|
|
266
|
+
next false unless request.query_string.include?('facets')
|
|
267
|
+
facets = Rack::Utils.parse_nested_query(request.query_string)['facets'] rescue nil
|
|
268
|
+
Item::Facets.too_many_selected_values?(facets, except: [:title])
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
In your own partials, use `facet.value_selectable?(value)` (lists) or `facet.selectable?` (booleans), `facets.can_select_more?` and `facets.selected_values_count`.
|
|
272
|
+
|
|
243
273
|
## Upgrading to 4.0
|
|
244
274
|
|
|
245
275
|
If you only use the facets of the gem, the results do not change.
|
|
@@ -310,6 +340,7 @@ The tests use the fixtures of `test/fixtures` and the models of the dummy app (`
|
|
|
310
340
|
- `test/faceted_search_test.rb`: results of the main combinations of filters
|
|
311
341
|
- `test/semi_joins_test.rb`: habtm facets combined, OR inside a facet, values of the facets, `COUNT` without `DISTINCT`, queries run by the partials
|
|
312
342
|
- `test/facets_behavior_test.rb`: `find_by`, custom sources and facets, options, paths, trees
|
|
343
|
+
- `test/max_selected_values_test.rb`: limit of selected values, count, partials at the limit
|
|
313
344
|
|
|
314
345
|
### Trying the dummy app
|
|
315
346
|
|
|
@@ -42,6 +42,16 @@ module FacetedSearch
|
|
|
42
42
|
value.to_s.in? params_array
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
def selected_values_count
|
|
46
|
+
params_array.count(&:present?)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A selected value can always be unselected,
|
|
50
|
+
# other values only while the limit of selected values is not reached
|
|
51
|
+
def value_selectable?(value)
|
|
52
|
+
value_selected?(value) || facets.can_select_more?
|
|
53
|
+
end
|
|
54
|
+
|
|
45
55
|
def path_for(value)
|
|
46
56
|
value = value.to_s
|
|
47
57
|
custom_params = params_array.dup
|
|
@@ -6,6 +6,11 @@ module FacetedSearch
|
|
|
6
6
|
@options[:placeholder]
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
+
# A search is not a combination of values: not counted
|
|
10
|
+
def selected_values_count
|
|
11
|
+
0
|
|
12
|
+
end
|
|
13
|
+
|
|
9
14
|
def add_scope(scope)
|
|
10
15
|
return scope if params.blank?
|
|
11
16
|
scope.where(where_condition, search_term: "%#{self.class.sanitize_sql_like(params)}%")
|
|
@@ -10,6 +10,35 @@ module FacetedSearch
|
|
|
10
10
|
# or for one facets class (Item::Facets.distinct_results = true).
|
|
11
11
|
class_attribute :distinct_results, instance_writer: false, default: false
|
|
12
12
|
|
|
13
|
+
# Maximum number of values selected at once, all facets together
|
|
14
|
+
# (text facets are not counted).
|
|
15
|
+
# Beyond that, the combination makes no sense for a human: robots follow the
|
|
16
|
+
# links of the facets and crawl every combination, each running several queries.
|
|
17
|
+
# When it is reached, the partials do not link to more values (selected values
|
|
18
|
+
# can still be unselected).
|
|
19
|
+
# Can be set globally (FacetedSearch::Facets.max_selected_values = 5)
|
|
20
|
+
# or for one facets class (Item::Facets.max_selected_values = 20).
|
|
21
|
+
# Set to nil to remove the limit.
|
|
22
|
+
class_attribute :max_selected_values, instance_writer: false, default: 10
|
|
23
|
+
|
|
24
|
+
# Number of values in raw facets params, without building the facets,
|
|
25
|
+
# for instance in a Rack::Attack blocklist.
|
|
26
|
+
# Values are separated by commas: give the params of the text facets in except,
|
|
27
|
+
# a search with commas is not a list.
|
|
28
|
+
def self.selected_values_count(params, except: [])
|
|
29
|
+
return 0 unless params.respond_to?(:each_pair)
|
|
30
|
+
except = except.map(&:to_s)
|
|
31
|
+
params.each_pair.sum do |name, value|
|
|
32
|
+
next 0 if name.to_s.in?(except)
|
|
33
|
+
value.to_s.split(',').count(&:present?)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.too_many_selected_values?(params, except: [])
|
|
38
|
+
max_selected_values.present? &&
|
|
39
|
+
selected_values_count(params, except: except) > max_selected_values
|
|
40
|
+
end
|
|
41
|
+
|
|
13
42
|
def initialize(params)
|
|
14
43
|
if params.is_a? ActionController::Parameters
|
|
15
44
|
@params = params.to_unsafe_hash
|
|
@@ -57,6 +86,21 @@ module FacetedSearch
|
|
|
57
86
|
: scope.all
|
|
58
87
|
end
|
|
59
88
|
|
|
89
|
+
# Number of values selected in all the facets (text facets are not counted)
|
|
90
|
+
def selected_values_count
|
|
91
|
+
list.sum(&:selected_values_count)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def can_select_more?
|
|
95
|
+
max_selected_values.nil? || selected_values_count < max_selected_values
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Requests beyond the limit do not come from the links of the partials:
|
|
99
|
+
# the application can refuse them (head :bad_request).
|
|
100
|
+
def too_many_selected_values?
|
|
101
|
+
max_selected_values.present? && selected_values_count > max_selected_values
|
|
102
|
+
end
|
|
103
|
+
|
|
60
104
|
def model_table_name
|
|
61
105
|
@model.table_name
|
|
62
106
|
end
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
<%
|
|
1
|
+
<% selectable = local_assigns.fetch(:selectable, true) %>
|
|
2
|
+
<% if !selectable %>
|
|
3
|
+
<span class="faceted__unselectable text-muted"><%= display_value %></span>
|
|
4
|
+
<% elsif searchable %>
|
|
2
5
|
<%= link_to display_value, path %>
|
|
3
6
|
<% else %>
|
|
4
7
|
<%= link_to display_value, path, rel: 'nofollow' %>
|
|
@@ -9,16 +9,19 @@
|
|
|
9
9
|
name = facet.display_method.call(value)
|
|
10
10
|
path = facet.facets.path_for(facet, identifier) + anchor
|
|
11
11
|
selected = facet.value_selected?(identifier)
|
|
12
|
+
selectable = facet.value_selectable?(identifier)
|
|
12
13
|
%>
|
|
13
14
|
<li class="faceted__facet__checkboxes__value<%= '--selected' if selected %>">
|
|
14
15
|
<input type="checkbox"
|
|
15
16
|
onclick="window.location.href='<%= path %>'"
|
|
16
17
|
<%= 'checked' if selected %>
|
|
18
|
+
<%= 'disabled' unless selectable %>
|
|
17
19
|
/>
|
|
18
20
|
<%= render 'faceted_search/link',
|
|
19
21
|
display_value: name,
|
|
20
22
|
path: path,
|
|
21
|
-
searchable: facet.searchable
|
|
23
|
+
searchable: facet.searchable,
|
|
24
|
+
selectable: selectable %>
|
|
22
25
|
</li>
|
|
23
26
|
<% end %>
|
|
24
27
|
</ul>
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
<%= render 'faceted_search/link',
|
|
6
6
|
display_value: facet.display_method.call(value),
|
|
7
7
|
path: facet.facets.path_for(facet, identifier) + anchor,
|
|
8
|
-
searchable: facet.searchable
|
|
8
|
+
searchable: facet.searchable,
|
|
9
|
+
selectable: facet.value_selectable?(identifier) %>
|
|
9
10
|
<% child_values = facet.values_with_parent(value.id) %>
|
|
10
11
|
<% if child_values.any? %>
|
|
11
12
|
<ol class="dd-list">
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
<%= render 'faceted_search/link',
|
|
13
13
|
display_value: display_value,
|
|
14
14
|
path: facet.facets.path_for(facet, identifier) + anchor,
|
|
15
|
-
searchable: facet.searchable
|
|
15
|
+
searchable: facet.searchable,
|
|
16
|
+
selectable: facet.value_selectable?(identifier) %>
|
|
16
17
|
</li>
|
|
17
18
|
<% end %>
|
|
18
19
|
</ol>
|
|
@@ -7,16 +7,19 @@
|
|
|
7
7
|
<%
|
|
8
8
|
path = facet.facets.path_for(facet, identifier) + anchor
|
|
9
9
|
selected = facet.value_selected?(identifier)
|
|
10
|
+
selectable = facet.value_selectable?(identifier)
|
|
10
11
|
%>
|
|
11
12
|
<li class="faceted__facet__checkboxes__value<%= '--selected' if selected %>">
|
|
12
13
|
<input type="checkbox"
|
|
13
14
|
onclick="window.location.href='<%= path %>'"
|
|
14
15
|
<%= 'checked' if selected %>
|
|
16
|
+
<%= 'disabled' unless selectable %>
|
|
15
17
|
/>
|
|
16
18
|
<%= render 'faceted_search/link',
|
|
17
19
|
display_value: identifier,
|
|
18
20
|
path: path,
|
|
19
|
-
searchable: facet.searchable
|
|
21
|
+
searchable: facet.searchable,
|
|
22
|
+
selectable: selectable %>
|
|
20
23
|
</li>
|
|
21
24
|
<% end %>
|
|
22
25
|
</ul>
|