Commit b51e95f0135cbeb18f86acb8fe817f7473742dd7

Authored by Macartur Sousa
1 parent e4628511
Exists in fix_sign_up_form

Elasticsearch: refacted sort by

* Changed name from helpers to display sort_types
* Adding dynamic sort to text_article
* indexed category_ids to be used after

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
@@ -11,7 +11,7 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -11,7 +11,7 @@ class ElasticsearchPluginController &lt; ApplicationController
11 11
12 def search 12 def search
13 define_searchable_types 13 define_searchable_types
14 - define_search_fields_types 14 + define_sort_types
15 define_results 15 define_results
16 end 16 end
17 17
@@ -26,9 +26,9 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -26,9 +26,9 @@ class ElasticsearchPluginController &lt; ApplicationController
26 @selected_type = (params[:selected_type]|| :all ).to_sym 26 @selected_type = (params[:selected_type]|| :all ).to_sym
27 end 27 end
28 28
29 - def define_search_fields_types  
30 - @filter_types = filters  
31 - @selected_filter = (params[:filter] || :relevance).to_sym 29 + def define_sort_types
  30 + @sort_types = sort_types
  31 + @selected_sort = (params[:filter] || :relevance).to_sym
32 end 32 end
33 33
34 end 34 end
plugins/elasticsearch/helpers/elasticsearch_helper.rb
@@ -11,8 +11,8 @@ module ElasticsearchHelper @@ -11,8 +11,8 @@ module ElasticsearchHelper
11 } 11 }
12 end 12 end
13 13
14 - def filters  
15 - filters = { 14 + def sort_types
  15 + sorts = {
16 :relevance => { label: _("Relevance")}, 16 :relevance => { label: _("Relevance")},
17 :lexical => { label: _("Alphabetical")}, 17 :lexical => { label: _("Alphabetical")},
18 :more_recent => { label: _("More Recent")}, 18 :more_recent => { label: _("More Recent")},
@@ -20,11 +20,11 @@ module ElasticsearchHelper @@ -20,11 +20,11 @@ module ElasticsearchHelper
20 20
21 selected_type = params[:selected_type] || nil 21 selected_type = params[:selected_type] || nil
22 22
23 - if selected_type 23 + if selected_type and selected_type.to_sym != :all
24 klass = selected_type.to_s.classify.constantize 24 klass = selected_type.to_s.classify.constantize
25 - filters.update klass.especific_filter if klass.respond_to? :especific_filter 25 + sorts.update klass.especific_sort if klass.respond_to? :especific_sort
26 end 26 end
27 - filters 27 + sorts
28 end 28 end
29 29
30 def process_results 30 def process_results
plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb
@@ -66,8 +66,9 @@ module ElasticsearchIndexedModel @@ -66,8 +66,9 @@ module ElasticsearchIndexedModel
66 66
67 def indexed_fields 67 def indexed_fields
68 fields = { 68 fields = {
69 - :environment => {type: :nested, hash: NestedEnvironment::environment_hash },  
70 - :created_at => {type: :date } 69 + :environment => {type: :nested, hash: NestedEnvironment::environment_hash },
  70 + :category_ids => {type: :integer },
  71 + :created_at => {type: :date }
71 } 72 }
72 fields.update(self::SEARCHABLE_FIELDS) 73 fields.update(self::SEARCHABLE_FIELDS)
73 fields.update(self.control_fields) 74 fields.update(self.control_fields)
plugins/elasticsearch/lib/elasticsearch_plugin/api.rb
@@ -18,8 +18,8 @@ class ElasticsearchPlugin::API &lt; Grape::API @@ -18,8 +18,8 @@ class ElasticsearchPlugin::API &lt; Grape::API
18 present types, with: Grape::Presenters::Presenter 18 present types, with: Grape::Presenters::Presenter
19 end 19 end
20 20
21 - get 'filters' do  
22 - present filters, with: Grape::Presenters::Presenter 21 + get 'sort_types' do
  22 + present sort_types, with: Grape::Presenters::Presenter
23 end 23 end
24 24
25 end 25 end
plugins/elasticsearch/lib/ext/community.rb
@@ -23,7 +23,7 @@ class Community @@ -23,7 +23,7 @@ class Community
23 ] 23 ]
24 end 24 end
25 25
26 - def self.especific_filter 26 + def self.especific_sort
27 { 27 {
28 :more_active => { label: _("More Active") }, 28 :more_active => { label: _("More Active") },
29 :more_popular => { label: _("More Popular") } 29 :more_popular => { label: _("More Popular") }
plugins/elasticsearch/lib/ext/person.rb
@@ -24,7 +24,7 @@ class Person @@ -24,7 +24,7 @@ class Person
24 ] 24 ]
25 end 25 end
26 26
27 - def self.especific_filter 27 + def self.especific_sort
28 { 28 {
29 :more_active => { label: _("More Active") }, 29 :more_active => { label: _("More Active") },
30 :more_popular => { label: _("More Popular") } 30 :more_popular => { label: _("More Popular") }
plugins/elasticsearch/lib/ext/text_article.rb
@@ -10,9 +10,11 @@ class TextArticle @@ -10,9 +10,11 @@ class TextArticle
10 10
11 def self.control_fields 11 def self.control_fields
12 { 12 {
13 - :advertise => { type: :boolean },  
14 - :published => { type: 'boolean'},  
15 - :profile => { type: :nested , hash: NestedProfile.hash } 13 + :advertise => { type: :boolean },
  14 + :published => { type: 'boolean'},
  15 + :comments_count => { type: :integer },
  16 + :hits => { type: :integer },
  17 + :profile => { type: :nested , hash: NestedProfile.hash }
16 } 18 }
17 end 19 end
18 20
@@ -32,5 +34,21 @@ class TextArticle @@ -32,5 +34,21 @@ class TextArticle
32 ] 34 ]
33 end 35 end
34 36
  37 + def self.especific_sort
  38 + {
  39 + :more_popular => { label: _("More Viewed") },
  40 + :more_comments => { label: _("More Commented") }
  41 + }
  42 + end
  43 +
  44 + def self.get_sort_by sort_by
  45 + case sort_by
  46 + when "more_popular"
  47 + { :hits => {order: :desc} }
  48 + when "more_comments"
  49 + { :comments_count => {order: :desc}}
  50 + end
  51 + end
  52 +
35 include SearchableModelHelper 53 include SearchableModelHelper
36 end 54 end
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
@@ -26,18 +26,18 @@ @@ -26,18 +26,18 @@
26 <div class="sidebar"> 26 <div class="sidebar">
27 <ul class="search-options"> 27 <ul class="search-options">
28 <% for type,value in @searchable_types %> 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}&filter=#{@selected_filter}"%>  
31 - </li> 29 + <li class="select-search-type <%= "active" if type == @selected_type %>">
  30 + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&filter=#{@selected_sort}"%>
  31 + </li>
32 <% end %> 32 <% end %>
33 </ul> 33 </ul>
34 <div class="search-filter"> 34 <div class="search-filter">
35 <h3 class="box-title"><%= _("Sort by") %></h3> 35 <h3 class="box-title"><%= _("Sort by") %></h3>
36 <ul> 36 <ul>
37 - <% for type, value in @filter_types %>  
38 - <li class="select-search-type <%= "active" if type == @selected_filter %>">  
39 - <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}" %>  
40 - </li> 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}" %>
  40 + </li>
41 <% end %> 41 <% end %>
42 </ul> 42 </ul>
43 </div> 43 </div>