Commit 9621cfda75d765b2c478214fc108d1f5ef66398e
Committed by
Macartur Sousa
1 parent
ca88b8ac
Adds dynamic categories via javascript url
Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Macartur de Sousa <macartur.sc@gmail.com>
Showing
4 changed files
with
51 additions
and
22 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
| @@ -26,7 +26,7 @@ class ElasticsearchPluginController < ApplicationController | @@ -26,7 +26,7 @@ class ElasticsearchPluginController < ApplicationController | ||
| 26 | 26 | ||
| 27 | def define_searchable_types | 27 | def define_searchable_types |
| 28 | @searchable_types = searchable_types | 28 | @searchable_types = searchable_types |
| 29 | - @selected_type = (params[:selected_type]|| :all ).to_sym | 29 | + @selected_type = (params[:selected_type] || :all).to_sym |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | def define_sort_types | 32 | def define_sort_types |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
| @@ -48,7 +48,7 @@ module ElasticsearchHelper | @@ -48,7 +48,7 @@ module ElasticsearchHelper | ||
| 48 | begin | 48 | begin |
| 49 | klass = model.to_s.classify.constantize | 49 | klass = model.to_s.classify.constantize |
| 50 | filter = (params[:filter] || "" ).to_sym | 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(params[:filter],klass), categories: params[:categories] |
| 52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records | 52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
| 53 | rescue | 53 | rescue |
| 54 | [] | 54 | [] |
| @@ -87,28 +87,37 @@ module ElasticsearchHelper | @@ -87,28 +87,37 @@ module ElasticsearchHelper | ||
| 87 | end | 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 | end | 110 | end |
| 104 | 111 | ||
| 105 | def get_query text="", options={} | 112 | def get_query text="", options={} |
| 106 | klass = options[:klass] | 113 | klass = options[:klass] |
| 107 | sort_by = options[:sort_by] | 114 | sort_by = options[:sort_by] |
| 115 | + categories = (options[:categories] || "").split(",") | ||
| 116 | + categories = categories.map(&:to_i) | ||
| 108 | 117 | ||
| 109 | models = (klass.nil?) ? searchable_models : [klass] | 118 | models = (klass.nil?) ? searchable_models : [klass] |
| 110 | 119 | ||
| 111 | - query = query_method(text, models) | 120 | + query = query_method(text, models, categories) |
| 112 | query[:sort] = sort_by if sort_by | 121 | query[:sort] = sort_by if sort_by |
| 113 | 122 | ||
| 114 | query | 123 | query |
plugins/elasticsearch/lib/searchable_model/filter.rb
| @@ -32,6 +32,15 @@ module Filter | @@ -32,6 +32,15 @@ module Filter | ||
| 32 | 32 | ||
| 33 | result_filter | 33 | result_filter |
| 34 | end | 34 | end |
| 35 | + | ||
| 36 | + def filter_category selected_categories | ||
| 37 | + { | ||
| 38 | + query: { | ||
| 39 | + terms: { category_ids: selected_categories } | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + end | ||
| 43 | + | ||
| 35 | end | 44 | end |
| 36 | 45 | ||
| 37 | module InstanceMethods | 46 | module InstanceMethods |
plugins/elasticsearch/public/javascripts/categories.js
| 1 | var main = function() { | 1 | var main = function() { |
| 2 | - $('.categories ul li input').on("click", function() { | ||
| 3 | - var categoryParam = ""; | ||
| 4 | - if (window.location.href.search("categories") < 0) { | ||
| 5 | - categoryParam += "&categories="; | ||
| 6 | - } | ||
| 7 | - if ($(".categories ul li input[checked]").length > 0){ | ||
| 8 | - categoryParam += ("," + this.value); | 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); | ||
| 9 | } else { | 18 | } else { |
| 10 | - categoryParam += (this.value); | 19 | + categories.splice(idx, 1); |
| 11 | } | 20 | } |
| 12 | - window.location.href += categoryParam; | 21 | + |
| 22 | + url += categories.join(","); | ||
| 23 | + window.location.href = url; | ||
| 13 | }); | 24 | }); |
| 14 | }; | 25 | }; |
| 15 | 26 |