Commit 5268427cfc84608a1d058d054f94e6959d443518
1 parent
99864270
Exists in
master
and in
29 other branches
Only search for products from enabled enterprises
Also, add a facet to filter enabled/not enabled enterprises
Showing
4 changed files
with
29 additions
and
10 deletions
Show diff stats
app/controllers/public/search_controller.rb
| ... | ... | @@ -45,7 +45,7 @@ class SearchController < PublicController |
| 45 | 45 | |
| 46 | 46 | def products |
| 47 | 47 | if !@empty_query |
| 48 | - full_text_search ['public:true'] | |
| 48 | + full_text_search ['public:true', 'enabled:true'] | |
| 49 | 49 | else |
| 50 | 50 | @one_page = true |
| 51 | 51 | @geosearch = logged_in? && current_user.person.lat && current_user.person.lng | ... | ... |
app/models/product.rb
| ... | ... | @@ -238,7 +238,7 @@ class Product < ActiveRecord::Base |
| 238 | 238 | end |
| 239 | 239 | |
| 240 | 240 | alias_method :name_sortable, :name |
| 241 | - delegate :region, :region_id, :environment, :environment_id, :to => :enterprise | |
| 241 | + delegate :enabled, :region, :region_id, :environment, :environment_id, :to => :enterprise | |
| 242 | 242 | def name_sortable # give a different name for solr |
| 243 | 243 | name |
| 244 | 244 | end |
| ... | ... | @@ -278,7 +278,7 @@ class Product < ActiveRecord::Base |
| 278 | 278 | {:description => :text}, {:category_full_name => :text}, |
| 279 | 279 | # filtered fields |
| 280 | 280 | {:public => :boolean}, {:environment_id => :integer}, |
| 281 | - {:category_filter => :integer}, | |
| 281 | + {:enabled => :boolean}, {:category_filter => :integer}, | |
| 282 | 282 | # ordered/query-boosted fields |
| 283 | 283 | {:price_sortable => :decimal}, {:name_sortable => :string}, |
| 284 | 284 | {:lat => :float}, {:lng => :float}, | ... | ... |
app/models/profile.rb
| ... | ... | @@ -857,6 +857,7 @@ private :generate_url, :url_options |
| 857 | 857 | def f_categories |
| 858 | 858 | category_ids - [region_id] |
| 859 | 859 | end |
| 860 | + | |
| 860 | 861 | def f_region |
| 861 | 862 | self.region_id |
| 862 | 863 | end |
| ... | ... | @@ -870,6 +871,14 @@ private :generate_url, :url_options |
| 870 | 871 | end |
| 871 | 872 | end |
| 872 | 873 | |
| 874 | + def self.f_enabled_proc(enabled) | |
| 875 | + enabled = enabled == "true" ? true : false | |
| 876 | + enabled ? _('Enabled') : _('Not enabled') | |
| 877 | + end | |
| 878 | + def f_enabled | |
| 879 | + self.enabled | |
| 880 | + end | |
| 881 | + | |
| 873 | 882 | def name_sortable # give a different name for solr |
| 874 | 883 | name |
| 875 | 884 | end |
| ... | ... | @@ -882,11 +891,13 @@ private :generate_url, :url_options |
| 882 | 891 | public |
| 883 | 892 | |
| 884 | 893 | acts_as_faceted :fields => { |
| 894 | + :f_enabled => {:label => _('Situation'), :type_if => proc { |klass| klass.kind_of?(Enterprise) }, | |
| 895 | + :proc => proc { |id| f_enabled_proc(id) }}, | |
| 885 | 896 | :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, |
| 886 | 897 | :f_categories => {:multi => true, :proc => proc {|facet, id| f_categories_proc(facet, id)}, |
| 887 | 898 | :label => proc { |env| f_categories_label_proc(env) }, :label_abbrev => proc{ |env| f_categories_label_abbrev_proc(env) }}, |
| 888 | 899 | }, :category_query => proc { |c| "category_filter:#{c.id}" }, |
| 889 | - :order => [:f_region, :f_categories] | |
| 900 | + :order => [:f_region, :f_categories, :f_enabled] | |
| 890 | 901 | |
| 891 | 902 | acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index, |
| 892 | 903 | # searched fields | ... | ... |
test/functional/search_controller_test.rb
| ... | ... | @@ -799,18 +799,26 @@ class SearchControllerTest < ActionController::TestCase |
| 799 | 799 | assert_equal [prod2, prod1, prod3], assigns(:results)[:products].docs |
| 800 | 800 | end |
| 801 | 801 | |
| 802 | + should 'only list products from enabled enterprises' do | |
| 803 | + ent1 = fast_create(Enterprise, :enabled => true) | |
| 804 | + ent2 = fast_create(Enterprise, :enabled => false) | |
| 805 | + prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent1.id, :product_category_id => @product_category.id) | |
| 806 | + prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent2.id, :product_category_id => @product_category.id) | |
| 807 | + | |
| 808 | + get :products, :query => 'product' | |
| 809 | + | |
| 810 | + assert_equal [prod1], assigns(:results)[:products].docs | |
| 811 | + end | |
| 812 | + | |
| 802 | 813 | should 'order product results by name when requested' do |
| 803 | 814 | ent = fast_create(Enterprise) |
| 804 | 815 | prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent.id, :product_category_id => @product_category.id) |
| 805 | 816 | prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent.id, :product_category_id => @product_category.id) |
| 806 | 817 | prod3 = Product.create!(:name => 'product 3', :enterprise_id => ent.id, :product_category_id => @product_category.id) |
| 807 | 818 | |
| 808 | - prod3.name = 'product A' | |
| 809 | - prod3.save! | |
| 810 | - prod1.name = 'product B' | |
| 811 | - prod1.save! | |
| 812 | - prod2.name = 'product C' | |
| 813 | - prod2.save! | |
| 819 | + prod3.update_attribute! :name, 'product A' | |
| 820 | + prod2.update_attribute! :name, 'product B' | |
| 821 | + prod1.update_attribute! :name, 'product C' | |
| 814 | 822 | |
| 815 | 823 | get :products, :query => 'product', :order_by => :name |
| 816 | 824 | ... | ... |