faceted_search 1.1.8 → 2.0.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: 74e46e964703fb8393bd61a38c156c4e160f2ddbd1e080bebed62d7a81f570e6
4
- data.tar.gz: 18dd01b18a593d8cf64f25d844ccbdfbe64c8f4ee425af35de2e607d21caccb6
3
+ metadata.gz: 85b178d8ebe6784b6a4f5314795e448ad235122f70011f5e928a3d22cb6d8b71
4
+ data.tar.gz: cb478862460627e6ff37983212f1a4ef17af809bc64cba208edb432cad6b010d
5
5
  SHA512:
6
- metadata.gz: 9dceb31e9d014a8b7b31e551d13671602730a444c008d5ae4a9c5fe0bde4ae9d0e49d9a13c68b95b4c8c2b777cbe229c9e14a782e1bffba97d497bc3c1b30eac
7
- data.tar.gz: dafdf064c2c14d93675da97b6687af3bc33c9db04db52949ca57ea8e941c4dd953e111cb7de625403813a119f791c6b781702e9adf503f40e888121c241cf869
6
+ metadata.gz: aab3bff63a1cb917d6e5fd5439f8d0f02a61ff81005d4fc2f2f57c316c5d61f3df626b9d05e37100c10a54f0dec54fa1de871342f9c2e49c97ef926e7ac0cfb5
7
+ data.tar.gz: d290071a7050fefd94fb87cb71563827f8267427bad0f0086f86e331e5c222dd9e4deb86902ab75cefeaf1d1d05b3ca9ea6f26097e5e7a010ba138ea1eee14ef
@@ -1,25 +1,16 @@
1
1
  module FacetedSearch
2
2
  class Facets::Default
3
- attr_reader :name, :params, :facets, :find_by
3
+ attr_reader :name, :params, :facets
4
4
 
5
- def initialize( name:,
6
- params:,
7
- facets:,
8
- find_by: nil,
9
- source: nil,
10
- habtm: false,
11
- title: nil)
5
+ def initialize(name, params, facets, options)
12
6
  @name = name
13
- @title = title
14
7
  @params = params
15
8
  @facets = facets
16
- @find_by = find_by
17
- @source = source
18
- @habtm = habtm
9
+ @options = options
19
10
  end
20
11
 
21
12
  def title
22
- @title ||= name.to_s.humanize.titleize
13
+ @options[:title] || name.to_s.humanize.titleize
23
14
  end
24
15
 
25
16
  def kind
@@ -37,7 +28,7 @@ module FacetedSearch
37
28
 
38
29
  def path(custom_params = @params)
39
30
  return '' if custom_params.blank?
40
- "&facets[#{@name}]=#{custom_params}"
31
+ "&facets[#{name}]=#{custom_params}"
41
32
  end
42
33
 
43
34
  def to_s
@@ -1,35 +1,35 @@
1
1
  module FacetedSearch
2
2
  class Facets::Filter < Facets::Default
3
- attr_reader :display_method
4
3
 
5
- def initialize( name:,
6
- params:,
7
- facets:,
8
- display_method:,
9
- find_by: nil,
10
- source: nil,
11
- habtm: false,
12
- title: nil)
13
- super(name: name,
14
- title: title,
15
- facets: facets,
16
- params: params,
17
- find_by: find_by,
18
- source: source,
19
- habtm: habtm,
20
- title: title)
21
- @display_method = display_method
4
+ def tree?
5
+ @options[:tree]
22
6
  end
23
7
 
24
- def path_for(value)
25
- value = value.to_s
26
- custom_params = params_array.dup
27
- selected?(value) ? custom_params.delete(value)
28
- : custom_params.push(value)
29
- path(custom_params.join(','))
8
+ def display_method
9
+ @options[:display_method] ||= Proc.new { |object| object.to_s }
30
10
  end
31
11
 
32
- def selected?(value)
12
+ # Adds a scope corresponding to this facet
13
+ # to the scope sent as an argument
14
+ # and return the modified scope
15
+ def add_scope(scope)
16
+ return scope if params_array.blank?
17
+ habtm? ? scope.joins(name).where(name => { find_by => params_array })
18
+ : scope.where(name => params_array)
19
+ end
20
+
21
+ def find_by
22
+ @options[:find_by] || :id
23
+ end
24
+
25
+ # Show all values that have corresponding results.
26
+ # This is a regular SQL inner join.
27
+ def values
28
+ joined_table = facets.model_table_name.to_sym
29
+ source.all.joins(joined_table).where(joined_table => { id: facets.model }).distinct
30
+ end
31
+
32
+ def value_selected?(value)
33
33
  value.to_s.in? params_array
34
34
  end
35
35
 
@@ -42,38 +42,34 @@ module FacetedSearch
42
42
  scope
43
43
  end
44
44
 
45
- def params_array
46
- @params_array ||= @params.to_s.split(',')
45
+ def path_for(value)
46
+ value = value.to_s
47
+ custom_params = params_array.dup
48
+ value_selected?(value) ? custom_params.delete(value)
49
+ : custom_params.push(value)
50
+ path(custom_params.join(','))
47
51
  end
