forty_facets 0.1.8 → 0.1.8.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 914d7f9d2fa6d7db93c59f23553498fac3729beb
4
- data.tar.gz: b135c7d3f2792503872fdbc8df95ae0a6010b9ea
2
+ SHA256:
3
+ metadata.gz: ce24e462cc7ee4e339d052120807239c0e4406faa14c2be784ae41548bcbc18a
4
+ data.tar.gz: ab5d0fa8bf95ca88d5071ae74084bf56c3baae9c4a9af40dbd10fd5bf6f29132
5
5
  SHA512:
6
- metadata.gz: 2336fb3607d0aa4d38551451f20b10e38b18936b1f7c0854c0419694d49f5dd0d59f875b58c6949bf51ca67d9c876cf24d67665577627ebb0fa17e7483906b54
7
- data.tar.gz: a28eebb1677fc78b32aab48b859dd16ac316be6dda8f7f3d4b770f9fc7cf3c56b59542e9c7af0aa859aee101e963628275e1ab7c103a63037e3666e2dcd29aa8
6
+ metadata.gz: 3e49b4403a1be6a802f1f9e492ca8753a64ee0d1bc5f39b1c254880b3f75baf7e52448cc81e7f530c37e59a6a5c9e5067fcb537bbe9a7b99fd0f7753c96c90b3
7
+ data.tar.gz: 585d41f4b4b55c09c8af4a51ac755951174c67e528187910e025d001fa147d2e3dd3752c1e2856d04f66c50ac6c09a7fba888363d2d5b2f456003fb0fb3265db
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4.0
1
+ 2.5.1
@@ -117,9 +117,14 @@ module FortyFacets
117
117
 
118
118
  def result(skip_ordering: false)
119
119
  query = @filters.reject(&:empty?).inject(root) do |previous, filter|
120
+ # p 'PREVIOS'
121
+ # p previous
122
+ # p 'FILTER'
123
+ # p filter
124
+ # p '..........'
120
125
  filter.build_scope.call(previous)
121
126
  end
122
-
127
+
123
128
  if order && !skip_ordering
124
129
  query = order.apply(query)
125
130
  else
@@ -3,7 +3,6 @@ module FortyFacets
3
3
  # type of filter. Most FilterDefinitions will have their own Filter subclass
4
4
  # to control values for display and rendering to request parameters.
5
5
  Filter = Struct.new(:definition, :search_instance, :value) do
6
-
7
6
  FacetValue = Struct.new(:entity, :count, :selected)
8
7
 
9
8
  def name
@@ -86,7 +86,6 @@ module FortyFacets
86
86
  old_values << value.to_s
87
87
  search_instance.class.new_unwrapped(new_params, search_instance.root)
88
88
  end
89
-
90
89
  end
91
90
 
92
91
  class BelongsToFilter < AssociationFacetFilter
@@ -2,23 +2,35 @@ module FortyFacets
2
2
  class ScopeFilterDefinition < FilterDefinition
3
3
  class ScopeFilter < Filter
4
4
  def active?
5
- value == '1'
5
+ definition.options[:pass_value] ? value.present? : '1'
6
+ end
7
+
8
+ def selected
9
+ [value]
6
10
  end
7
11
 
8
12
  def build_scope
9
- return Proc.new { |base| base } unless active?
10
- Proc.new { |base| base.send(definition.path.first) }
13
+ return proc { |base| base } unless active?
14
+
15
+ proc { |base|
16
+ if definition.options[:pass_value]
17
+ base.send(definition.path.first, value)
18
+ else
19
+ base.send(definition.path.first)
20
+ end
21
+ }
11
22
  end
12
23
 
13
- def remove
24
+ # added value to standardize the API even though it's not used
25
+ def remove(value = nil)
14
26
  new_params = search_instance.params || {}
15
27
  new_params.delete(definition.request_param)
16
28
  search_instance.class.new_unwrapped(new_params, search_instance.root)
17
29
  end
18
30
 
19
- def add
31
+ def add(value = nil)
20
32
  new_params = search_instance.params || {}
21
- new_params[definition.request_param] = '1'
33
+ new_params[definition.request_param] = value
22
34
  search_instance.class.new_unwrapped(new_params, search_instance.root)
23
35
  end
24
36
  end
@@ -6,6 +6,7 @@ module FortyFacets
6
6
  @search = search
7
7
  @queries = queries
8
8
  @path = Array(opts[:path]) if opts[:path].present?
9
+ @joins = Array(opts[:joins]) if opts[:joins].present?
9
10
  @path ||= @queries.keys
