From adc7e9beb80645bb76bd1fe78ff5abf96281255e Mon Sep 17 00:00:00 2001 From: Tallys Martins Date: Tue, 12 Jan 2016 02:17:30 -0200 Subject: [PATCH] Multi env on SoftwareInfo scope search_by_query --- src/noosfero-spb/software_communities/lib/ext/search_controller.rb | 3 ++- src/noosfero-spb/software_communities/lib/software_info.rb | 6 +++--- src/noosfero-spb/software_communities/test/unit/software_info_test.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/noosfero-spb/software_communities/lib/ext/search_controller.rb b/src/noosfero-spb/software_communities/lib/ext/search_controller.rb index 8d0c7bc..2c02af9 100644 --- a/src/noosfero-spb/software_communities/lib/ext/search_controller.rb +++ b/src/noosfero-spb/software_communities/lib/ext/search_controller.rb @@ -89,10 +89,11 @@ class SearchController params[:query] ||= "" visible_communities = visible_profiles(Community) - filtered_software_list = SoftwareInfo.search_by_query(params[:query]) + filtered_software_list = SoftwareInfo.search_by_query(params[:query], environment) if params[:only_softwares] params[:only_softwares].collect!{ |software_name| software_name.to_slug } + #FIX-ME: This query is not appropriate filtered_software_list = SoftwareInfo.all.select{ |s| params[:only_softwares].include?(s.identifier) } @public_software_selected = false end diff --git a/src/noosfero-spb/software_communities/lib/software_info.rb b/src/noosfero-spb/software_communities/lib/software_info.rb index 02fb072..a9816f2 100644 --- a/src/noosfero-spb/software_communities/lib/software_info.rb +++ b/src/noosfero-spb/software_communities/lib/software_info.rb @@ -13,15 +13,15 @@ class SoftwareInfo < ActiveRecord::Base DatabaseDescription ] - scope :search_by_query, lambda { |query = ""| + scope :search_by_query, lambda { |query = "", env = Environment.default| filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') search_fields = SoftwareInfo.pg_search_plugin_fields if query.empty? - SoftwareInfo.joins(:community).where("profiles.visible = ?", true) + SoftwareInfo.joins(:community).where("profiles.visible = ? AND environment_id = ? ", true, env.id) else searchable_software_objects = SoftwareInfo.transform_list_in_methods_list(SEARCHABLE_SOFTWARE_CLASSES) - includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ?", true) + includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ? AND environment_id = ?", true, env.id) end } diff --git a/src/noosfero-spb/software_communities/test/unit/software_info_test.rb b/src/noosfero-spb/software_communities/test/unit/software_info_test.rb index 1fa477a..bd5ea14 100644 --- a/src/noosfero-spb/software_communities/test/unit/software_info_test.rb +++ b/src/noosfero-spb/software_communities/test/unit/software_info_test.rb @@ -41,4 +41,19 @@ class SoftwareInfoValidationTest < ActiveSupport::TestCase assert_equal @software_info.license_info.link, another_license_link end + should "search softwares on the correct environment when multi environments available" do + software_info = create_software_info("soft1") + another_software_info = create_software_info("soft2") + other_env = Environment.create!(name: "sisp") + another_soft_profile = another_software_info.community + another_soft_profile.environment_id = other_env.id + another_soft_profile.save + + assert_equal 2, SoftwareInfo.count + assert_equal 1, SoftwareInfo.search_by_query("", Environment.default).count + assert_equal software_info, SoftwareInfo.search_by_query("", Environment.default).first + assert_equal 1, SoftwareInfo.search_by_query("", other_env).count + assert_equal true, SoftwareInfo.search_by_query("", other_env).includes?(another_software_info) + end + end -- libgit2 0.21.2