faceted_search 1.0.2 → 1.0.3
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77832a861939a5e50823932c80cf4cf46f4d5217b53407793880b1990b08bccb
|
4
|
+
data.tar.gz: 740e994a3457116649af590d2659bd098fba3a6a9e7c0d1ce9b5879c1401f0fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa376c253830824ad91e281d4c0c5129b8a061f7e98a94b6adb0bde938d15abf0b8bbb61b4aff4b9bb86b34456b9a14f8ce89e5d527b5b86e50ddc6c741899f0
|
7
|
+
data.tar.gz: 29a5eaf6e27593357df61731de56e50ab20c249bf4c731dec245e05422c5a77402ec0df6c5ed6bda7bdca495454d1a3531f194e3ae5d38f989d3eb9b9b375aba
|
data/README.md
CHANGED
@@ -21,6 +21,46 @@ Or install it yourself as:
|
|
21
21
|
$ gem install faceted_search
|
22
22
|
```
|
23
23
|
|
24
|
+
## Getting started
|
25
|
+
|
26
|
+
Create a model defining your facets:
|
27
|
+
|
28
|
+
class Item::Facets < FacetedSearch::Facets
|
29
|
+
def define
|
30
|
+
set_model Item.all
|
31
|
+
search :title
|
32
|
+
filter :products, find_by: :title
|
33
|
+
filter :kinds
|
34
|
+
filter :categories
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
In your controller, use it:
|
39
|
+
|
40
|
+
@facets = Item::Facets.new params[:facets]
|
41
|
+
@items = @facets.results.order(:title).page params[:page]
|
42
|
+
|
43
|
+
In your view, do something like that (with bootstrap):
|
44
|
+
|
45
|
+
<div class="row">
|
46
|
+
<div class="col-md-3">
|
47
|
+
<%= render 'faceted_search/facets', facets: @facets %>
|
48
|
+
</div>
|
49
|
+
<div class="col-md-9">
|
50
|
+
<div class="row">
|
51
|
+
<% @items.each do |item| %>
|
52
|
+
<div class="col-md-4">
|
53
|
+
<h2><%= item %></h2>
|
54
|
+
<p>Products: <%= item.products.join(', ') %></p>
|
55
|
+
<p>Categories: <%= item.categories.join(', ') %></p>
|
56
|
+
<p>Kinds: <%= item.kinds.join(', ') %></p>
|
57
|
+
</div>
|
58
|
+
<% end %>
|
59
|
+
</div>
|
60
|
+
</div>
|
61
|
+
</div>
|
62
|
+
|
63
|
+
|
24
64
|
## Contributing
|
25
65
|
Contribution directions go here.
|
26
66
|
|
@@ -56,11 +56,13 @@ module FacetedSearch
|
|
56
56
|
facets: self))
|
57
57
|
end
|
58
58
|
|
59
|
-
def filter(value, find_by: :id)
|
59
|
+
def filter(value, find_by: :id, source: nil, habtm: nil)
|
60
60
|
add(Filter.new( name: value,
|
61
61
|
params: params_for(value),
|
62
62
|
facets: self,
|
63
|
-
find_by: find_by
|
63
|
+
find_by: find_by,
|
64
|
+
source: source,
|
65
|
+
habtm: habtm))
|
64
66
|
end
|
65
67
|
|
66
68
|
def params_for(value)
|
@@ -2,11 +2,13 @@ module FacetedSearch
|
|
2
2
|
class Facets::Default
|
3
3
|
attr_reader :name, :params, :facets, :find_by
|
4
4
|
|
5
|
-
def initialize(name:, params:, facets:, find_by: nil)
|
5
|
+
def initialize(name:, params:, facets:, find_by: nil, source: nil, habtm: false)
|
6
6
|
@name = name
|
7
7
|
@params = params
|
8
8
|
@facets = facets
|
9
9
|
@find_by = find_by
|
10
|
+
@source = source
|
11
|
+
@habtm = habtm
|
10
12
|
end
|
11
13
|
|
12
14
|
def title
|
@@ -18,15 +18,21 @@ module FacetedSearch
|
|
18
18
|
|
19
19
|
def add_scope(scope)
|
20
20
|
return scope if params_array.blank?
|
21
|
-
|
21
|
+
if @habtm
|
22
|
+
scope.joins(name).where(name => { find_by => params_array })
|
23
|
+
else
|
24
|
+
scope.where(name => params_array)
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
def values
|
25
29
|
source.all
|
26
30
|
end
|
27
31
|
|
32
|
+
protected
|
33
|
+
|
28
34
|
def source
|
29
|
-
@name.to_s.singularize.titleize.constantize
|
35
|
+
@source ||= @name.to_s.singularize.titleize.constantize
|
30
36
|
end
|
31
37
|
end
|
32
38
|
end
|