Commit a6fc6a13ae33ebf3c34f5141cda0c4524ef728d0

Authored by Dylan Guedes
Committed by Macartur Sousa
1 parent 84db6592

Adds dynamic rendering of javascript helper

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,22 @@ 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 +
  22 +
15 23 var idx = categories.indexOf(this.value);
16 24 if (idx == -1) {
17 25 categories.push(this.value);
... ... @@ -19,8 +27,16 @@ var main = function() {
19 27 categories.splice(idx, 1);
20 28 }
21 29  
22   - url += categories.join(",");
23   - window.location.href = url;
  30 + $.ajax({
  31 + method: "GET",
  32 + url: "/search?format=js",
  33 + data: {
  34 + categories: categories.join(","),
  35 + selected_type: dataParams["selected_type"],
  36 + filter: dataParams["filter"],
  37 + query: dataParams["query"]
  38 + }
  39 + });
24 40 });
25 41 };
26 42  
... ...
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/_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.html.erb
... ... @@ -44,25 +44,14 @@
44 44  
45 45 <div class="categories">
46 46 <h3 class="box-title"><%= _("Categories") %></h3>
47   - <%= render_categories(@categories, @selected_categories) %>
  47 + <div id="categories-list">
  48 + <%= render_categories(@categories, @selected_categories) %>
  49 + </div>
48 50 </div>
49 51 </div>
50 52  
51 53 <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>
  54 + <%= render :partial => 'search_collection' %>
66 55 </div>
67 56  
68 57 </div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.js.erb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +$(".results").html("<%= escape_javascript(render partial: 'search_collection') %>");
  2 +
... ...