diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index 71b5c45..8cbd9b0 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -35,15 +35,20 @@ module ElasticsearchHelper private def search_from_all_models - query = get_query params[:query], sort_by: get_sort_by(params[:filter]) - Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records + begin + filter = (params[:filter] || "" ).to_sym + query = get_query params[:query], sort_by: get_sort_by(filter) + Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records + rescue + [] + end end def search_from_model model begin klass = model.to_s.classify.constantize - - query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter],klass) + filter = (params[:filter] || "" ).to_sym + query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter,klass) klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records rescue [] @@ -56,9 +61,9 @@ module ElasticsearchHelper def get_sort_by sort_by, klass=nil case sort_by - when "lexical" + when :lexical { "name.raw" => {"order" => "asc" }} - when "more_recent" + when :more_recent { "created_at" => {"order" => "desc"}} else ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil @@ -105,6 +110,7 @@ module ElasticsearchHelper query = query_method(text, models) query[:sort] = sort_by if sort_by + query end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index 4dcf128..3f2a8a9 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -30,12 +30,12 @@ class Community } end - def self.get_sort_by sort_by + def self.get_sort_by sort_by="" case sort_by - when "more_active" + when :more_active { :activities_count => {order: :desc}} - when "more_popular" - { :members_count => {order: :desc} } + when :more_popular + { :members_count => {order: :desc}} end end diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 453d9b3..eb6569a 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -31,11 +31,11 @@ class Person } end - def self.get_sort_by sort_by + def self.get_sort_by sort_by="" case sort_by - when "more_active" + when :more_active { :activities_count => {order: :desc}} - when "more_popular" + when :more_popular { :friends_count => {order: :desc} } end end diff --git a/plugins/elasticsearch/lib/ext/text_article.rb b/plugins/elasticsearch/lib/ext/text_article.rb index 5c65e18..bd65f03 100644 --- a/plugins/elasticsearch/lib/ext/text_article.rb +++ b/plugins/elasticsearch/lib/ext/text_article.rb @@ -37,15 +37,15 @@ class TextArticle def self.especific_sort { :more_popular => { label: _("More Viewed") }, - :more_comments => { label: _("More Commented") } + :more_comments => { label: _("Most Commented") } } end - def self.get_sort_by sort_by + def self.get_sort_by sort_by="" case sort_by - when "more_popular" + when :more_popular { :hits => {order: :desc} } - when "more_comments" + when :more_comments { :comments_count => {order: :desc}} end end diff --git a/plugins/elasticsearch/lib/searchable_model/filter.rb b/plugins/elasticsearch/lib/searchable_model/filter.rb index 88ff870..1583b61 100644 --- a/plugins/elasticsearch/lib/searchable_model/filter.rb +++ b/plugins/elasticsearch/lib/searchable_model/filter.rb @@ -32,7 +32,6 @@ module Filter result_filter end - end module InstanceMethods diff --git a/plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb b/plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb index 6357568..ed372e8 100644 --- a/plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb +++ b/plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb @@ -9,6 +9,20 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase end def create_instances + create_instances_environment + create_instances_environment2 + end + + def create_instances_environment2 + 5.times do |index| + create_user "Sample User Environment 2", environment:Environment.second + end + 6.times do |index| + fast_create Community, name:"Sample Community Environment 2", created_at: Date.new, environment_id: Environment.second.id + end + end + + def create_instances_environment create_people create_communities end @@ -73,7 +87,7 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) end - should 'return new person indexed' do + should 'return new community indexed' do get :index, { "selected_type" => :community} assert_response :success assert_select ".search-item", 6 @@ -87,13 +101,14 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase assert_select ".search-item", 7 end - should 'not return person deleted' do + should 'not return community deleted' do get :index, { "selected_type" => :community} assert_response :success assert_select ".search-item", 6 Community.first.delete Community.import + sleep 2 get :index, { "selected_type" => :community} assert_response :success @@ -112,4 +127,17 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase assert_not_nil assigns(:results) assert_template partial: '_community_display' end + + should 'filter community by default environment' do + get :index, { "selected_type" => :community} + assert_response :success + assert_select ".search-item", 6 + end + + should 'filter person by default environment' do + get :index, { "selected_type" => :person} + assert_response :success + assert_select ".search-item", 5 + end + end diff --git a/plugins/elasticsearch/test/unit/community_test.rb b/plugins/elasticsearch/test/unit/community_test.rb index 4a68f75..8729de3 100644 --- a/plugins/elasticsearch/test/unit/community_test.rb +++ b/plugins/elasticsearch/test/unit/community_test.rb @@ -21,4 +21,26 @@ class CommunityTest < ActiveSupport::TestCase end end + should 'respond with should method to return public community' do + assert Community.respond_to? :should + end + + should 'respond with especific sort' do + assert Community.respond_to? :especific_sort + end + + should 'respond with get_sort_by to order especific sort' do + assert Community.respond_to? :get_sort_by + end + + should 'return hash to sort by more_active' do + more_active_hash = {:activities_count => {order: :desc}} + assert_equal more_active_hash, Community.get_sort_by(:more_active) + end + + should 'return hash to sort by more_popular' do + more_popular_hash = {:members_count => {order: :desc}} + assert_equal more_popular_hash, Community.get_sort_by(:more_popular) + end + end diff --git a/plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb b/plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb index 87822d8..f4cb983 100644 --- a/plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb +++ b/plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb @@ -72,4 +72,9 @@ class ElasticsearchHelperTest < ActiveSupport::TestCase assert_equal ["Joana Abreu", "Joao Abreu"], result.map(&:name) end + should 'have sort in get_query return if has the option sort_by ' do + self.params= {} + assert get_query("", sort_by: :more_popular).keys.include?(:sort) + end + end diff --git a/plugins/elasticsearch/test/unit/event_test.rb b/plugins/elasticsearch/test/unit/event_test.rb index ecdc277..3725a48 100644 --- a/plugins/elasticsearch/test/unit/event_test.rb +++ b/plugins/elasticsearch/test/unit/event_test.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../test_helper" +require_relative '../../lib/nested_helper/profile' class EventTest < ActionController::TestCase @@ -21,4 +22,16 @@ class EventTest < ActionController::TestCase end end + should 'respond with should method to return public event' do + assert Event.respond_to? :should + end + + should 'respond with nested_filter' do + assert Event.respond_to? :nested_filter + end + + should 'have NestedProfile_filter in nested_filter' do + assert Event.nested_filter.include? NestedProfile.filter + end + end diff --git a/plugins/elasticsearch/test/unit/person_test.rb b/plugins/elasticsearch/test/unit/person_test.rb index c56d47a..fc9e98a 100644 --- a/plugins/elasticsearch/test/unit/person_test.rb +++ b/plugins/elasticsearch/test/unit/person_test.rb @@ -20,4 +20,26 @@ class PersonTest < ActionController::TestCase assert_equal indexed_fields(Person)[key][:type], value[:type] || 'string' end end + + should 'respond with should method to return public person' do + assert Person.respond_to? :should + end + + should 'respond with especific sort' do + assert Person.respond_to? :especific_sort + end + + should 'respond with get_sort_by to order especific sort' do + assert Person.respond_to? :get_sort_by + end + + should 'return hash to sort by more_active' do + more_active_hash = {:activities_count => {order: :desc}} + assert_equal more_active_hash, Person.get_sort_by(:more_active) + end + + should 'return hash to sort by more_popular' do + more_popular_hash = {:friends_count => {order: :desc}} + assert_equal more_popular_hash, Person.get_sort_by(:more_popular) + end end diff --git a/plugins/elasticsearch/test/unit/text_article_test.rb b/plugins/elasticsearch/test/unit/text_article_test.rb index 2354fac..f110f8d 100644 --- a/plugins/elasticsearch/test/unit/text_article_test.rb +++ b/plugins/elasticsearch/test/unit/text_article_test.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../test_helper" +require_relative '../../lib/nested_helper/profile' class TextArticleTest < ActionController::TestCase @@ -21,4 +22,34 @@ class TextArticleTest < ActionController::TestCase end end + should 'respond with should method to return public text_article' do + assert TextArticle.respond_to? :should + end + + should 'respond with especific sort' do + assert TextArticle.respond_to? :especific_sort + end + + should 'respond with get_sort_by to order especific sort' do + assert TextArticle.respond_to? :get_sort_by + end + + should 'return hash to sort by most commented' do + more_active_hash = {:comments_count => {order: :desc}} + assert_equal more_active_hash, TextArticle.get_sort_by(:more_comments) + end + + should 'return hash to sort by more popular' do + more_popular_hash = {:hits => {order: :desc}} + assert_equal more_popular_hash, TextArticle.get_sort_by(:more_popular) + end + + should 'respond with nested_filter' do + assert TextArticle.respond_to? :nested_filter + end + + should 'have NestedProfile_filter in nested_filter' do + assert TextArticle.nested_filter.include? NestedProfile.filter + end + end diff --git a/plugins/elasticsearch/test/unit/uploaded_file_test.rb b/plugins/elasticsearch/test/unit/uploaded_file_test.rb index 7a3ae19..997bff3 100644 --- a/plugins/elasticsearch/test/unit/uploaded_file_test.rb +++ b/plugins/elasticsearch/test/unit/uploaded_file_test.rb @@ -21,4 +21,16 @@ class UploadedFileTest < ActionController::TestCase end end + should 'respond with should method to return public text_article' do + assert TextArticle.respond_to? :should + end + + should 'respond with nested_filter' do + assert TextArticle.respond_to? :nested_filter + end + + should 'have NestedProfile_filter in nested_filter' do + assert TextArticle.nested_filter.include? NestedProfile.filter + end + end -- libgit2 0.21.2