48
52
 
49
- # Adds a scope corresponding to this facet
50
- # to the scope sent as an argument
51
- # and return the modified scope
52
- def add_scope(scope)
53
- return scope if params_array.blank?
54
- @habtm ? scope.joins(name).where(name => { find_by => params_array })
55
- : scope.where(name => params_array)
53
+ protected
54
+
55
+ def params_array
56
+ @params_array ||= @params.to_s.split(',')
56
57
  end
57
58
 
58
59
  # Adds a scope corresponding to this facet
59
60
  # to the scope sent as an argument with specific value
60
61
  # and return the modified scope
61
62
  def add_scope_with_value(scope, value)
62
- @habtm ? scope.joins(name).where(name => { find_by => value })
63
+ habtm? ? scope.joins(name).where(name => { find_by => value })
63
64
  : scope.where(name => value)
64
65
  end
65
66
 
66
- # Show all values that have corresponding results.
67
- # This is a regular SQL inner join.
68
- def values
69
- joined_table = @facets.model_table_name.to_sym
70
- source.all.joins(joined_table).where(joined_table => { id: @facets.model }).distinct
67
+ def source
68
+ @options[:source] || name.to_s.singularize.titleize.constantize
71
69
  end
72
70
 
73
- protected
74
-
75
- def source
76
- @source ||= @name.to_s.singularize.titleize.constantize
71
+ def habtm?
72
+ @options[:habtm]
77
73
  end
78
74
  end
79
75
  end
@@ -1,29 +1,13 @@
1
1
  module FacetedSearch
2
2
  class Facets::Search < Facets::Default
3
- attr_reader :placeholder
4
3
 
5
- def initialize( name:,
6
- params:,
7
- facets:,
8
- placeholder: nil,
9
- find_by: nil,
10
- source: nil,
11
- habtm: false,
12
- title: nil)
13
- super(name: name,
14
- title: title,
15
- facets: facets,
16
- params: params,
17
- find_by: find_by,
18
- source: source,
19
- habtm: habtm,
20
- title: title)
21
- @placeholder = placeholder
4
+ def placeholder
5
+ @options[:placeholder]
22
6
  end
23
7
 
24
8
  def add_scope(scope)
25
- return scope if @params.blank?
26
- scope.where("#{@facets.model_table_name}.#{name} ILIKE ?", "%#{@params}%")
9
+ return scope if params.blank?
10
+ scope.where("#{facets.model_table_name}.#{name} ILIKE ?", "%#{params}%")
27
11
  end
28
12
  end
29
13
  end
@@ -41,23 +41,12 @@ module FacetedSearch
41
41
 
42
42
  protected
43
43
 
44
- def search(value, placeholder: nil, title: nil)
45
- add(Search.new( name: value,
46
- params: params_for(value),
47
- placeholder: placeholder,
48
- facets: self,
49
- title: title))
44
+ def search(value, options = {})
45
+ add(Search.new(value, params_for(value), self, options))
50
46
  end
51
47
 
52
- def filter(value, find_by: :id, source: nil, habtm: nil, title: nil, display_method: Proc.new { |object| object.to_s })
53
- add(Filter.new( name: value,
54
- params: params_for(value),
55
- facets: self,
56
- find_by: find_by,
57
- source: source,
58
- habtm: habtm,
59
- title: title,
60
- display_method: display_method))
48
+ def filter(value, options = {})
49
+ add(Filter.new(value, params_for(value), self, options))
61
50
  end
62
51
 
63
52
  def params_for(value)
@@ -1,13 +1,12 @@
1
- <ul class="faceted__facet__filter list-unstyled">
1
+ <ul class="faceted__facet__filter<%= '--tree' if facet.tree? %> list-unstyled">
2
2
  <% facet.values.each do |value| %>
3
3
  <%
4
4
  identifier = value.send facet.find_by
5
- selected = facet.selected? identifier
6
5
  results_count = facet.results_with(identifier).count
7
6
  display_value = facet.display_method.call(value)
8
7
  next if results_count.zero?
9
8
  %>
10
- <li class="faceted__facet__filter__value<%= '--selected' if selected %>">
9
+ <li class="faceted__facet__filter__value<%= '--selected' if facet.value_selected?(identifier) %>">
11
10
  <%= link_to "#{display_value} (#{results_count})", facet.facets.path_for(facet, identifier) %>
12
11
  </li>
13
12
  <% end %>
@@ -1,3 +1,3 @@
1
1
  module FacetedSearch
2
- VERSION = '1.1.8'
2
+ VERSION = '2.0.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: 1.1.8
4
+ version: 2.0.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-22 00:00:00.000000000 Z
13
+ date: 2019-03-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -139,7 +139,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  requirements: []
142
- rubygems_version: 3.0.1
142
+ rubyforge_project:
143
+ rubygems_version: 2.7.6
143
144
  signing_key:
144
145
  specification_version: 4
145
146
  summary: Faceted search with Active Record