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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecae24335def2fe0a7246f68975022ddbfaa54ec894a5a159f78150937ea49e7
4
- data.tar.gz: '09650474feb511a86c4c95a1f51f853db85bda550a76d92102a76cc6e3ac8970'
3
+ metadata.gz: c688a2fe70c452de1943ddee5edfb3663341e0b416f24488fff082612b93be50
4
+ data.tar.gz: 18513d2d6e29658258d7bb5cfb2efa1d58e1f30eb443a159fbec8cba7f5a9840
5
5
  SHA512:
6
- metadata.gz: 101d6c2253540fee9b420ea1923790dd2e377190a67a69c0f3532261145eae9dfa58b781a60645e82ce476720f4e65a6798000af5091578b1d17558db5335560
7
- data.tar.gz: e585d9aa6de7ca3058174566754e5256140e38c01c8b81bf74c1b5934890199d02575c8a985cbf15417b3991d23fd6a171effca2be7ac800c561cb6b3970574d
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
 
@@ -5,6 +5,14 @@ module FacetedSearch
5
5
  params == "true"
6
6
  end
7
7
 
8
+ def selected_values_count
9
+ selected? ? 1 : 0
10
+ end
11
+
12
+ def selectable?
13
+ selected? || facets.can_select_more?
14
+ end
15
+
8
16
  def add_scope(scope)
9
17
  selected? ? scope.where(name => true)
10
18
  : scope
@@ -37,6 +37,11 @@ module FacetedSearch
37
37
  @options[:param_name] || name
38
38
  end
39
39
 
40
+ # Counted in FacetedSearch::Facets.max_selected_values
41
+ def selected_values_count
42
+ params.present? ? 1 : 0
43
+ end
44
+
40
45
  def kind
41
46
  self.class.to_s
42
47
  end
@@ -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
- <% if searchable %>
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,6 +9,7 @@ path = facet.facets.path_for(facet, target) + anchor
9
9
  <input type="checkbox"
10
10
  onclick="window.location.href='<%= path %>'"
11
11
  <%= 'checked' if facet.selected? %>
12
+ <%= 'disabled' unless facet.selectable? %>
12
13
  />
13
14
  <%= facet.title %>
14
15
  </label>
@@ -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>
@@ -8,7 +8,8 @@
8
8
  <%= render 'faceted_search/link',
9
9
  display_value: year,
10
10
  path: facet.facets.path_for(facet, year) + anchor,
11
- searchable: facet.searchable %>
11
+ searchable: facet.searchable,
12
+ selectable: facet.value_selectable?(year) %>
12
13
  </li>
13
14
  <% end %>
14
15
  </ol>
@@ -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>
@@ -27,7 +27,8 @@
27
27
  <%= render 'faceted_search/link',
28
28
  display_value: display_value,
29
29
  path: path,
30
- searchable: facet.searchable %>
30
+ searchable: facet.searchable,
31
+ selectable: facet.value_selectable?(identifier) %>
31
32
  </li>
32
33
  <% end %>
33
34
  </ol>
@@ -1,3 +1,3 @@
1
1
  module FacetedSearch
2
- VERSION = '4.0.0.beta1'
2
+ VERSION = '4.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faceted_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnaud Levy