faceted_search 2.1.1 → 2.2.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: 427a86317140e3a5be8d82d673407cbafca492109bf7caf012abada45276ee3d
4
- data.tar.gz: 237bfd5aaa130318a08508171b70e5e4d025a631cef19025390ef2ce77d6c2dc
3
+ metadata.gz: f82d1b15fdbb630bd290e8d915b5dd109b7e07ad17561c959043a44a13af4ade
4
+ data.tar.gz: c958ae79ef81325ae652ebe54a0c9fdfebe7ebd0ce8b60a30188c0bb3de3a062
5
5
  SHA512:
6
- metadata.gz: 84153b104746f072e29c7dead126574830e5a35cf138c7647014c4282bbd6422bf34fee58ec0ccaf9c6c84bd265583798b756f73950516dcd9878438bcc73379
7
- data.tar.gz: e74d114ffbaa6285aa414c94197031ea458a707a8a6329dbfac7ec1b4ad4aae7589805ec17f9b7e9a363d469c0fb0515368ac2e960ebdbe6e0543f28b7d6dd60
6
+ metadata.gz: 91a8751b4523d08c0dff7112a05d5d87eebd6ed69c1c7ace85d438093d521c5421e860adeee41beeb931c0833143ced6a45f2f0f0fc16dcbbffdbdec861f4bc9
7
+ data.tar.gz: cd64b29320ff0a978e436a5df1b48f8f98319d512c3c9984756bc352d6f74d911b1ea2bbc48dbffc4bf17382eed3e95275dd1375b69f8ba54060e0a4364ce178
@@ -40,17 +40,33 @@ window.facetedSearch.Tree.prototype.generateItem = function (item) {
40
40
  liNode.className = item.selected ? "dd-item dd-item--selected"
41
41
  : "dd-item";
42
42
 
43
- var aNode = document.createElement('a');
44
- aNode.setAttribute("href", item.url);
45
- aNode.textContent = item.title;
43
+ var iconNode = null;
44
+ iconNode = document.createElement('i');
45
+ iconNode.className = "back-link fas fa-arrow-left";
46
+ var titleNode = document.createTextNode(item.title);
47
+
48
+ var itemContent = document.createDocumentFragment();
49
+ if (item.back === "true") {
50
+ itemContent.appendChild(iconNode);
51
+ }
52
+ itemContent.appendChild(titleNode);
53
+
54
+ if (item.url !== "") {
55
+ var aNode = document.createElement('a');
56
+ aNode.setAttribute("href", item.url);
57
+ aNode.appendChild(itemContent);
58
+ liNode.appendChild(aNode);
59
+ } else {
60
+ liNode.appendChild(itemContent);
61
+ }
46
62
 
47
- liNode.appendChild(aNode);
48
63
 
49
64
  if (item.children.length > 0) {
50
65
  var olNode = this.generateList(item.children);
51
66
  liNode.appendChild(olNode);
52
67
  }
53
68
 
69
+
54
70
  return liNode;
55
71
  };
56
72
 
@@ -82,6 +98,7 @@ window.facetedSearch.Tree.prototype.initDOM = function () {
82
98
  selected: elt.getAttribute('data-selected') === "true",
83
99
  resultsCount: parseInt(elt.getAttribute('data-count'), 10),
84
100
  url: elt.getAttribute('data-url'),
101
+ back: elt.getAttribute('data-back'),
85
102
  title: elt.textContent.trim()
86
103
  };
87
104
  });
@@ -103,9 +120,9 @@ window.facetedSearch.Tree.prototype.initNestable = function () {
103
120
  "use strict";
104
121
 
105
122
  $('.dd', this.wrapper).nestable({
106
- expandBtnHTML: '<button class="dd-expand-btn" data-action="expand" type="button"><i class="fas fa-folder"></i></button>',
107
- collapseBtnHTML: '<button class="dd-collapse-btn" data-action="collapse" type="button"><i class="fas fa-folder-open"></i></button>'
108
- }).data("nestable").collapseAll();
123
+ expandBtnHTML: '',
124
+ collapseBtnHTML: ''
125
+ }).data("nestable");
109
126
  };
110
127
 
