Commit 90c1659995b840fd24d71faf6766c16a9b05e18e

Authored by Macartur Sousa
1 parent 3509f25c
Exists in fix_sign_up_form

Elasticsearch: Adding style to views

Create initial style of elasticsearch, and refectored html
Changes style of elasticsearch plugin, add variable to store search types.
Adds style for communities partial.
Adds person partial style
Adding event to models and adding styles
Refactored search controller and view
  - Changed search-form to use get instead of post
  - Uses search method directly in models
  - Refactored process results
  - Adding pagination
Removed hardcode name from community view

Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 1 class ElasticsearchPluginController < ApplicationController
2 2 no_design_blocks
3 3  
4   - SEARCHABLE_MODELS = {communities: true, articles: true, people: true}
  4 + SEARCHABLE_TYPES = { :all => { label: _("All Results")},
  5 + :community => { label: _("Communities")},
  6 + :event => { label: _("Events")},
  7 + :person => { label: _("People")}
  8 + }
  9 +
  10 + SEARCH_FILTERS = { :lexical => { label: _("Alphabetical Order")},
  11 + :recent => { label: _("More Recent Order")},
  12 + :access => { label: _("More accessed")}
  13 + }
5 14  
6 15 def index
7 16 search()
... ... @@ -9,39 +18,37 @@ class ElasticsearchPluginController &lt; ApplicationController
9 18 end
10 19  
11 20 def search
12   - @results = []
  21 + define_searchable_types
  22 + define_search_fields_types
  23 +
  24 + process_results
  25 + end
  26 +
  27 + def process_results
13 28 @query = params[:q]
14   - @checkbox = {}
15 29  
16   - if params[:model].present?
17   - params[:model].keys.each do |model|
18   - @checkbox[model.to_sym] = true
19   - results model
20   - end
  30 + if @selected_type == :all
  31 + @results = search_from_all_models
21 32 else
22   - unless params[:q].blank?
23   - SEARCHABLE_MODELS.keys.each do |model|
24   - results model
25   - end
26   - end
  33 + @results = search_from_model @selected_type
27 34 end
28   -
29 35 end
30 36  
31 37 private
32 38  
33   - def get_query text, klass
  39 + def fields_from_model
  40 + klass::SEARCHABLE_FIELDS.map do |key, value|
  41 + if value[:weight]
  42 + "#{key}^#{value[:weight]}"
  43 + else
  44 + "#{key}"
  45 + end
  46 + end
  47 + end
  48 +
  49 + def get_query text, klass=nil
