Commit cebb76c40b27d5d4d58d2300a2f808ac9697ab1c
Committed by
Macartur Sousa
1 parent
174fea9a
Exists in
fix_sign_up_form
Elasticsearch: Fixes categories
* Adding method categories_data to return a json with categories list * Adds category params to controller * Adds dynamic categories via javascript url * Merges test changes from filter branch * Fixes plugin javascript to use & in regex and uses categories Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Macartur de Sousa <macartur.sc@gmail.com>
Showing
9 changed files
with
117 additions
and
17 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 | 1 | require_relative '../helpers/elasticsearch_helper' |
2 | +require_relative '../helpers/elasticsearch_plugin_helper' | |
2 | 3 | |
3 | 4 | class ElasticsearchPluginController < ApplicationController |
4 | 5 | no_design_blocks |
5 | 6 | include ElasticsearchHelper |
7 | + helper ElasticsearchPluginHelper | |
6 | 8 | |
7 | 9 | def index |
8 | 10 | search() |
... | ... | @@ -12,6 +14,7 @@ class ElasticsearchPluginController < ApplicationController |
12 | 14 | def search |
13 | 15 | define_searchable_types |
14 | 16 | define_sort_types |
17 | + define_categories | |
15 | 18 | define_results |
16 | 19 | end |
17 | 20 | |
... | ... | @@ -23,7 +26,7 @@ class ElasticsearchPluginController < ApplicationController |
23 | 26 | |
24 | 27 | def define_searchable_types |
25 | 28 | @searchable_types = searchable_types |
26 | - @selected_type = (params[:selected_type]|| :all ).to_sym | |
29 | + @selected_type = (params[:selected_type] || :all).to_sym | |
27 | 30 | end |
28 | 31 | |
29 | 32 | def define_sort_types |
... | ... | @@ -31,4 +34,9 @@ class ElasticsearchPluginController < ApplicationController |
31 | 34 | @selected_sort = (params[:filter] || :relevance).to_sym |
32 | 35 | end |
33 | 36 | |
37 | + def define_categories | |
38 | + @categories = Category.where(parent: nil) | |
39 | + @selected_categories = (params[:categories] || "").split(",") | |
40 | + end | |
41 | + | |
34 | 42 | end | ... | ... |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
... | ... | @@ -37,7 +37,7 @@ module ElasticsearchHelper |
37 | 37 | def search_from_all_models |
38 | 38 | begin |
39 | 39 | filter = (params[:filter] || "" ).to_sym |
40 | - query = get_query params[:query], sort_by: get_sort_by(filter) | |
40 | + query = get_query params[:query], sort_by: get_sort_by(filter), categories: params[:categories] | |
41 | 41 | Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records |
42 | 42 | rescue |
43 | 43 | [] |
... | ... | @@ -48,7 +48,7 @@ module ElasticsearchHelper |
48 | 48 | begin |
49 | 49 | klass = model.to_s.classify.constantize |
50 | 50 | filter = (params[:filter] || "" ).to_sym |
51 | - query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter,klass) | |
51 | + query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter ,klass), categories: params[:categories] | |
52 | 52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
53 | 53 | rescue |
54 | 54 | [] |
... | ... | @@ -87,28 +87,37 @@ module ElasticsearchHelper |
87 | 87 | end |
88 | 88 | |
89 | 89 | |
90 | - def query_method expression="", models=[] | |
91 | - { | |
92 | - query: { | |
93 | - filtered: { | |
94 | - query: query_string(expression,models), | |
95 | - filter: { | |
96 | - bool: { | |
97 | - should: models.map {|model| model.filter(environment: @environment.id) } | |
98 | - } | |
99 | - } | |
90 | + def query_method expression="", models=[], categories=[] | |
91 | + query = {} | |
92 | + query[:query] = { | |
93 | + filtered: { | |
94 | + query: query_string(expression, models), | |
95 | + filter: { | |
96 | + bool: {} | |
100 | 97 | } |
101 | 98 | } |
102 | 99 | } |
100 | + | |
101 | + query[:query][:filtered][:filter][:bool] = { | |
102 | + should: models.map {|model| model.filter(environment: @environment.id) } | |
103 | + } | |
104 | + | |
105 | + unless categories.blank? | |
106 | + query[:query][:filtered][:filter][:bool][:must] = models.first.filter_category(categories) | |
107 | + end | |
108 | + | |
109 | + query | |
103 | 110 | end |
104 | 111 | |
105 | 112 | def get_query text="", options={} |
106 | 113 | klass = options[:klass] |
107 | 114 | sort_by = options[:sort_by] |
115 | + categories = (options[:categories] || "").split(",") | |
116 | + categories = categories.map(&:to_i) | |
108 | 117 | |
109 | 118 | models = (klass.nil?) ? searchable_models : [klass] |
110 | 119 | |
111 | - query = query_method(text, models) | |
120 | + query = query_method(text, models, categories) | |
112 | 121 | query[:sort] = sort_by if sort_by |
113 | 122 | |
114 | 123 | query | ... | ... |
plugins/elasticsearch/helpers/elasticsearch_plugin_helper.rb
0 → 100644
... | ... | @@ -0,0 +1,20 @@ |
1 | +module ElasticsearchPluginHelper | |
2 | + | |
3 | + def render_categories(collection, selected_collections) | |
4 | + content_tag :ul, class: "category-ident" do | |
5 | + if collection.respond_to? :each | |
6 | + collection.collect do |item| | |
7 | + concat ("<li>".html_safe) | |
8 | + concat (check_box_tag(item.name, item.id, selected_collections.include?(item.id.to_s)) ) | |
9 | + concat (label_tag item.name) | |
10 | + concat (render_categories(item.children, selected_collections)) if item.children_count > 0 | |
11 | + concat ("</li>".html_safe) | |
12 | + end | |
13 | + else | |
14 | + check_box_tag collection.name, collection.id, selected_collections.include?(collection.id) | |
15 | + label_tag collection.name | |
16 | + end | |
17 | + end | |
18 | + end | |
19 | + | |
20 | +end | ... | ... |
plugins/elasticsearch/lib/elasticsearch_plugin.rb
plugins/elasticsearch/lib/ext/text_article.rb
... | ... | @@ -11,7 +11,7 @@ class TextArticle |
11 | 11 | def self.control_fields |
12 | 12 | { |
13 | 13 | :advertise => { type: :boolean }, |
14 | - :published => { type: 'boolean'}, | |
14 | + :published => { type: :boolean }, | |
15 | 15 | :comments_count => { type: :integer }, |
16 | 16 | :hits => { type: :integer }, |
17 | 17 | :profile => { type: :nested , hash: NestedProfile.hash } | ... | ... |
plugins/elasticsearch/lib/searchable_model/filter.rb
... | ... | @@ -0,0 +1,27 @@ |
1 | +var main = function() { | |
2 | + var categories = [] | |
3 | + var categoryParam = ""; | |
4 | + var url = window.location.href; | |
5 | + var indexOfCategories; | |
6 | + | |
7 | + url = url.replace(/&categories.*/g, ""); | |
8 | + url += "&categories="; | |
9 | + | |
10 | + $(".categories ul li input[checked]").map(function(idx, element) { | |
11 | + categories.push(element.value); | |
12 | + }); | |
13 | + | |
14 | + $('.categories ul li input[type=checkbox]').on('click', function(){ | |
15 | + var idx = categories.indexOf(this.value); | |
16 | + if (idx == -1) { | |
17 | + categories.push(this.value); | |
18 | + } else { | |
19 | + categories.splice(idx, 1); | |
20 | + } | |
21 | + | |
22 | + url += categories.join(","); | |
23 | + window.location.href = url; | |
24 | + }); | |
25 | +}; | |
26 | + | |
27 | +$(document).ready(main); | ... | ... |
plugins/elasticsearch/public/style.css
... | ... | @@ -50,6 +50,7 @@ |
50 | 50 | font-size: 18px; |
51 | 51 | } |
52 | 52 | |
53 | +.controller-elasticsearch_plugin #content-inner .categories, | |
53 | 54 | .controller-elasticsearch_plugin #content-inner .search-filter, |
54 | 55 | .controller-elasticsearch_plugin #content-inner ul.search-options { |
55 | 56 | background: #fff; |
... | ... | @@ -145,3 +146,19 @@ |
145 | 146 | text-decoration: none; |
146 | 147 | } |
147 | 148 | |
149 | +.controller-elasticsearch_plugin #content-inner .categories .category-ident { | |
150 | + margin-left: 10px; | |
151 | +} | |
152 | + | |
153 | +.controller-elasticsearch_plugin #content-inner .categories .box-title { | |
154 | + margin-bottom: 20px; | |
155 | +} | |
156 | + | |
157 | +.controller-elasticsearch_plugin #content-inner .categories ul li { | |
158 | + padding: 5px; | |
159 | + font-size: 15px; | |
160 | +} | |
161 | + | |
162 | +.controller-elasticsearch_plugin #content-inner .categories li input { | |
163 | + margin-right: 5px; | |
164 | +} | ... | ... |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... | ... | @@ -27,7 +27,7 @@ |
27 | 27 | <ul class="search-options"> |
28 | 28 | <% for type,value in @searchable_types %> |
29 | 29 | <li class="select-search-type <%= "active" if type == @selected_type %>"> |
30 | - <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}"%> | |
30 | + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&categories=#{@selected_categories.join(',')}"%> | |
31 | 31 | </li> |
32 | 32 | <% end %> |
33 | 33 | </ul> |
... | ... | @@ -36,11 +36,16 @@ |
36 | 36 | <ul> |
37 | 37 | <% for type, value in @sort_types %> |
38 | 38 | <li class="select-search-type <%= "active" if type == @selected_sort %>"> |
39 | - <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}" %> | |
39 | + <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}&categories=#{@selected_categories.join(',')}" %> | |
40 | 40 | </li> |
41 | 41 | <% end %> |
42 | 42 | </ul> |
43 | 43 | </div> |
44 | + | |
45 | + <div class="categories"> | |
46 | + <h3 class="box-title"><%= _("Categories") %></h3> | |
47 | + <%= render_categories(@categories, @selected_categories) %> | |
48 | + </div> | |
44 | 49 | </div> |
45 | 50 | |
46 | 51 | <div class="results"> |
... | ... | @@ -59,5 +64,6 @@ |
59 | 64 | <%= pagination_links @results if @results.count > 0 %> |
60 | 65 | </div> |
61 | 66 | </div> |
67 | + | |
62 | 68 | </div> |
63 | 69 | </div> | ... | ... |