Commit d6de9190770c486013d5aa42578ad6550d553d3b
1 parent
f0335103
Exists in
master
and in
28 other branches
Add order_by helper and parameter
Showing
5 changed files
with
23 additions
and
5 deletions
Show diff stats
app/controllers/public/search_controller.rb
| @@ -89,13 +89,13 @@ class SearchController < PublicController | @@ -89,13 +89,13 @@ class SearchController < PublicController | ||
| 89 | # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop | 89 | # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop |
| 90 | 90 | ||
| 91 | if !@query.blank? || @region && !params[:radius].blank? | 91 | if !@query.blank? || @region && !params[:radius].blank? |
| 92 | - ret = @noosfero_finder.find(asset, @query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all})) | 92 | + ret = @noosfero_finder.find(asset, @query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month], params[:order_by]).merge({:limit => :all})) |
| 93 | @result_ids = ret.is_a?(Hash) ? ret[:results] : ret | 93 | @result_ids = ret.is_a?(Hash) ? ret[:results] : ret |
| 94 | end | 94 | end |
| 95 | 95 | ||
| 96 | end | 96 | end |
| 97 | 97 | ||
| 98 | - def calculate_find_options(asset, limit, page, product_category, region, radius, year, month) | 98 | + def calculate_find_options(asset, limit, page, product_category, region, radius, year, month, solr_order) |
| 99 | result = { :product_category => product_category, :per_page => limit, :page => page } | 99 | result = { :product_category => product_category, :per_page => limit, :page => page } |
| 100 | if [:enterprises, :people, :products].include?(asset) && region | 100 | if [:enterprises, :people, :products].include?(asset) && region |
| 101 | result.merge!(:within => radius, :region => region.id) | 101 | result.merge!(:within => radius, :region => region.id) |
| @@ -111,6 +111,8 @@ class SearchController < PublicController | @@ -111,6 +111,8 @@ class SearchController < PublicController | ||
| 111 | :browse => params[:facet] ? params[:facet].map{ |k,v| k.to_s+':'+v.to_s} : ''}) | 111 | :browse => params[:facet] ? params[:facet].map{ |k,v| k.to_s+':'+v.to_s} : ''}) |
| 112 | end | 112 | end |
| 113 | 113 | ||
| 114 | + result[:order_by] = solr_order | ||
| 115 | + | ||
| 114 | result | 116 | result |
| 115 | end | 117 | end |
| 116 | 118 | ||
| @@ -169,7 +171,7 @@ class SearchController < PublicController | @@ -169,7 +171,7 @@ class SearchController < PublicController | ||
| 169 | 171 | ||
| 170 | where_to_search.select { |key,description| @searching[key] }.each do |key, description| | 172 | where_to_search.select { |key,description| @searching[key] }.each do |key, description| |
| 171 | @order << key | 173 | @order << key |
| 172 | - find_options = calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]); | 174 | + find_options = calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius], params[:year], params[:month], params[:order_by]); |
| 173 | ret = @noosfero_finder.find(key, @query, find_options) | 175 | ret = @noosfero_finder.find(key, @query, find_options) |
| 174 | @results[key] = ret.is_a?(Hash) ? ret[:results] : ret | 176 | @results[key] = ret.is_a?(Hash) ? ret[:results] : ret |
| 175 | @facets[key] = ret.is_a?(Hash) ? ret[:facets] : {} | 177 | @facets[key] = ret.is_a?(Hash) ? ret[:facets] : {} |
app/helpers/search_helper.rb
| @@ -134,4 +134,11 @@ module SearchHelper | @@ -134,4 +134,11 @@ module SearchHelper | ||
| 134 | asset_class(asset).table_name | 134 | asset_class(asset).table_name |
| 135 | end | 135 | end |
| 136 | 136 | ||
| 137 | + def order_by(asset, options) | ||
| 138 | + content_tag('div', _('Order by ') + | ||
| 139 | + select_tag(asset.to_s + '[order]', options_for_select(options, params[:order_by]), | ||
| 140 | + {:onchange => "window.location=jQuery.param.querystring(window.location.href, { 'order_by' : this.options[this.selectedIndex].value})"}), | ||
| 141 | + :class => "search-ordering") | ||
| 142 | + end | ||
| 143 | + | ||
| 137 | end | 144 | end |
app/models/category_finder.rb
| @@ -29,10 +29,13 @@ class CategoryFinder | @@ -29,10 +29,13 @@ class CategoryFinder | ||
| 29 | 29 | ||
| 30 | if query.blank? | 30 | if query.blank? |
| 31 | options.delete(:facets) | 31 | options.delete(:facets) |
| 32 | + options.delete(:order_by) | ||
| 33 | + | ||
| 32 | asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) | 34 | asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) |
| 33 | else | 35 | else |
| 34 | pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | 36 | pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} |
| 35 | - solr_options = {:facets => options.delete(:facets)} | 37 | + solr_options = {:facets => options.delete(:facets), :order => options.delete(:order_by)} |
| 38 | + | ||
| 36 | asset_class(asset).find_by_contents(query, pg_options, solr_options, options_for_find(asset_class(asset), options, date_range))[:results] | 39 | asset_class(asset).find_by_contents(query, pg_options, solr_options, options_for_find(asset_class(asset), options, date_range))[:results] |
| 37 | end | 40 | end |
| 38 | end | 41 | end |
app/models/environment_finder.rb
| @@ -23,6 +23,7 @@ class EnvironmentFinder | @@ -23,6 +23,7 @@ class EnvironmentFinder | ||
| 23 | 23 | ||
| 24 | if query.blank? | 24 | if query.blank? |
| 25 | options.delete(:facets) | 25 | options.delete(:facets) |
| 26 | + options.delete(:order_by) | ||
| 26 | 27 | ||
| 27 | # FIXME this test is in more than one place | 28 | # FIXME this test is in more than one place |
| 28 | if finder_method == 'paginate' | 29 | if finder_method == 'paginate' |
| @@ -51,7 +52,8 @@ class EnvironmentFinder | @@ -51,7 +52,8 @@ class EnvironmentFinder | ||
| 51 | end | 52 | end |
| 52 | else | 53 | else |
| 53 | pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | 54 | pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} |
| 54 | - solr_options = {:facets => options.delete(:facets)} | 55 | + solr_options = {:facets => options.delete(:facets), :order => options.delete(:order_by)} |
| 56 | + | ||
| 55 | if product_category && asset == :products | 57 | if product_category && asset == :products |
| 56 | # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source | 58 | # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source |
| 57 | ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id })) | 59 | ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id })) |
public/stylesheets/application.css
| @@ -1775,6 +1775,10 @@ input.disabled { | @@ -1775,6 +1775,10 @@ input.disabled { | ||
| 1775 | filter-opacity: 50%; | 1775 | filter-opacity: 50%; |
| 1776 | } | 1776 | } |
| 1777 | 1777 | ||
| 1778 | +.search-ordering { | ||
| 1779 | + float: right; | ||
| 1780 | +} | ||
| 1781 | + | ||
| 1778 | .search-relevance { | 1782 | .search-relevance { |
| 1779 | font-size: smaller; | 1783 | font-size: smaller; |
| 1780 | font-style: italic; | 1784 | font-style: italic; |