Commit 5268427cfc84608a1d058d054f94e6959d443518

Authored by Braulio Bhavamitra
1 parent 99864270

Only search for products from enabled enterprises

Also, add a facet to filter enabled/not enabled enterprises
app/controllers/public/search_controller.rb
@@ -45,7 +45,7 @@ class SearchController < PublicController @@ -45,7 +45,7 @@ class SearchController < PublicController
45 45
46 def products 46 def products
47 if !@empty_query 47 if !@empty_query
48 - full_text_search ['public:true'] 48 + full_text_search ['public:true', 'enabled:true']
49 else 49 else
50 @one_page = true 50 @one_page = true
51 @geosearch = logged_in? && current_user.person.lat && current_user.person.lng 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,7 +238,7 @@ class Product < ActiveRecord::Base
238 end 238 end
239 239
240 alias_method :name_sortable, :name 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 def name_sortable # give a different name for solr 242 def name_sortable # give a different name for solr
243 name 243 name
244 end 244 end
@@ -278,7 +278,7 @@ class Product < ActiveRecord::Base @@ -278,7 +278,7 @@ class Product < ActiveRecord::Base
278 {:description => :text}, {:category_full_name => :text}, 278 {:description => :text}, {:category_full_name => :text},
279 # filtered fields 279 # filtered fields
280 {:public => :boolean}, {:environment_id => :integer}, 280 {:public => :boolean}, {:environment_id => :integer},
281 - {:category_filter => :integer}, 281 + {:enabled => :boolean}, {:category_filter => :integer},
282 # ordered/query-boosted fields 282 # ordered/query-boosted fields
283 {:price_sortable => :decimal}, {:name_sortable => :string}, 283 {:price_sortable => :decimal}, {:name_sortable => :string},
284 {:lat => :float}, {:lng => :float}, 284 {:lat => :float}, {:lng => :float},
app/models/profile.rb
@@ -857,6 +857,7 @@ private :generate_url, :url_options @@ -857,6 +857,7 @@ private :generate_url, :url_options
857 def f_categories 857 def f_categories
858 category_ids - [region_id] 858 category_ids - [region_id]
859 end 859 end
  860 +
860 def f_region 861 def f_region
861 self.region_id 862 self.region_id
862 end 863 end
@@ -870,6 +871,14 @@ private :generate_url, :url_options @@ -870,6 +871,14 @@ private :generate_url, :url_options
870 end 871 end
871 end 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 def name_sortable # give a different name for solr 882 def name_sortable # give a different name for solr
874 name 883 name
875 end 884 end
@@ -882,11 +891,13 @@ private :generate_url, :url_options @@ -882,11 +891,13 @@ private :generate_url, :url_options
882 public 891 public
883 892
884 acts_as_faceted :fields => { 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 :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, 896 :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }},
886 :f_categories => {:multi => true, :proc => proc {|facet, id| f_categories_proc(facet, id)}, 897 :f_categories => {:multi => true, :proc => proc {|facet, id| f_categories_proc(facet, id)},
887 :label => proc { |env| f_categories_label_proc(env) }, :label_abbrev => proc{ |env| f_categories_label_abbrev_proc(env) }}, 898 :label => proc { |env| f_categories_label_proc(env) }, :label_abbrev => proc{ |env| f_categories_label_abbrev_proc(env) }},
888 }, :category_query => proc { |c| "category_filter:#{c.id}" }, 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 acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index, 902 acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index,
892 # searched fields 903 # searched fields
test/functional/search_controller_test.rb
@@ -799,18 +799,26 @@ class SearchControllerTest < ActionController::TestCase @@ -799,18 +799,26 @@ class SearchControllerTest < ActionController::TestCase
799 assert_equal [prod2, prod1, prod3], assigns(:results)[:products].docs 799 assert_equal [prod2, prod1, prod3], assigns(:results)[:products].docs
800 end 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 should 'order product results by name when requested' do 813 should 'order product results by name when requested' do
803 ent = fast_create(Enterprise) 814 ent = fast_create(Enterprise)
804 prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent.id, :product_category_id => @product_category.id) 815 prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent.id, :product_category_id => @product_category.id)
805 prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent.id, :product_category_id => @product_category.id) 816 prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent.id, :product_category_id => @product_category.id)
806 prod3 = Product.create!(:name => 'product 3', :enterprise_id => ent.id, :product_category_id => @product_category.id) 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 get :products, :query => 'product', :order_by => :name 823 get :products, :query => 'product', :order_by => :name
816 824