forty_facets 0.0.6 → 0.0.7

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
2
  SHA1:
3
- metadata.gz: f4d7a0229978d1d9133b0082e3924554cd5a0cbf
4
- data.tar.gz: b054dd26585014ffe9aa8a7851e9d30c4e43d130
3
+ metadata.gz: 7d9c32f0c9730c55259649e32b7b38909d24da00
4
+ data.tar.gz: 0b38e2588247108aa332113e2a50907bf89ca8d1
5
5
  SHA512:
6
- metadata.gz: 4936450a29cb55d55562e94894db107defda556f29dd3690d3421f893f814a416f76dafeb84d09148375bf405e613f1ec76d104ed248b18bc0aa647f91ea4a56
7
- data.tar.gz: f331f6c08b3aab3e1c51e633ed628633dea49ffdfa839dc777e79dc818a7dda41b9cc0f8ddc8a3daef6932705c844bfc3e9b9be8f9603a0d8194a6753eb64a2a
6
+ metadata.gz: 1260c88aba045ed9c89e1f8209e154b45cbef190f67183e13625705de727dc6bd54c3057075b5d1f4ab31dc93d540475a904cf14868dec24cce1416628e10909
7
+ data.tar.gz: de80f703356346a84b8a2c3c776e8a23e70935ad31512e3ad7a94de9bdcc92961b8e78f85937a3ad7d05aa4c9235ceecd833bbbf52bd35a5b801ecf8d10aba95
data/README.md CHANGED
@@ -99,6 +99,25 @@ Use the search object to display further narrowing options to the user
99
99
  = link_to genre.name, filter.add(genre).path
100
100
  %span.count= "(#{facet_value.count})"
