Commit 59364b6fa85795b62d2bcd25be71b536d62c258c

Authored by Dylan Guedes
Committed by Macartur Sousa
1 parent 1110c012

Elasticsearch: Fixed categories updates and views

* Adds dynamic rendering of javascript helper
* Adding partial renders to update links and input text

Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
Signed-off-by: Macartur de Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
... ... @@ -7,8 +7,7 @@ class ElasticsearchPluginController &lt; ApplicationController
7 7 helper ElasticsearchPluginHelper
8 8  
9 9 def index
10   - search()
11   - render :action => 'search'
  10 + search
12 11 end
13 12  
14 13 def search
... ... @@ -16,6 +15,11 @@ class ElasticsearchPluginController &lt; ApplicationController
16 15 define_sort_types
17 16 define_categories
18 17 define_results
  18 + respond_to do |format|
  19 + format.html { render :action => 'search' }
  20 + format.js
  21 + params["format"] = ""
  22 + end
19 23 end
20 24  
21 25 def define_results
... ...
plugins/elasticsearch/public/javascripts/categories.js
... ... @@ -4,14 +4,21 @@ var main = function() {
4 4 var url = window.location.href;
5 5 var indexOfCategories;
6 6  
7   - url = url.replace(/&categories.*/g, "");
8   - url += "&categories=";
9   -
10 7 $(".categories ul li input[checked]").map(function(idx, element) {
11 8 categories.push(element.value);
12 9 });
13 10  
14 11 $('.categories ul li input[type=checkbox]').on('click', function(){
  12 + var dataParams = {};
  13 +
  14 + url = url.replace(/.*\?/, "");
  15 + var params = url.split('&');
  16 + console.log("Dataparams: ", params);
  17 + params.map(function(param) {
  18 + var item = param.split('=');
  19 + dataParams[item[0]] = item[1];
  20 + });
  21 +
15 22 var idx = categories.indexOf(this.value);
16 23 if (idx == -1) {
17 24 categories.push(this.value);
... ... @@ -19,8 +26,13 @@ var main = function() {
19 26 categories.splice(idx, 1);
20 27 }
21 28  
22   - url += categories.join(",");
23   - window.location.href = url;
  29 + dataParams['categories'] = categories.join(",")
  30 +
  31 + $.ajax({
  32 + method: "GET",
  33 + url: "/search?format=js",
  34 + data: dataParams
  35 + });
24 36 });
25 37 };
26 38  
... ...
plugins/elasticsearch/public/style.css
... ... @@ -52,7 +52,7 @@
52 52  
53 53 .controller-elasticsearch_plugin #content-inner .categories,
54 54 .controller-elasticsearch_plugin #content-inner .search-filter,
55   -.controller-elasticsearch_plugin #content-inner ul.search-options {
  55 +.controller-elasticsearch_plugin #content-inner .search-options ul {
56 56 background: #fff;
57 57 overflow: hidden;
58 58 border-radius: 5px;
... ...
plugins/elasticsearch/test/api/elasticsearch_plugin_api_test.rb
... ... @@ -122,4 +122,16 @@ class ElasticsearchPluginApiTest &lt; ActiveSupport::TestCase
122 122 assert_equal 3, json["results"].count
123 123 end
124 124  
  125 + should 'respond with only categories from given model' do
  126 + get "/api/v1/search?selected_type=community&categories=1,2,3"
  127 + json = JSON.parse(last_response.body)
  128 + assert_equal 200, last_response.status
  129 + assert_equal 3, json["results"].count
  130 +
  131 + get "/api/v1/search?selected_type=person&categories=1,2"
  132 + json = JSON.parse(last_response.body)
  133 + assert_equal 200, last_response.status
  134 + assert_equal 0, json["results"].count
  135 + end
  136 +
125 137 end
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_results_count.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<p>
  2 + <strong><%= @hits %></strong>
  3 + <% if not @query.blank? %>
  4 + <%= _(" results for ") %><%= @query %>
  5 + <% else %>
  6 + <%= _(" total results") %>
  7 + <% end %>
  8 +</p>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_search_collection.html.erb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +<% for result in @results.to_a %>
  2 + <% for klass in @searchable_types.keys %>
  3 + <% next if klass.to_s.include? "all" %>
  4 + <% if result.is_a? klass.to_s.classify.constantize %>
  5 + <div class="search-item">
  6 + <%= render partial: "#{klass}_display", :locals => { klass => result} %>
  7 + </div>
  8 + <% break %>
  9 + <% end %>
  10 + <% end %>
  11 +<% end %>
  12 +
  13 +<div class="search_paginate">
  14 + <%= pagination_links @results if @results.count > 0 %>
  15 +</div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_search_field.html.erb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<%= form_tag '/plugin/elasticsearch/search', method: :get do %>
  2 +
  3 + <%= hidden_field_tag("selected_type", @selected_type) if @selected_type %>
  4 +
  5 + <%= hidden_field_tag("filter", @selected_sort) if @selected_sort %>
  6 +
  7 + <%= hidden_field_tag("categories", @selected_categories.join(",")) if @selected_categories %>
  8 +
  9 + <%= text_field_tag :query, @query %>
  10 +
  11 + <%= submit_tag _("Send") %>
  12 +<% end %>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_search_filter.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<h3 class="box-title"><%= _("Sort by") %></h3>
  2 +<ul>
  3 +<% for type, value in @sort_types %>
  4 + <li class="select-search-type <%= "active" if type == @selected_sort %>">
  5 + <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}&categories=#{@selected_categories.join(',')}" %>
  6 + </li>
  7 +<% end %>
  8 +</ul>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_search_option.html.erb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<ul>
  2 + <% for type,value in @searchable_types %>
  3 + <li class="select-search-type <%= "active" if type == @selected_type %>">
  4 + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&categories=#{@selected_categories.join(',')}"%>
  5 + </li>
  6 + <% end %>
  7 +</ul>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
