Commit 02e6f4281637b84c37c887582c7f844de3efa6bd
1 parent
289597b9
Exists in
temp_soft_comm_refactoring
First step to refactor software catalog/search properly
Showing
5 changed files
with
231 additions
and
197 deletions
Show diff stats
src/noosfero-spb/software_communities/lib/ext/search_controller.rb
1 | -require_dependency 'search_controller' | ||
2 | - | ||
3 | -class SearchController | ||
4 | - | ||
5 | - def communities | ||
6 | - delete_communities = [] | ||
7 | - valid_communities_string = Community.get_valid_communities_string | ||
8 | - Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} | ||
9 | - | ||
10 | - @scope = visible_profiles(Community) | ||
11 | - @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty? | ||
12 | - | ||
13 | - full_text_search | ||
14 | - end | ||
15 | - | ||
16 | - def software_infos | ||
17 | - prepare_software_search_page | ||
18 | - results = filter_software_infos_list | ||
19 | - @software_count = results.count | ||
20 | - results = results.paginate(:per_page => @per_page, :page => params[:page]) | ||
21 | - @searches[@asset] = {:results => results} | ||
22 | - @search = results | ||
23 | - | ||
24 | - render :layout=>false if request.xhr? | ||
25 | - end | ||
26 | - | ||
27 | - protected | ||
28 | - | ||
29 | - def filter_communities_list | ||
30 | - unfiltered_list = visible_profiles(Community) | ||
31 | - | ||
32 | - unless params[:query].nil? | ||
33 | - unfiltered_list = unfiltered_list.select do |com| | ||
34 | - com.name.downcase =~ /#{params[:query].downcase}/ | ||
35 | - end | ||
36 | - end | ||
37 | - | ||
38 | - communities_list = [] | ||
39 | - unfiltered_list.each do |profile| | ||
40 | - if profile.class == Community && !profile.is_template? && yield(profile) | ||
41 | - communities_list << profile | ||
42 | - end | ||
43 | - end | ||
44 | - | ||
45 | - communities_list | ||
46 | - end | ||
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_community_list | ||
52 | - end | ||
53 | - | ||
54 | - def get_filter_category_ids | ||
55 | - category_ids = [] | ||
56 | - unless params[:selected_categories_id].blank? | ||
57 | - category_ids = params[:selected_categories_id] | ||
58 | - end | ||
59 | - category_ids.map(&:to_i) | ||
60 | - end | ||
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 | ||
87 | - | ||
88 | - filtered_software_list | ||
89 | - end | ||
90 | - | ||
91 | - def get_communities_list software_list | ||
92 | - filtered_community_list = [] | ||
93 | - software_list.each do |software| | ||
94 | - if !@public_software_selected || software.public_software? | ||
95 | - filtered_community_list << software.community unless software.community.nil? | ||
96 | - end | ||
97 | - end | ||
98 | - filtered_community_list | ||
99 | - end | ||
100 | - | ||
101 | - def sort_communities_list communities_list | ||
102 | - communities_list.sort! {|a, b| a.name.downcase <=> b.name.downcase} | ||
103 | - | ||
104 | - if params[:sort] && params[:sort] == "desc" | ||
105 | - communities_list.reverse! | ||
106 | - elsif params[:sort] && params[:sort] == "relevance" | ||
107 | - communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | ||
108 | - end | ||
109 | - communities_list | ||
110 | - end | ||
111 | - | ||
112 | - def prepare_software_search_page | ||
113 | - prepare_software_infos_params | ||
114 | - prepare_software_infos_message | ||
115 | - prepare_software_infos_category_groups | ||
116 | - prepare_software_infos_category_enable | ||
117 | - end | ||
118 | - | ||
119 | - def prepare_software_infos_params | ||
120 | - @asset = "software_communities_plugin/software_infos" | ||
121 | - @assets = [@asset] | ||
122 | - @titles["software_communities_plugin/software_infos"] = _("Result Search") | ||
123 | - @selected_categories_id = params[:selected_categories_id] | ||
124 | - @selected_categories_id ||= [] | ||
125 | - @selected_categories_id = @selected_categories_id.map(&:to_i) | ||
126 | - @all_selected = params[:software_type] == "all" | ||
127 | - @public_software_selected = !@all_selected | ||
128 | - @per_page = prepare_per_page | ||
129 | - end | ||
130 | - | ||
131 | - def prepare_per_page | ||
132 | - return 15 if params[:software_display].nil? | ||
133 | - | ||
134 | - if params[:software_display] == "all" | ||
135 | - SoftwareCommunitiesPlugin::SoftwareInfo.count | ||
136 | - else | ||
137 | - params[:software_display].to_i | ||
138 | - end | ||
139 | - end | ||
140 | - | ||
141 | - def prepare_software_infos_message | ||
142 | - @message_selected_options = "" | ||
143 | - | ||
144 | - @selected_categories = [] | ||
145 | - unless @selected_categories_id.empty? | ||
146 | - @message_selected_options = _("Selected options: ") | ||
147 | - | ||
148 | - @selected_categories = Category.find(@selected_categories_id) | ||
149 | - @message_selected_options += @selected_categories.collect { |category| | ||
150 | - "#{category.name}; " | ||
151 | - }.join() | ||
152 | - end | ||
153 | - end | ||
154 | - | ||
155 | - def prepare_software_infos_category_groups | ||
156 | - @categories = Category.software_categories.sort{|a, b| a.name <=> b.name} | ||
157 | - end | ||
158 | - | ||
159 | - def prepare_software_infos_category_enable | ||
160 | - @enabled_check_box = Hash.new | ||
161 | - categories = Category.software_categories | ||
162 | - | ||
163 | - categories.each do |category| | ||
164 | - if category.software_infos.count > 0 | ||
165 | - @enabled_check_box[category] = :enabled | ||
166 | - else | ||
167 | - @enabled_check_box[category] = :disabled | ||
168 | - end | ||
169 | - end | ||
170 | - end | ||
171 | -end | 1 | +# require_dependency 'search_controller' |
2 | + | ||
3 | +# class SearchController | ||
4 | + | ||
5 | +# def communities | ||
6 | +# delete_communities = [] | ||
7 | +# valid_communities_string = Community.get_valid_communities_string | ||
8 | +# Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} | ||
9 | + | ||
10 | +# @scope = visible_profiles(Community) | ||
11 | +# @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty? | ||
12 | + | ||
13 | +# full_text_search | ||
14 | +# end | ||
15 | + | ||
16 | +# def software_infos | ||
17 | +# prepare_software_search_page | ||
18 | +# results = filter_software_infos_list | ||
19 | +# @software_count = results.count | ||
20 | +# results = results.paginate(:per_page => @per_page, :page => params[:page]) | ||
21 | +# @searches[@asset] = {:results => results} | ||
22 | +# @search = results | ||
23 | + | ||
24 | +# render :layout=>false if request.xhr? | ||
25 | +# end | ||
26 | + | ||
27 | +# protected | ||
28 | + | ||
29 | +# def filter_communities_list | ||
30 | +# unfiltered_list = visible_profiles(Community) | ||
31 | + | ||
32 | +# unless params[:query].nil? | ||
33 | +# unfiltered_list = unfiltered_list.select do |com| | ||
34 | +# com.name.downcase =~ /#{params[:query].downcase}/ | ||
35 | +# end | ||
36 | +# end | ||
37 | + | ||
38 | +# communities_list = [] | ||
39 | +# unfiltered_list.each do |profile| | ||
40 | +# if profile.class == Community && !profile.is_template? && yield(profile) | ||
41 | +# communities_list << profile | ||
42 | +# end | ||
43 | +# end | ||
44 | + | ||
45 | +# communities_list | ||
46 | +# end | ||
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_community_list | ||
52 | +# end | ||
53 | + | ||
54 | +# def get_filter_category_ids | ||
55 | +# category_ids = [] | ||
56 | +# unless params[:selected_categories_id].blank? | ||
57 | +# category_ids = params[:selected_categories_id] | ||
58 | +# end | ||
59 | +# category_ids.map(&:to_i) | ||
60 | +# end | ||
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 | ||
87 | + | ||
88 | +# filtered_software_list | ||
89 | +# end | ||
90 | + | ||
91 | +# def get_communities_list software_list | ||
92 | +# filtered_community_list = [] | ||
93 | +# software_list.each do |software| | ||
94 | +# if !@public_software_selected || software.public_software? | ||
95 | +# filtered_community_list << software.community unless software.community.nil? | ||
96 | +# end | ||
97 | +# end | ||
98 | +# filtered_community_list | ||
99 | +# end | ||
100 | + | ||
101 | +# def sort_communities_list communities_list | ||
102 | +# communities_list.sort! {|a, b| a.name.downcase <=> b.name.downcase} | ||
103 | + | ||
104 | +# if params[:sort] && params[:sort] == "desc" | ||
105 | +# communities_list.reverse! | ||
106 | +# elsif params[:sort] && params[:sort] == "relevance" | ||
107 | +# communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | ||
108 | +# end | ||
109 | +# communities_list | ||
110 | +# end | ||
111 | + | ||
112 | +# def prepare_software_search_page | ||
113 | +# prepare_software_infos_params | ||
114 | +# prepare_software_infos_message | ||
115 | +# prepare_software_infos_category_groups | ||
116 | +# prepare_software_infos_category_enable | ||
117 | +# end | ||
118 | + | ||
119 | +# def prepare_software_infos_params | ||
120 | +# @asset = "software_communities_plugin/software_infos" | ||
121 | +# @assets = [@asset] | ||
122 | +# @titles["software_communities_plugin/software_infos"] = _("Result Search") | ||
123 | +# @selected_categories_id = params[:selected_categories_id] | ||
124 | +# @selected_categories_id ||= [] | ||
125 | +# @selected_categories_id = @selected_categories_id.map(&:to_i) | ||
126 | +# @all_selected = params[:software_type] == "all" | ||
127 | +# @public_software_selected = !@all_selected | ||
128 | +# @per_page = prepare_per_page | ||
129 | +# end | ||
130 | + | ||
131 | +# def prepare_per_page | ||
132 | +# return 15 if params[:software_display].nil? | ||
133 | + | ||
134 | +# if params[:software_display] == "all" | ||
135 | +# SoftwareCommunitiesPlugin::SoftwareInfo.count | ||
136 | +# else | ||
137 | +# params[:software_display].to_i | ||
138 | +# end | ||
139 | +# end | ||
140 | + | ||
141 | +# def prepare_software_infos_message | ||
142 | +# @message_selected_options = "" | ||
143 | + | ||
144 | +# @selected_categories = [] | ||
145 | +# unless @selected_categories_id.empty? | ||
146 | +# @message_selected_options = _("Selected options: ") | ||
147 | + | ||
148 | +# @selected_categories = Category.find(@selected_categories_id) | ||
149 | +# @message_selected_options += @selected_categories.collect { |category| | ||
150 | +# "#{category.name}; " | ||
151 | +# }.join() | ||
152 | +# end | ||
153 | +# end | ||
154 | + | ||
155 | +# def prepare_software_infos_category_groups | ||
156 | +# @categories = Category.software_categories.sort{|a, b| a.name <=> b.name} | ||
157 | +# end | ||
158 | + | ||
159 | +# def prepare_software_infos_category_enable | ||
160 | +# @enabled_check_box = Hash.new | ||
161 | +# categories = Category.software_categories | ||
162 | + | ||
163 | +# categories.each do |category| | ||
164 | +# if category.software_infos.count > 0 | ||
165 | +# @enabled_check_box[category] = :enabled | ||
166 | +# else | ||
167 | +# @enabled_check_box[category] = :disabled | ||
168 | +# end | ||
169 | +# end | ||
170 | +# end | ||
171 | +# end |
src/noosfero-spb/software_communities/lib/software_communities_plugin.rb
@@ -114,6 +114,38 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | @@ -114,6 +114,38 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | ||
114 | person.has_permission_without_plugins?(permission, target) | 114 | person.has_permission_without_plugins?(permission, target) |
115 | end | 115 | end |
116 | 116 | ||
117 | + def filter_search_scope(scope, asset) | ||
118 | + # Select only communities that are not related to any software. | ||
119 | + if asset.to_s == 'communities' | ||
120 | + scope = scope.joins('LEFT OUTER JOIN software_communities_plugin_software_infos as softwares | ||
121 | + ON profiles.id = softwares.community_id'). | ||
122 | + where('softwares.community_id IS NULL') | ||
123 | + | ||
124 | + return [scope, asset] | ||
125 | + # Select only communities that are related to a software. | ||
126 | + elsif asset.to_s == 'software_communities_plugin/software_infos' | ||
127 | + scope = scope.joins('INNER JOIN software_communities_plugin_software_infos as softwares | ||
128 | + ON profiles.id = softwares.community_id') | ||
129 | + return [scope, asset] | ||
130 | + # Go with the flow. | ||
131 | + else | ||
132 | + return [scope, asset] | ||
133 | + end | ||
134 | + end | ||
135 | + | ||
136 | + def new_search_assets | ||
137 | + class_name_underscored = "software_communities_plugin/software_infos" | ||
138 | + block = proc do | ||
139 | + @scope = visible_profiles(Community) | ||
140 | + @asset = class_name_underscored | ||
141 | + @assets = [@asset] | ||
142 | + @titles[class_name_underscored] = _("Result Search") | ||
143 | + params[:limit] = 24 | ||
144 | + end | ||
145 | + | ||
146 | + [{:name => 'software', :block => block, :common_profile_list_block => class_name_underscored}] | ||
147 | + end | ||
148 | + | ||
117 | protected | 149 | protected |
118 | 150 | ||
119 | def software_info_transaction | 151 | def software_info_transaction |
src/noosfero-spb/software_communities/views/search/_catalog_filter.html.erb
@@ -4,11 +4,13 @@ | @@ -4,11 +4,13 @@ | ||
4 | <div id="filter-categories-catalog"><h4> <%= _("Categories") %> </h4></div> | 4 | <div id="filter-categories-catalog"><h4> <%= _("Categories") %> </h4></div> |
5 | <div id="group-categories"> | 5 | <div id="group-categories"> |
6 | <ul> | 6 | <ul> |
7 | - <% @categories.each do |category| %> | ||
8 | - <li> | ||
9 | - <%= check_box_tag("selected_categories_id[]", category.id, @selected_categories_id.include?(category.id), :class => "categories-catalog", @enabled_check_box[category] => "true") %> | ||
10 | - <span><%= _("#{category.name}") %></span> | ||
11 | - </li> | 7 | + <% unless @categories.blank? %> |
8 | + <% @categories.each do |category| %> | ||
9 | + <li> | ||
10 | + <%= check_box_tag("selected_categories_id[]", category.id, @selected_categories_id.include?(category.id), :class => "categories-catalog", @enabled_check_box[category] => "true") %> | ||
11 | + <span><%= _("#{category.name}") %></span> | ||
12 | + </li> | ||
13 | + <% end %> | ||
12 | <% end %> | 14 | <% end %> |
13 | </ul> | 15 | </ul> |
14 | </div> | 16 | </div> |
src/noosfero-spb/software_communities/views/search/software_infos.html.erb
@@ -1,21 +0,0 @@ | @@ -1,21 +0,0 @@ | ||
1 | -<h2>Catálogo de Software Público</h2> | ||
2 | - | ||
3 | -<div id="software-search-container"> | ||
4 | - <%= search_page_title( @titles[@asset], @category ) %> | ||
5 | - | ||
6 | - <%= render :partial => 'software_search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %> | ||
7 | - | ||
8 | - <% if @asset == :product %> | ||
9 | - <%= javascript_tag do %> | ||
10 | - jQuery('.search-product-price-details').altBeautify(); | ||
11 | - <% end %> | ||
12 | - <% end %> | ||
13 | -</div> | ||
14 | - | ||
15 | -<%= render partial:"catalog_result_list" %> | ||
16 | - | ||
17 | -<div id="software-pagination"> | ||
18 | - <% if params[:display] != 'map' %> | ||
19 | - <%= pagination_links @searches[@asset][:results] %> | ||
20 | - <% end %> | ||
21 | -</div> |
src/noosfero-spb/software_communities/views/search/softwares.html.erb
0 → 100644
@@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
1 | +<h2>Catálogo de Software Público</h2> | ||
2 | + | ||
3 | +<div id="software-search-container"> | ||
4 | + <%= search_page_title( @titles[@asset], @category ) %> | ||
5 | + | ||
6 | + <%= render :partial => 'software_search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %> | ||
7 | + | ||
8 | + <% if @asset == :product %> | ||
9 | + <%= javascript_tag do %> | ||
10 | + jQuery('.search-product-price-details').altBeautify(); | ||
11 | + <% end %> | ||
12 | + <% end %> | ||
13 | +</div> | ||
14 | + | ||
15 | +<%= render partial:"catalog_result_list" %> | ||
16 | + | ||
17 | +<div id="software-pagination"> | ||
18 | + <% if params[:display] != 'map' %> | ||
19 | + <%= pagination_links @searches[@asset][:results] %> | ||
20 | + <% end %> | ||
21 | +</div> |