101
101
  ```
102
+ ## API
103
+
104
+ ### Base class
105
+
106
+ To create a custom search subclass `FortyFacets::FacetSearch`.
107
+
108
+ ```ruby
109
+ class MySearch
110
+ model 'MyActiveRecordModel' # replace this with an class name from your models folder
111
+ end
112
+ ```
113
+
114
+ ### Available declaration methods
115
+ | keyword | options | |
116
+ |---------|---------------|-------------------------------------------------------------------------------------------------------------------------|
117
+ | text | prefix:true | creates a filter to limit search result to entities containing the filter value in the given field |
118
+ | facet | | creates a facetted filter on the specified model attribute (attribute or belongs_to) |
119
+ | range | | creates a range filter (param format 'FROM - TO') limiting result to entities with values in that range |
120
+ | orders | | takes a hash mapping a label to an argument that the active record `order` method can be called with to sort the result |
102
121
 
103
122
  ## FAQ
104
123
 
@@ -32,11 +32,11 @@ module FortyFacets
32
32
  end
33
33
 
34
34
  def facet(model_field, opts = {})
35
- definitions << BelongsToFilterDefinition.new(self, model_field, opts)
36
- end
37
-
38
- def facet_attr(model_field, opts = {})
39
- definitions << AttributeFilterDefinition.new(self, model_field, opts)
35
+ if self.root_scope.reflect_on_association(model_field)
36
+ definitions << BelongsToFilterDefinition.new(self, model_field, opts)
37
+ else
38
+ definitions << AttributeFilterDefinition.new(self, model_field, opts)
39
+ end
40
40
  end
41
41
 
42
42
  def orders(name_and_order_options)
@@ -7,10 +7,6 @@ module FortyFacets
7
7
  filter_definition.options[:name] || filter_definition.model_field
8
8
  end
9
9
 
10
- def values
11
- @values ||= Array.wrap(value).sort.uniq
12
- end
13
-
14
10
  def empty?
15
11
  value.nil? || value == '' || value == []
16
12
  end
@@ -24,5 +20,29 @@ module FortyFacets
24
20
  search_instance.class.new_unwrapped(new_params)
25
21
  end
26
22
  end
23
+
24
+ # Base class for filter with multiple values and grouped facet values
25
+ class FacetFilter < Filter
26
+ def values
27
+ @values ||= Array.wrap(value).sort.uniq
28
+ end
29
+
30
+ protected
31
+
32
+ def order_facet!(facet)
33
+ order_accessor = filter_definition.options[:order]
34
+ if order_accessor
35
+ if order_accessor.is_a?(Proc)
36
+ facet.sort_by!{|facet_value| order_accessor.call(facet_value.entity) }
37
+ else
38
+ facet.sort_by!{|facet_value| facet_value.entity.send(order_accessor) }
39
+ end
40
+ else
41
+ facet.sort_by!{|facet_value| -facet_value.count }
42
+ end
43
+ facet
44
+ end
45
+
46
+ end
27
47
  end
28
48
 
@@ -1,6 +1,6 @@
1
1
  module FortyFacets
2
2
  class AttributeFilterDefinition < FilterDefinition
3
- class AttributeFilter < Filter
3
+ class AttributeFilter < FacetFilter
4
4
  def selected
5
5
  entity = search_instance.class.root_class
6
6
  column = entity.columns_hash[filter_definition.model_field.to_s]
@@ -15,10 +15,12 @@ module FortyFacets
15
15
  def facet
16
16
  my_column = filter_definition.model_field
17
17
  counts = without.result.reorder('').select("#{my_column} AS facet_value, count(#{my_column}) as occurrences").group(my_column)
18
- counts.map do |c|
18
+ facet = counts.map do |c|
19
19
  is_selected = selected.include?(c.facet_value)
20
20
  FacetValue.new(c.facet_value, c.occurrences, is_selected)
21
21
  end
22
+
23
+ order_facet!(facet)
22
24
  end
23
25
 
24
26
  def remove(value)
@@ -1,6 +1,6 @@
1
1
  module FortyFacets
2
2
  class BelongsToFilterDefinition < FilterDefinition
3
- class FacetFilter < Filter
3
+ class BelonsToFilter < FacetFilter
4
4
  def association
5
5
  filter_definition.search.root_class.reflect_on_association(filter_definition.model_field)
6
6
  end
@@ -48,21 +48,10 @@ module FortyFacets
48
48
  search_instance.class.new_unwrapped(new_params)
49
49
  end
50
50
 
51
- private
52
-
53
- def order_facet!(facet)
54
- order_accessor = filter_definition.options[:order]
55
- if order_accessor
56
- facet.sort_by!{|facet_value| facet_value.entity.send(order_accessor) }
57
- else
58
- facet.sort_by!{|facet_value| -facet_value.count }
59
- end
60
- facet
61
- end
62
51
  end
63
52
 
64
53
  def build_filter(search_instance, param_value)
65
- FacetFilter.new(self, search_instance, param_value)
54
+ BelonsToFilter.new(self, search_instance, param_value)
66
55
  end
67
56
 
68
57
  end
@@ -1,3 +1,3 @@
1
1
  module FortyFacets
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/test/smoke_test.rb CHANGED
@@ -39,7 +39,7 @@ class MovieSearch < FortyFacets::FacetSearch
39
39
 
40
40
  text :title, name: 'Title'
41
41
  facet :studio, name: 'Studio'
42
- facet_attr :year
42
+ facet :year, order: Proc.new {|year| -year}
43
43
  range :price, name: 'Price'
44
44
  end
45
45
 
@@ -116,4 +116,10 @@ class SmokeTest < Minitest::Test
116
116
  assert_equal movies_with_studio.size, first_facet_value.count
117
117
  end
118
118
 
119
+ def test_sort_by_proc
120
+ blank_search = MovieSearch.new
121
+ facet_entities = blank_search.filter(:year).facet.map(&:entity)
122
+ assert_equal Movie.all.map(&:year).sort.uniq.reverse, facet_entities
123
+ end
124
+
119
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forty_facets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Axel Tetzlaff