1 1 <div class="wrapper">
2 2 <div class="search_form">
3 3 <div class="search_field">
4   - <%= form_tag '/plugin/elasticsearch/search', method: :get do %>
5   - <%= hidden_field_tag "selected_type", @selected_type %>
6   - <% if @selected_filter %>
7   - <%= hidden_field_tag "filter", @selected_filter %>
8   - <% end %>
9   - <%= text_field_tag :query, @query %>
10   - <%= submit_tag _("Send") %>
11   - <% end %>
  4 + <%= render :partial => "search_field" %>
12 5 </div>
13 6 <div class="results-count">
14   - <p>
15   - <strong><%= @hits %></strong>
16   - <% if not @query.blank? %>
17   - <%= _(" results for ") %><%= @query %>
18   - <% else %>
19   - <%= _(" total results") %>
20   - <% end %>
21   - </p>
  7 + <%= render :partial => "results_count" %>
22 8 </div>
23 9 </div>
24 10  
25 11 <div class="results-wrapper">
26 12 <div class="sidebar">
27   - <ul class="search-options">
28   - <% for type,value in @searchable_types %>
29   - <li class="select-search-type <%= "active" if type == @selected_type %>">
30   - <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&categories=#{@selected_categories.join(',')}"%>
31   - </li>
32   - <% end %>
33   - </ul>
  13 +
  14 + <div class="search-options">
  15 + <%= render :partial => "search_option.html.erb" %>
  16 + </div>
  17 +
34 18 <div class="search-filter">
35   - <h3 class="box-title"><%= _("Sort by") %></h3>
36   - <ul>
37   - <% for type, value in @sort_types %>
38   - <li class="select-search-type <%= "active" if type == @selected_sort %>">
39   - <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}&categories=#{@selected_categories.join(',')}" %>
40   - </li>
41   - <% end %>
42   - </ul>
  19 + <%= render :partial => "search_filter.html.erb" %>
43 20 </div>
44 21  
45 22 <div class="categories">
46 23 <h3 class="box-title"><%= _("Categories") %></h3>
47   - <%= render_categories(@categories, @selected_categories) %>
  24 + <div id="categories-list">
  25 + <%= render_categories(@categories, @selected_categories) %>
  26 + </div>
48 27 </div>
49 28 </div>
50 29  
51 30 <div class="results">
52   - <% for result in @results.to_a %>
53   - <% for klass in @searchable_types.keys %>
54   - <% next if klass.to_s.include? "all" %>
55   - <% if result.is_a? klass.to_s.classify.constantize %>
56   - <div class="search-item">
57   - <%= render partial: "#{klass}_display", :locals => { klass => result} %>
58   - </div>
59   - <% break %>
60   - <% end %>
61   - <% end %>
62   - <% end %>
63   - <div class="search_paginate">
64   - <%= pagination_links @results if @results.count > 0 %>
65   - </div>
  31 + <%= render :partial => 'search_collection' %>
66 32 </div>
67 33  
68 34 </div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.js.erb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +$(".results").html("<%= escape_javascript(render partial: 'search_collection') %>");
  2 +$(".results-count").html("<%= escape_javascript(render partial: 'results_count') %>");
  3 +$(".search-options").html("<%= escape_javascript(render partial: 'search_option') %>");
  4 +$(".search-filter").html("<%= escape_javascript(render partial: 'search_filter') %>");
  5 +$(".search_field").html("<%= escape_javascript(render partial: 'search_field') %>");
... ...