111
128
  window.addEventListener('DOMContentLoaded', function () {
@@ -22,11 +22,15 @@
22
22
  font-size: 13px
23
23
  line-height: 20px
24
24
 
25
+
25
26
  a
26
27
  color: #8c8c8c
27
28
  &:hover
28
29
  color: #000
29
30
 
31
+ i.back-link
32
+ margin-right: 5px
33
+
30
34
  &--selected > a
31
35
  color: #000
32
36
 
@@ -14,36 +14,50 @@ module FacetedSearch
14
14
  # and return the modified scope
15
15
  def add_scope(scope)
16
16
  return scope if params_array.blank?
17
- habtm? ? scope.joins(name).where(name => { find_by => params_array })
18
- : scope.where(name => params_array)
17
+
18
+ identifiers = params_array.dup
19
+ identifiers << selected_objects.first.descendents if tree?
20
+ identifiers.flatten!
21
+
22
+ habtm? ? scope.joins(name).where(name => { find_by => identifiers })
23
+ : scope.where(name => identifiers)
19
24
  end
20
25
 
21
26
  def find_by
22
27
  @options[:find_by] || :id
23
28
  end
24
29
 
30
+ def selected_objects
31
+ values.where(find_by => params_array)
32
+ end
33
+
25
34
  # Show all values that have corresponding results.
26
35
  # This is a regular SQL inner join.
27
36
  def values
28
- joined_table = facets.model_table_name.to_sym
29
- source.all.joins(joined_table).where(joined_table => { id: facets.model }).distinct
37
+ @values ||= get_values
30
38
  end
31
39
 
32
- def value_selected?(value)
33
- value.to_s.in? params_array
40
+ def tree_values
41
+ return values.select { |obj| obj.root? } if params_array.empty?
42
+
43
+ selected_object = selected_objects.first
44
+ return [] if selected_object.nil?
45
+
46
+ array = []
47
+ array << values.find { |obj| obj.id == selected_object.parent_id } if selected_object.has_parent? # parent
48
+ array << values.select { |obj| obj.parent_id == selected_object.parent_id } # selected & siblings
49
+ array << values.select { |obj| obj.parent_id == selected_object.id } if selected_object.has_children? # selected object's children
50
+ array.flatten.compact
34
51
  end
35
52
 
36
- def results_with(value)
37
- scope = @facets.model
38
- @facets.list.each do |facet|
39
- scope = facet == self ? add_scope_with_value(scope, value)
40
- : facet.add_scope(scope)
41
- end
42
- scope
53
+ def value_selected?(value)
54
+ value.to_s.in? params_array
43
55
  end
44
56
 
45
57
  def path_for(value)
46
58
  value = value.to_s
59
+ return path_for_tree(value) if tree?
60
+
47
61
  custom_params = params_array.dup
48
62
  value_selected?(value) ? custom_params.delete(value)
49
63
  : custom_params.push(value)
@@ -54,18 +68,24 @@ module FacetedSearch
54
68
  @options[:source] || name.to_s.singularize.titleize.constantize.send(:all)
55
69
  end
56
70
 
71
+ def has_params?
72
+ params_array.any?
73
+ end
74
+
57
75
  protected
58
76
 
59
- def params_array
60
- @params_array ||= @params.to_s.split(',')
77
+ def get_values
78
+ joined_table = facets.model_table_name.to_sym
79
+ source.all.joins(joined_table).where(joined_table => { id: facets.model }).distinct
61
80
  end
62
81
 
63
- # Adds a scope corresponding to this facet
64
- # to the scope sent as an argument with specific value
65
- # and return the modified scope
66
- def add_scope_with_value(scope, value)
67
- habtm? ? scope.joins(name).where(name => { find_by => value })
68
- : scope.where(name => value)
82
+ def path_for_tree(value)
83
+ value_selected?(value) ? path('')
84
+ : path(value)
85
+ end
86
+
87
+ def params_array
88
+ @params_array ||= @params.to_s.split(',')
69
89
  end
70
90
 
71
91
  def habtm?
@@ -2,20 +2,28 @@
2
2
  <div class="faceted__facet__filter--tree">
3
3
  <div class="dd">
4
4
  <ol class="faceted__facet__filter--tree__level dd-list">
5
- <% facet.source.each do |object| %>
5
+ <% facet.tree_values.each_with_index do |object, index| %>
6
6
  <%
7
7
  identifier = object.send facet.find_by
8
- results_count = facet.results_with(identifier).count
9
- facet_url = facet.facets.path_for(facet, identifier)
10
- display_value = facet.display_method.call(object)
11
8
  selected = facet.value_selected?(identifier)
9
+ facet_url = selected ? '' : facet.facets.path_for(facet, identifier)
10
+ back = index == 0 && !selected && facet.has_params?
11
+ display_value = facet.display_method.call(object)
12
12
  parent_id = object.parent_id.nil? ? 0 : object.parent_id
13
13
  %>
14
- <li data-parent="<%= parent_id %>"
14
+ <% if index == 0 && selected %>
15
+ <li data-parent="0"
16
+ data-id="-1"
17
+ data-back="true"
18
+ data-selected="false"
19
+ data-url="<%= facet.facets.path_for(facet, '') %>">
20
+ </li>
21
+ <% end %>
22
+ <li data-parent="<%= back ? 0 : parent_id %>"
15
23
  data-id="<%= object.id %>"
24
+ data-back="<%= back %>"
16
25
  data-selected="<%= selected %>"
17
- data-url="<%= facet_url %>"
18
- data-count="<%= results_count %>">
26
+ data-url="<%= facet_url %>">
19
27
  <%= display_value %>
20
28
  </li>
21
29
  <% end %>
@@ -27,12 +35,10 @@
27
35
  <% facet.values.each do |value| %>
28
36
  <%
29
37
  identifier = value.send facet.find_by
30
- results_count = facet.results_with(identifier).count
31
38
  display_value = facet.display_method.call(value)
32
- next if results_count.zero?
33
39
  %>
34
40
  <li class="faceted__facet__filter__value<%= '--selected' if facet.value_selected?(identifier) %>">
35
- <%= link_to "#{display_value} (#{results_count})", facet.facets.path_for(facet, identifier) + anchor %>
41
+ <%= link_to display_value, facet.facets.path_for(facet, identifier) + anchor %>
36
42
  </li>
37
43
  <% end %>
38
44
  </ul>
@@ -1,3 +1,3 @@
1
1
  module FacetedSearch
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.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: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnaud Levy
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-26 00:00:00.000000000 Z
13
+ date: 2019-03-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -171,8 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0'
173
173
  requirements: []
174
- rubyforge_project:
175
- rubygems_version: 2.7.6
174
+ rubygems_version: 3.0.1
176
175
  signing_key:
177
176
  specification_version: 4
178
177
  summary: Faceted search with Active Record