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 | 89 | # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop |
| 90 | 90 | |
| 91 | 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 | 93 | @result_ids = ret.is_a?(Hash) ? ret[:results] : ret |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | 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 | 99 | result = { :product_category => product_category, :per_page => limit, :page => page } |
| 100 | 100 | if [:enterprises, :people, :products].include?(asset) && region |
| 101 | 101 | result.merge!(:within => radius, :region => region.id) |
| ... | ... | @@ -111,6 +111,8 @@ class SearchController < PublicController |
| 111 | 111 | :browse => params[:facet] ? params[:facet].map{ |k,v| k.to_s+':'+v.to_s} : ''}) |
| 112 | 112 | end |
| 113 | 113 | |
| 114 | + result[:order_by] = solr_order | |
| 115 | + | |
| 114 | 116 | result |
| 115 | 117 | end |
| 116 | 118 | |
| ... | ... | @@ -169,7 +171,7 @@ class SearchController < PublicController |
| 169 | 171 | |
| 170 | 172 | where_to_search.select { |key,description| @searching[key] }.each do |key, description| |
| 171 | 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 | 175 | ret = @noosfero_finder.find(key, @query, find_options) |
| 174 | 176 | @results[key] = ret.is_a?(Hash) ? ret[:results] : ret |
| 175 | 177 | @facets[key] = ret.is_a?(Hash) ? ret[:facets] : {} | ... | ... |
app/helpers/search_helper.rb
| ... | ... | @@ -134,4 +134,11 @@ module SearchHelper |
| 134 | 134 | asset_class(asset).table_name |
| 135 | 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 | 144 | end | ... | ... |
app/models/category_finder.rb
| ... | ... | @@ -29,10 +29,13 @@ class CategoryFinder |
| 29 | 29 | |
| 30 | 30 | if query.blank? |
| 31 | 31 | options.delete(:facets) |
| 32 | + options.delete(:order_by) | |
| 33 | + | |
| 32 | 34 | asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) |
| 33 | 35 | else |
| 34 | 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 | 39 | asset_class(asset).find_by_contents(query, pg_options, solr_options, options_for_find(asset_class(asset), options, date_range))[:results] |
| 37 | 40 | end |
| 38 | 41 | end | ... | ... |
app/models/environment_finder.rb
| ... | ... | @@ -23,6 +23,7 @@ class EnvironmentFinder |
| 23 | 23 | |
| 24 | 24 | if query.blank? |
| 25 | 25 | options.delete(:facets) |
| 26 | + options.delete(:order_by) | |
| 26 | 27 | |
| 27 | 28 | # FIXME this test is in more than one place |
| 28 | 29 | if finder_method == 'paginate' |
| ... | ... | @@ -51,7 +52,8 @@ class EnvironmentFinder |
| 51 | 52 | end |
| 52 | 53 | else |
| 53 | 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 | 57 | if product_category && asset == :products |
| 56 | 58 | # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source |
| 57 | 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