34 50 query = {}
35 51 unless text.blank?
36   - text = text.downcase
37   - fields = klass::SEARCHABLE_FIELDS.map do |key, value|
38   - if value[:weight]
39   - "#{key}^#{value[:weight]}"
40   - else
41   - "#{key}"
42   - end
43   - end
44   -
45 52 query = {
46 53 query: {
47 54 match_all: {
... ... @@ -68,23 +75,37 @@ class ElasticsearchPluginController &lt; ApplicationController
68 75 query
69 76 end
70 77  
71   - def get_terms params
72   - terms = {}
73   - return terms unless params[:filter].present?
74   - params[:filter].keys.each do |model|
75   - terms[model] = {}
76   - params[:filter][model].keys.each do |filter|
77   - @checkbox[filter.to_sym] = true
78   - terms[model][params[:filter][model.to_sym][filter]] = filter
79   - end
  78 +
  79 + def search_from_all_models
  80 + models = []
  81 + query = get_query params[:q]
  82 +
  83 + SEARCHABLE_TYPES.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all }
  84 + Elasticsearch::Model.search(query, models, size: default_per_page).page(params[:page]).records
  85 + end
  86 +
  87 + def search_from_model model
  88 + begin
  89 + klass = model.to_s.classify.constantize
  90 + query = get_query params[:q], klass
  91 + klass.search(query, size: default_per_page).page(params[:page]).records
  92 + rescue
  93 + []
80 94 end
81   - terms
82 95 end
83 96  
84   - def results model
85   - klass = model.to_s.classify.constantize
86   - query = get_query params[:q], klass
87   - @results |= klass.__elasticsearch__.search(query).records.to_a
  97 + def define_searchable_types
  98 + @searchable_types = SEARCHABLE_TYPES
  99 + @selected_type = params[:selected_type].nil? ? :all : params[:selected_type].to_sym
  100 + end
  101 +
  102 + def define_search_fields_types
  103 + @search_filter_types = SEARCH_FILTERS
  104 + @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym
  105 + end
  106 +
  107 + def default_per_page
  108 + 10
88 109 end
89 110  
90 111 end
... ...
plugins/elasticsearch/lib/elasticsearch_plugin.rb
... ... @@ -8,4 +8,7 @@ class ElasticsearchPlugin &lt; Noosfero::Plugin
8 8 _("This plugin is used to communicate a elasticsearch to privide a search.")
9 9 end
10 10  
  11 + def stylesheet?
  12 + true
  13 + end
11 14 end
... ...
plugins/elasticsearch/public/style.css 0 → 100644
... ... @@ -0,0 +1,78 @@
  1 +.controller-elasticsearch_plugin .wrapper {
  2 + display: flex;
  3 +}
  4 +
  5 +.controller-elasticsearch_plugin .sidebar {
  6 + width: 150px;
  7 +}
  8 +
  9 +.controller-elasticsearch_plugin .search_form {
  10 + flex: 1;
  11 +}
  12 +
  13 +.controller-elasticsearch_plugin .search_form .search_field {
  14 + margin-top: 5px;
  15 +}
  16 +.controller-elasticsearch_plugin .search_form .search_field #q {
  17 + width: 500px;
  18 +}
  19 +
  20 +.controller-elasticsearch_plugin .results-count {
  21 + margin-top: 7px;
  22 + margin-right: 15px;
  23 + text-align: right;
  24 +}
  25 +
  26 +.controller-elasticsearch_plugin ul {
  27 + list-style: none;
  28 + list-style-position: inside;
  29 + padding-left: 0px;
  30 +}
  31 +
  32 +.controller-elasticsearch_plugin .select-search-type {
  33 + padding: 10px;
  34 + margin: 10px;
  35 + font-weight: 900;
  36 + font-family: arial;
  37 + border: 1px solid #ddd;
  38 + margin-top: 0px;
  39 + margin-bottom: 0px;
  40 +}
  41 +.controller-elasticsearch_plugin .select-search-type.active {
  42 + padding: 10px;
  43 + background: #ccc;
  44 + margin-left: 10px;
  45 + margin-right: 10px;
  46 + font-weight: 900;
  47 + font-family: arial;
  48 +}
  49 +
  50 +.controller-elasticsearch_plugin .select-search-type a {
  51 + text-decoration: none;
  52 + color: black;
  53 +}
  54 +
  55 +.controller-elasticsearch_plugin .search-item .model-label {
  56 + background: #ddd;
  57 + color: black;
  58 + font-weight: 900;
  59 + padding: 3px;
  60 +}
  61 +
  62 +.controller-elasticsearch_plugin .search-item {
  63 + margin-bottom: 25px;
  64 + margin-top: 25px;
  65 +}
  66 +
  67 +.controller-elasticsearch_plugin .search-item .person-item {
  68 + display: flex;
  69 +}
  70 +
  71 +.controller-elasticsearch_plugin .right-side {
  72 + flex: 2;
  73 +}
  74 +
  75 +.controller-elasticsearch_plugin .left-side {
  76 + margin-left: 25px;
  77 + width: 100px;
  78 +}
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb
... ... @@ -1,2 +0,0 @@
1   -
2   -Article: <%= article.id %><%= article.name %>
plugins/elasticsearch/views/elasticsearch_plugin/_community_display.html.erb
1   -Community: <%= community.name %>
  1 +<div class="community-header">
  2 + <%= community.created_at.strftime("%d %B %Y at %H:%M") %> - <span class="model-label">COMUNIDADE</span>
  3 +</div>
  4 +<div class="body">
  5 + <h2><%= community.name %></h2>
  6 + <p><%= community.description %></p>
  7 +</div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_event_display.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<div class="event-header">
  2 + <%= event.created_at.strftime("%d %B %Y at %H:%M") %> - <span class="model-label">EVENT</span>
  3 +</div>
  4 +
  5 +<div class="body">
  6 + <h2><%= event.name %></h2>
  7 + <p><%= event.body %></p>
  8 +</div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb
1   -
2   -Person: <%= person.name %>
  1 +<div class="person-item">
  2 + <div class="left-side">
  3 + <%= profile_image person %>
  4 + </div>
  5 + <div class="right-side">
  6 + <%= person.created_at.strftime("%d %B %Y at %H:%M") %> - <%= person.name %> <span class="model-label"><%= _("PERSON") %></span>
  7 + <div class="body">
  8 + <h2><%= person.name %></h2>
  9 + <p><%= person.description %></p>
  10 + </div>
  11 + </div>
  12 +</div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
1   -<h1> Search </h1>
2   -
3   -<%= form_tag controller: "elasticsearch_plugin", action: "search" do %>
4   - <%= label_tag(:q, _("Search")) %>
5   - <%= text_field_tag(:q, @query) %>
6   -
7   - <%= submit_tag _("Send") %>
8   -
9   - <%= check_box_tag 'model[communities]', 1, @checkbox[:communities] %>
10   - <%= label_tag('communities', _("communities")) %>
11   -
12   - <%= check_box_tag 'model[people]', 1, @checkbox[:people] %>
13   - <%= label_tag('people', _("people")) %>
14   -
15   - <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %>
16   - <%= label_tag('articles', _("articles")) %>
17   -
18   -<% end %>
19   -
20   -<% for result in @results %>
21   - <% if result.is_a? Article %>
22   - <%= render partial: "article_display", :locals => {:article => result} %>
23   - <br>
24   - <% end %>
25   -
26   - <% if result.is_a? Person %>
27   - <%= render partial: "person_display", :locals => {:person => result} %>
28   - <br>
29   - <% end %>
30   - <% if result.is_a? Community %>
31   - <%= render partial: "community_display", :locals => {:community => result} %>
32   - <br>
33   - <% end %>
34   -<% end %>
  1 +<div class="wrapper">
  2 +
  3 + <div class="sidebar">
  4 + <div class="results-count">
  5 + <%= @results.total %> <%= _(" results by") %> :
  6 + </div>
  7 + <ul>
  8 + <% for type,value in @searchable_types %>
  9 + <li class="select-search-type <%= "active" if type == @selected_type %>">
  10 + <%= link_to value[:label], "?selected_type=#{type}&q=#{@query}&selected_filter_field=#{@selected_filter_field}"%>
  11 + </li>
  12 + <% end %>
  13 + </ul>
  14 +
  15 + <div class="search-filter">
  16 + <h3><%= _("Filter by") %></h3>
  17 + <ul>
  18 + <% for type in @search_filter_types.values %>
  19 + <li><a href="#"><%= type[:label] %></a></li>
  20 + <% end %>
  21 + </ul>
  22 + </div>
  23 + </div>
  24 +
  25 + <div class="search_form">
  26 + <div class="search_field">
  27 + <%= form_tag '/plugin/elasticsearch/search', method: :get do %>
  28 + <%= hidden_field_tag "selected_type", @selected_type %>
  29 + <%= hidden_field_tag "selected_filter_field", @selected_filter_field %>
  30 + <%= text_field_tag :q, @query %>
  31 + <%= submit_tag _("Send") %>
  32 + <% end %>
  33 + </div>
  34 +
  35 + <div class="results">
  36 + <% for result in @results.to_a %>
  37 + <% for klass in @searchable_types.keys %>
  38 + <% next if klass.to_s.include? "all" %>
  39 + <% if result.is_a? klass.to_s.classify.constantize %>
  40 + <div class="search-item">
  41 + <%= render partial: "#{klass}_display", :locals => { klass => result} %>
  42 + </div>
  43 + <% break %>
  44 + <% end %>
  45 + <% end %>
  46 + <% end %>
  47 + <div class="search_paginate">
  48 + <%= pagination_links @results %>
  49 + </div>
  50 + </div>
  51 + </div>
  52 +</div>
... ...