10
11
  @options = opts
11
12
  end
@@ -29,7 +30,7 @@ module FortyFacets
29
30
  Proc.new do |base|
30
31
  # intersection of values and definition queries
31
32
  base.where(selected_queries.values.map do |query|
32
- "(#{query})"
33
+ "(#{query})"
33
34
  end.join(" OR "))
34
35
  end
35
36
  end
@@ -55,13 +56,14 @@ module FortyFacets
55
56
 
56
57
  def facet
57
58
  query = definition.queries.map do |key, sql_query|
58
- "(#{sql_query}) as #{key}"
59
+ "(#{sql_query}) as #{key}"
59
60
  end.join(", ")
60
61
  query += ", count(*) as occurrences"
61
-
62
+
62
63
  counts = without.result.reorder("")
63
64
  .select(query)
64
65
  .group(definition.queries.keys)
66
+ counts = counts.joins(definition.joins) if definition.joins
65
67
  counts.includes_values = []
66
68
 
67
69
  result = {}
@@ -1,3 +1,3 @@
1
1
  module FortyFacets
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.8.9"
3
3
  end
data/test/fixtures.rb CHANGED
@@ -89,6 +89,7 @@ class Movie < ActiveRecord::Base
89
89
 
90
90
  scope :classics, -> { where("year <= ?", 1980) }
91
91
  scope :non_classics, -> { where("year > ?", 1980) }
92
+ scope :year_lte, -> (year) { where("year > ?", year) }
92
93
  end
93
94
 
94
95
  LOREM = %w{Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren}
data/test/smoke_test.rb CHANGED
@@ -23,17 +23,26 @@ class MovieSearch < FortyFacets::FacetSearch
23
23
  facet [:studio, :country], name: 'Country'
24
24
  facet [:studio, :status], name: 'Studio status'
25
25
  facet [:studio, :producers], name: 'Producers'
26
+ sql_facet({ uschis: "studios.name = 'Uschi'", non_uschis: "studios.name != 'USCHI'" },
27
+ { name: "Uschis", path: [:studio, :uschis], joins: [:studio] })
26
28
  sql_facet({ classic: "year <= 1980", non_classic: "year > 1980" },
27
29
  { name: "Classic", path: :classic })
28
30
  sql_facet({ classic: "year <= 1980", non_classic: "year > 1980" },
29
31
  { name: "Classic" })
30
32
  text [:studio, :description], name: 'Studio Description'
31
- scope :classics, 'Name classics'
33
+ scope :classics, name: 'Name classics'
34
+ scope :year_lte, name: 'Year less than or equal', pass_value: true
32
35
  custom :needs_complex_filtering
33
36
  end
34
37
 
35
38
  class SmokeTest < Minitest::Test
36
39
 
40
+ def test_sql_facet_with_belongs_to
41
+ search = MovieSearch.new({'studio-uschis' => {}})
42
+ assert_equal Movie.count, search.result.size
43
+ assert_equal search.filter([:studio, :uschis]).facet, [FortyFacets::FacetValue.new(:uschis, 0, false), FortyFacets::FacetValue.new(:non_uschis, 40, false)]
44
+ end
45
+
37
46
  def test_it_finds_all_movies
38
47
  search = MovieSearch.new({})
39
48
  assert_equal Movie.all.size, search.result.size
@@ -270,10 +279,15 @@ class SmokeTest < Minitest::Test
270
279
  end
271
280
 
272
281
  def test_scope_filter
273
- search_with_scope = MovieSearch.new().filter(:classics).add
282
+ search_with_scope = MovieSearch.new().filter(:classics).add('1')
274
283
  assert search_with_scope.result.count < Movie.count, 'Activating the scope should yield a smaller set of movies'
275
284
  end
276
285
 
286
+ def test_scope_filter_with_params
287
+ search_with_scope = MovieSearch.new().filter(:year_lte).add(1980)
288
+ assert search_with_scope.result.count < Movie.count, 'Activating the scope with filter should yield a smaller set of movies'
289
+ end
290
+
277
291
  def test_custom_filter
278
292
  search = MovieSearch.new
279
293
  new_search = search.filter(:needs_complex_filtering).set('foo')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forty_facets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Axel Tetzlaff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -162,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  version: '0'
163
163
  requirements: []
164
164
  rubyforge_project:
165
- rubygems_version: 2.6.8
165
+ rubygems_version: 2.7.6
166
166
  signing_key:
167
167
  specification_version: 4
168
168
  summary: Library for building facet searches for active_record models