Commit adc7e9beb80645bb76bd1fe78ff5abf96281255e
1 parent
6129c293
Exists in
master
and in
3 other branches
Multi env on SoftwareInfo scope search_by_query
Signed-off-by: Tallys Martins <tallysmartins@yahoo.com.br>
Showing
3 changed files
with
20 additions
and
4 deletions
Show diff stats
src/noosfero-spb/software_communities/lib/ext/search_controller.rb
| ... | ... | @@ -89,10 +89,11 @@ class SearchController |
| 89 | 89 | params[:query] ||= "" |
| 90 | 90 | visible_communities = visible_profiles(Community) |
| 91 | 91 | |
| 92 | - filtered_software_list = SoftwareInfo.search_by_query(params[:query]) | |
| 92 | + filtered_software_list = SoftwareInfo.search_by_query(params[:query], environment) | |
| 93 | 93 | |
| 94 | 94 | if params[:only_softwares] |
| 95 | 95 | params[:only_softwares].collect!{ |software_name| software_name.to_slug } |
| 96 | + #FIX-ME: This query is not appropriate | |
| 96 | 97 | filtered_software_list = SoftwareInfo.all.select{ |s| params[:only_softwares].include?(s.identifier) } |
| 97 | 98 | @public_software_selected = false |
| 98 | 99 | end | ... | ... |
src/noosfero-spb/software_communities/lib/software_info.rb
| ... | ... | @@ -13,15 +13,15 @@ class SoftwareInfo < ActiveRecord::Base |
| 13 | 13 | DatabaseDescription |
| 14 | 14 | ] |
| 15 | 15 | |
| 16 | - scope :search_by_query, lambda { |query = ""| | |
| 16 | + scope :search_by_query, lambda { |query = "", env = Environment.default| | |
| 17 | 17 | filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') |
| 18 | 18 | search_fields = SoftwareInfo.pg_search_plugin_fields |
| 19 | 19 | |
| 20 | 20 | if query.empty? |
| 21 | - SoftwareInfo.joins(:community).where("profiles.visible = ?", true) | |
| 21 | + SoftwareInfo.joins(:community).where("profiles.visible = ? AND environment_id = ? ", true, env.id) | |
| 22 | 22 | else |
| 23 | 23 | searchable_software_objects = SoftwareInfo.transform_list_in_methods_list(SEARCHABLE_SOFTWARE_CLASSES) |
| 24 | - includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ?", true) | |
| 24 | + includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ? AND environment_id = ?", true, env.id) | |
| 25 | 25 | end |
| 26 | 26 | } |
| 27 | 27 | ... | ... |
src/noosfero-spb/software_communities/test/unit/software_info_test.rb
| ... | ... | @@ -41,4 +41,19 @@ class SoftwareInfoValidationTest < ActiveSupport::TestCase |
| 41 | 41 | assert_equal @software_info.license_info.link, another_license_link |
| 42 | 42 | end |
| 43 | 43 | |
| 44 | + should "search softwares on the correct environment when multi environments available" do | |
| 45 | + software_info = create_software_info("soft1") | |
| 46 | + another_software_info = create_software_info("soft2") | |
| 47 | + other_env = Environment.create!(name: "sisp") | |
| 48 | + another_soft_profile = another_software_info.community | |
| 49 | + another_soft_profile.environment_id = other_env.id | |
| 50 | + another_soft_profile.save | |
| 51 | + | |
| 52 | + assert_equal 2, SoftwareInfo.count | |
| 53 | + assert_equal 1, SoftwareInfo.search_by_query("", Environment.default).count | |
| 54 | + assert_equal software_info, SoftwareInfo.search_by_query("", Environment.default).first | |
| 55 | + assert_equal 1, SoftwareInfo.search_by_query("", other_env).count | |
| 56 | + assert_equal true, SoftwareInfo.search_by_query("", other_env).includes?(another_software_info) | |
| 57 | + end | |
| 58 | + | |
| 44 | 59 | end | ... | ... |