forty_facets 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/forty_facets/facet_search.rb +5 -0
- data/lib/forty_facets/filter/scope_filter_definition.rb +32 -0
- data/lib/forty_facets/version.rb +1 -1
- data/lib/forty_facets.rb +1 -0
- data/test/smoke_test.rb +6 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c4e5f1187a806a2d29a592deb215db699c940ca
|
4
|
+
data.tar.gz: f744711ed252d741a508583565c033531d70f5f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97da5234fce9ebb01fff1c1f5994ca739724a62b61b343de9e58dd7eacae1e4eb543743b9e338862f098e3c0abbac7aed716848bcc71b16f746bc43aff0675e3
|
7
|
+
data.tar.gz: 0ade8fdfb9ee93d93f76f642412f90f60b2dd2959f5f0a092efaf19031b8ed45daa0ee91993ff9854c5630f621e0d5b36711108ebb7ae2c859265cc07f8e722f
|
data/README.md
CHANGED
@@ -45,6 +45,8 @@ If you have Movies with a textual title, categotized by genre, studio and year w
|
|
45
45
|
belongs_to :year
|
46
46
|
belongs_to :studio
|
47
47
|
has_and_belongs_to_many :genres
|
48
|
+
|
49
|
+
scope :classics, -> { where("year <= ?", 1980) }
|
48
50
|
end
|
49
51
|
|
50
52
|
You can then declare the structure of your search like so:
|
@@ -55,6 +57,7 @@ class HomeController < ApplicationController
|
|
55
57
|
class MovieSearch < FortyFacets::FacetSearch
|
56
58
|
model 'Movie' # which model to search for
|
57
59
|
text :title # filter by a generic string entered by the user
|
60
|
+
scope :classics # only return movies which are in the scope 'classics'
|
58
61
|
range :price, name: 'Price' # filter by ranges for decimal fields
|
59
62
|
facet :year, name: 'Releaseyear', order: :year # additionally order values in the year field
|
60
63
|
facet :studio, name: 'Studio', order: :name
|
@@ -117,6 +120,7 @@ end
|
|
117
120
|
| keyword | options | |
|
118
121
|
|---------|---------------|-------------------------------------------------------------------------------------------------------------------------|
|
119
122
|
| text | prefix:true | creates a filter to limit search result to entities containing the filter value in the given field |
|
123
|
+
| scope | | creates a filter to limit search result to entities matching the scope with the given name |
|
120
124
|
| facet | | creates a facetted filter on the specified model attribute (attribute or belongs_to) |
|
121
125
|
| range | | creates a range filter (param format 'FROM - TO') limiting result to entities with values in that range |
|
122
126
|
| orders | | takes a hash mapping a label to an argument that the active record `order` method can be called with to sort the result |
|
@@ -5,6 +5,7 @@ module FortyFacets
|
|
5
5
|
# model 'Movie'
|
6
6
|
#
|
7
7
|
# text :title
|
8
|
+
# scope :someScope, name: 'Only very special'
|
8
9
|
# range :price
|
9
10
|
# facet :genre, name: 'Genre'
|
10
11
|
# facet :year, name: 'Releaseyear', order: :year
|
@@ -32,6 +33,10 @@ module FortyFacets
|
|
32
33
|
definitions << TextFilterDefinition.new(self, path, opts)
|
33
34
|
end
|
34
35
|
|
36
|
+
def scope(path, opts = {})
|
37
|
+
definitions << ScopeFilterDefinition.new(self, path, opts)
|
38
|
+
end
|
39
|
+
|
35
40
|
def range(path, opts = {})
|
36
41
|
definitions << RangeFilterDefinition.new(self, path, opts)
|
37
42
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FortyFacets
|
2
|
+
class ScopeFilterDefinition < FilterDefinition
|
3
|
+
class ScopeFilter < Filter
|
4
|
+
def active?
|
5
|
+
value == '1'
|
6
|
+
end
|
7
|
+
|
8
|
+
def build_scope
|
9
|
+
return Proc.new { |base| base } unless active?
|
10
|
+
Proc.new { |base| base.send(definition.path.first) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def remove
|
14
|
+
new_params = search_instance.params || {}
|
15
|
+
new_params.delete(definition.request_param)
|
16
|
+
search_instance.class.new_unwrapped(new_params, search_instance.root)
|
17
|
+
end
|
18
|
+
|
19
|
+
def add
|
20
|
+
new_params = search_instance.params || {}
|
21
|
+
new_params[definition.request_param] = '1'
|
22
|
+
search_instance.class.new_unwrapped(new_params, search_instance.root)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_filter(search_instance, value)
|
27
|
+
ScopeFilter.new(self, search_instance, value)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
data/lib/forty_facets/version.rb
CHANGED
data/lib/forty_facets.rb
CHANGED
@@ -9,6 +9,7 @@ require "forty_facets/filter"
|
|
9
9
|
require "forty_facets/filter_definition"
|
10
10
|
require "forty_facets/filter/range_filter_definition"
|
11
11
|
require "forty_facets/filter/text_filter_definition"
|
12
|
+
require "forty_facets/filter/scope_filter_definition"
|
12
13
|
require "forty_facets/filter/facet_filter_definition"
|
13
14
|
require "forty_facets/filter/sql_facet_filter_definition"
|
14
15
|
require "forty_facets/facet_search"
|
data/test/smoke_test.rb
CHANGED
@@ -28,6 +28,7 @@ class MovieSearch < FortyFacets::FacetSearch
|
|
28
28
|
sql_facet({ classic: "year <= 1980", non_classic: "year > 1980" },
|
29
29
|
{ name: "Classic" })
|
30
30
|
text [:studio, :description], name: 'Studio Description'
|
31
|
+
scope :classics, 'Name classics'
|
31
32
|
end
|
32
33
|
|
33
34
|
class SmokeTest < Minitest::Test
|
@@ -267,4 +268,9 @@ class SmokeTest < Minitest::Test
|
|
267
268
|
search.filter(:studio).facet.reject(&:selected).to_a
|
268
269
|
end
|
269
270
|
|
271
|
+
def test_scope_filter
|
272
|
+
search_with_scope = MovieSearch.new().filter(:classics).add
|
273
|
+
assert search_with_scope.result.count < Movie.count, 'Activating the scope should yield a smaller set of movies'
|
274
|
+
end
|
275
|
+
|
270
276
|
end
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Axel Tetzlaff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- lib/forty_facets/filter.rb
|
118
118
|
- lib/forty_facets/filter/facet_filter_definition.rb
|
119
119
|
- lib/forty_facets/filter/range_filter_definition.rb
|
120
|
+
- lib/forty_facets/filter/scope_filter_definition.rb
|
120
121
|
- lib/forty_facets/filter/sql_facet_filter_definition.rb
|
121
122
|
- lib/forty_facets/filter/text_filter_definition.rb
|
122
123
|
- lib/forty_facets/filter_definition.rb
|