Commit 12a26bee066ee360d963b9f6ca64a8c0c2b7ff67
1 parent
fc273ce5
Exists in
temp_soft_comm_refactoring
Refactoring search controller from software communities
Showing
3 changed files
with
89 additions
and
58 deletions
Show diff stats
src/noosfero-spb/software_communities/lib/ext/search_controller.rb
| @@ -45,11 +45,11 @@ class SearchController | @@ -45,11 +45,11 @@ class SearchController | ||
| 45 | communities_list | 45 | communities_list |
| 46 | end | 46 | end |
| 47 | 47 | ||
| 48 | - def filter_software_infos_list | ||
| 49 | - filtered_software_list = get_filtered_software_list | ||
| 50 | - # filtered_community_list = get_communities_list(filtered_software_list) | ||
| 51 | - sort_communities_list filtered_software_list | ||
| 52 | - end | 48 | + # def filter_software_infos_list |
| 49 | + # filtered_software_list = get_community_ids_from_filtered_software | ||
| 50 | + # filtered_community_list = get_communities_list(filtered_software_list) | ||
| 51 | + # sort_communities_list filtered_community_list | ||
| 52 | + # end | ||
| 53 | 53 | ||
| 54 | def get_filter_category_ids | 54 | def get_filter_category_ids |
| 55 | category_ids = [] | 55 | category_ids = [] |
| @@ -59,46 +59,16 @@ class SearchController | @@ -59,46 +59,16 @@ class SearchController | ||
| 59 | category_ids.map(&:to_i) | 59 | category_ids.map(&:to_i) |
| 60 | end | 60 | end |
| 61 | 61 | ||
| 62 | - def get_filtered_software_list | ||
| 63 | - params[:query] ||= "" | ||
| 64 | - visible_communities = visible_profiles(Community) | ||
| 65 | - | ||
| 66 | - filtered_software_list = SoftwareCommunitiesPlugin::SoftwareInfo.search_by_query(params[:query]) | ||
| 67 | - | ||
| 68 | - if params[:only_softwares] | ||
| 69 | - params[:only_softwares].collect!{ |software_name| software_name.to_slug } | ||
| 70 | - filtered_software_list = SoftwareCommunitiesPlugin::SoftwareInfo.all.select{ |s| params[:only_softwares].include?(s.identifier) } | ||
| 71 | - @public_software_selected = false | ||
| 72 | - end | ||
| 73 | - | ||
| 74 | - filtered_software_list.select!{ |software| visible_communities.include?(software.community) } | ||
| 75 | - category_ids = get_filter_category_ids | ||
| 76 | - | ||
| 77 | - unless category_ids.empty? | ||
| 78 | - filtered_software_list.select! do |software| | ||
| 79 | - if software.nil? || software.community.nil? | ||
| 80 | - false | ||
| 81 | - else | ||
| 82 | - result_ids = (software.community.category_ids & category_ids).sort | ||
| 83 | - result_ids == category_ids.sort | ||
| 84 | - end | ||
| 85 | - end | ||
| 86 | - end | 62 | + # def sort_communities_list communities_list |
| 63 | + # communities_list.sort! {|a, b| a.name.downcase <=> b.name.downcase} | ||
| 87 | 64 | ||
| 88 | - ids = filtered_software_list.pluck(:id) | ||
| 89 | - visible_communities.joins(:software_infos).where("software_infos.id IN ?", ids) | ||
| 90 | - end | ||
| 91 | - | ||
| 92 | - def sort_communities_list communities_list | ||
| 93 | - communities_list.order('name') | ||
| 94 | - | ||
| 95 | - if params[:sort] && params[:sort] == "desc" | ||
| 96 | - communities_list.order('name DESC') | ||
| 97 | - elsif params[:sort] && params[:sort] == "relevance" | ||
| 98 | - communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | ||
| 99 | - end | ||
| 100 | - communities_list | ||
| 101 | - end | 65 | + # if params[:sort] && params[:sort] == "desc" |
| 66 | + # communities_list.reverse! | ||
| 67 | + # elsif params[:sort] && params[:sort] == "relevance" | ||
| 68 | + # communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | ||
| 69 | + # end | ||
| 70 | + # communities_list | ||
| 71 | + # end | ||
| 102 | 72 | ||
| 103 | def prepare_software_search_page | 73 | def prepare_software_search_page |
| 104 | prepare_software_infos_params | 74 | prepare_software_infos_params |
src/noosfero-spb/software_communities/lib/software_communities_plugin.rb
| @@ -123,12 +123,12 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | @@ -123,12 +123,12 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | ||
| 123 | where('softwares.community_id IS NULL') | 123 | where('softwares.community_id IS NULL') |
| 124 | 124 | ||
| 125 | return [scope, asset] | 125 | return [scope, asset] |
| 126 | - # Select only communities that are related to a software. | ||
| 127 | - elsif asset.to_s == 'software_communities_plugin/software_infos' | ||
| 128 | - scope = scope.joins('INNER JOIN software_communities_plugin_software_infos as softwares | ||
| 129 | - ON profiles.id = softwares.community_id') | ||
| 130 | - return [scope, asset] | ||
| 131 | - # Go with the flow. | 126 | + # # Select only communities that are related to a software. |
| 127 | + # elsif asset.to_s == 'software_communities_plugin/software_infos' | ||
| 128 | + # scope = scope.joins('INNER JOIN software_communities_plugin_software_infos as softwares | ||
| 129 | + # ON profiles.id = softwares.community_id') | ||
| 130 | + # return [scope, asset] | ||
| 131 | + # # Go with the flow. | ||
| 132 | else | 132 | else |
| 133 | return [scope, asset] | 133 | return [scope, asset] |
| 134 | end | 134 | end |
| @@ -138,7 +138,6 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | @@ -138,7 +138,6 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | ||
| 138 | class_name_underscored = "software_communities_plugin/software_infos" | 138 | class_name_underscored = "software_communities_plugin/software_infos" |
| 139 | block = proc do | 139 | block = proc do |
| 140 | prepare_software_search_page | 140 | prepare_software_search_page |
| 141 | - results = filter_software_infos_list | ||
| 142 | @scope = visible_profiles(Community) | 141 | @scope = visible_profiles(Community) |
| 143 | @asset = class_name_underscored | 142 | @asset = class_name_underscored |
| 144 | @assets = [@asset] | 143 | @assets = [@asset] |
| @@ -156,8 +155,62 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | @@ -156,8 +155,62 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | ||
| 156 | [{:name => 'software', :block => block, :common_profile_list_block => class_name_underscored}] | 155 | [{:name => 'software', :block => block, :common_profile_list_block => class_name_underscored}] |
| 157 | end | 156 | end |
| 158 | 157 | ||
| 158 | + def find_by_contents(asset, scope, query, paginate_options={}, options={}) | ||
| 159 | + if query && asset.to_s == 'software_communities_plugin/software_infos' | ||
| 160 | + community_ids = get_community_ids_from_filtered_software(query) | ||
| 161 | + scope = Community.where(:id => community_ids) | ||
| 162 | + end | ||
| 163 | + | ||
| 164 | + scope.order(:name) | ||
| 165 | + # scope = scope.like_search(query, options) unless query.blank? | ||
| 166 | + # scope = scope.send(options[:filter]) unless options[:filter].blank? | ||
| 167 | + {:results => scope.paginate(paginate_options)} | ||
| 168 | + end | ||
| 169 | + | ||
| 159 | protected | 170 | protected |
| 160 | 171 | ||
| 172 | + def get_community_ids_from_filtered_software(query) | ||
| 173 | + filtered_software_list = SoftwareCommunitiesPlugin::SoftwareInfo.search_by_query(query) | ||
| 174 | + | ||
| 175 | + if context.params[:only_softwares] | ||
| 176 | + context.params[:only_softwares].collect!{ |software_name| software_name.to_slug } | ||
| 177 | + filtered_software_list = SoftwareCommunitiesPlugin::SoftwareInfo.all.select{ |s| context.params[:only_softwares].include?(s.identifier) } | ||
| 178 | + @public_software_selected = false | ||
| 179 | + end | ||
| 180 | + | ||
| 181 | + category_ids = get_filter_category_ids | ||
| 182 | + | ||
| 183 | + unless category_ids.empty? | ||
| 184 | + filtered_software_list.select! do |software| | ||
| 185 | + if software.nil? || software.community.nil? | ||
| 186 | + false | ||
| 187 | + else | ||
| 188 | + result_ids = (software.community.category_ids & category_ids).sort | ||
| 189 | + result_ids == category_ids.sort | ||
| 190 | + end | ||
| 191 | + end | ||
| 192 | + end | ||
| 193 | + | ||
| 194 | + community_ids = [] | ||
| 195 | + | ||
| 196 | + filtered_software_list.each do |software| | ||
| 197 | + community_ids << software.community_id | ||
| 198 | + end | ||
| 199 | + | ||
| 200 | + community_ids | ||
| 201 | + end | ||
| 202 | + | ||
| 203 | + def sort_communities_list communities_list | ||
| 204 | + communities_list.sort! {|a, b| a.name.downcase <=> b.name.downcase} | ||
| 205 | + | ||
| 206 | + if params[:sort] && params[:sort] == "desc" | ||
| 207 | + communities_list.reverse! | ||
| 208 | + elsif params[:sort] && params[:sort] == "relevance" | ||
| 209 | + communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | ||
| 210 | + end | ||
| 211 | + communities_list | ||
| 212 | + end | ||
| 213 | + | ||
| 161 | def software_info_transaction | 214 | def software_info_transaction |
| 162 | SoftwareCommunitiesPlugin::SoftwareInfo.transaction do | 215 | SoftwareCommunitiesPlugin::SoftwareInfo.transaction do |
| 163 | context.profile. | 216 | context.profile. |
src/noosfero-spb/software_communities/models/software_communities_plugin/software_info.rb
| @@ -13,6 +13,13 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | @@ -13,6 +13,13 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | ||
| 13 | SoftwareCommunitiesPlugin::DatabaseDescription | 13 | SoftwareCommunitiesPlugin::DatabaseDescription |
| 14 | ] | 14 | ] |
| 15 | 15 | ||
| 16 | + SEARCHABLE_SOFTWARE_CLASSES_ATTRIBUTES = { | ||
| 17 | + SoftwareCommunitiesPlugin::SoftwareInfo => "software_info", | ||
| 18 | + Community => "community", | ||
| 19 | + SoftwareCommunitiesPlugin::ProgrammingLanguage => "programming_language", | ||
| 20 | + SoftwareCommunitiesPlugin::DatabaseDescription => "database_description" | ||
| 21 | + } | ||
| 22 | + | ||
| 16 | scope :search_by_query, lambda { |query = ""| | 23 | scope :search_by_query, lambda { |query = ""| |
| 17 | filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') | 24 | filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') |
| 18 | search_fields = SoftwareCommunitiesPlugin::SoftwareInfo.pg_search_plugin_fields | 25 | search_fields = SoftwareCommunitiesPlugin::SoftwareInfo.pg_search_plugin_fields |
| @@ -20,7 +27,8 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | @@ -20,7 +27,8 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | ||
| 20 | if query.empty? | 27 | if query.empty? |
| 21 | SoftwareCommunitiesPlugin::SoftwareInfo.joins(:community).where("profiles.visible = ?", true) | 28 | SoftwareCommunitiesPlugin::SoftwareInfo.joins(:community).where("profiles.visible = ?", true) |
| 22 | else | 29 | else |
| 23 | - searchable_software_objects = SoftwareCommunitiesPlugin::SoftwareInfo.transform_list_in_methods_list(SEARCHABLE_SOFTWARE_CLASSES) | 30 | + searchable_software_objects = SoftwareCommunitiesPlugin::SoftwareInfo.transform_list_in_methods_list(SEARCHABLE_SOFTWARE_CLASSES_ATTRIBUTES) |
| 31 | + | ||
| 24 | includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ?", true) | 32 | includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ?", true) |
| 25 | end | 33 | end |
| 26 | } | 34 | } |
| @@ -28,11 +36,11 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | @@ -28,11 +36,11 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | ||
| 28 | def self.transform_list_in_methods_list list | 36 | def self.transform_list_in_methods_list list |
| 29 | methods_list = [] | 37 | methods_list = [] |
| 30 | 38 | ||
| 31 | - list.each do |element| | ||
| 32 | - if SoftwareCommunitiesPlugin::SoftwareInfo.instance_methods.include?(element.to_s.underscore.to_sym) | ||
| 33 | - methods_list << element.to_s.underscore.to_sym | ||
| 34 | - elsif SoftwareCommunitiesPlugin::SoftwareInfo.instance_methods.include?(element.to_s.underscore.pluralize.to_sym) | ||
| 35 | - methods_list << element.to_s.underscore.pluralize.to_sym | 39 | + list.each do |klass, method| |
| 40 | + if SoftwareCommunitiesPlugin::SoftwareInfo.instance_methods.include?(method.to_sym) | ||
| 41 | + methods_list << method.to_sym | ||
| 42 | + elsif SoftwareCommunitiesPlugin::SoftwareInfo.instance_methods.include?(method.pluralize.to_sym) | ||
| 43 | + methods_list << method.pluralize.to_sym | ||
| 36 | end | 44 | end |
| 37 | end | 45 | end |
| 38 | 46 | ||
| @@ -70,7 +78,7 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | @@ -70,7 +78,7 @@ class SoftwareCommunitiesPlugin::SoftwareInfo < ActiveRecord::Base | ||
| 70 | 78 | ||
| 71 | has_many :libraries, :dependent => :destroy, :class_name => 'SoftwareCommunitiesPlugin::Library' | 79 | has_many :libraries, :dependent => :destroy, :class_name => 'SoftwareCommunitiesPlugin::Library' |
| 72 | has_many :software_databases, :class_name => 'SoftwareCommunitiesPlugin::SoftwareDatabase' | 80 | has_many :software_databases, :class_name => 'SoftwareCommunitiesPlugin::SoftwareDatabase' |
| 73 | - has_many :database_descriptions, :class_name => 'SoftwareCommunitiesPlugin::SoftwareDatabaseDescription', :through => :software_databases | 81 | + has_many :database_descriptions, :class_name => 'SoftwareCommunitiesPlugin::DatabaseDescription', :through => :software_databases |
| 74 | has_many :software_languages, :class_name => 'SoftwareCommunitiesPlugin::SoftwareLanguage' | 82 | has_many :software_languages, :class_name => 'SoftwareCommunitiesPlugin::SoftwareLanguage' |
| 75 | has_many :operating_systems, :class_name => 'SoftwareCommunitiesPlugin::OperatingSystem' | 83 | has_many :operating_systems, :class_name => 'SoftwareCommunitiesPlugin::OperatingSystem' |
| 76 | has_many :programming_languages, :class_name => 'SoftwareCommunitiesPlugin::ProgrammingLanguage', :through => :software_languages | 84 | has_many :programming_languages, :class_name => 'SoftwareCommunitiesPlugin::ProgrammingLanguage', :through => :software_languages |