Commit 97540ec02d5a6bdeb0a296a48480c3a2ba8d71e6

Authored by MoisesMachado
1 parent 667fc50c

ActionItem129: added georeference criteria to searches


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1846 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/admin/edit_template_controller.rb
@@ -9,6 +9,7 @@ class EditTemplateController < AdminController @@ -9,6 +9,7 @@ class EditTemplateController < AdminController
9 %w[ 9 %w[
10 FavoriteLinks 10 FavoriteLinks
11 ListBlock 11 ListBlock
  12 + SellersSearchBlock
12 ] 13 ]
13 end 14 end
14 15
app/controllers/admin/environment_design_controller.rb
@@ -3,7 +3,7 @@ class EnvironmentDesignController < BoxOrganizerController @@ -3,7 +3,7 @@ class EnvironmentDesignController < BoxOrganizerController
3 protect 'edit_environment_design', :environment 3 protect 'edit_environment_design', :environment
4 4
5 def available_blocks 5 def available_blocks
6 - @available_blocks ||= [ LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock ] 6 + @available_blocks ||= [ LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock ]
7 end 7 end
8 8
9 end 9 end
app/controllers/public/search_controller.rb
@@ -60,14 +60,24 @@ class SearchController < ApplicationController @@ -60,14 +60,24 @@ class SearchController < ApplicationController
60 # TODO don't hardcode like this >:-( 60 # TODO don't hardcode like this >:-(
61 LIST_LIMIT = 20 61 LIST_LIMIT = 20
62 62
  63 + def complete_region
  64 + @regions = Region.find(:all, :conditions => [ 'name like ? and lat is not null and lng is not null', '%' + params[:region][:name] + '%' ])
  65 + render :action => 'complete_region', :layout => false
  66 + end
  67 +
63 def index 68 def index
64 @query = params[:query] || '' 69 @query = params[:query] || ''
65 @filtered_query = remove_stop_words(@query) 70 @filtered_query = remove_stop_words(@query)
  71 + @region = Region.find_by_name(params[:region][:name]) if params[:region]
66 72
67 @results = {} 73 @results = {}
68 @names = {} 74 @names = {}
69 SEARCH_IN.each do |key, description| 75 SEARCH_IN.each do |key, description|
70 - @results[key] = @finder.find(key, @filtered_query) if @searching[key] 76 + if [:enterprises, :people].include?(key) && @region
  77 + @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id) if @searching[key]
  78 + else
  79 + @results[key] = @finder.find(key, @filtered_query) if @searching[key]
  80 + end
71 @names[key] = gettext(description) 81 @names[key] = gettext(description)
72 end 82 end
73 end 83 end
@@ -122,16 +132,20 @@ class SearchController < ApplicationController @@ -122,16 +132,20 @@ class SearchController < ApplicationController
122 end 132 end
123 133
124 def sellers 134 def sellers
  135 + # FIXME use a better select for category
125 @categories = ProductCategory.find(:all) 136 @categories = ProductCategory.find(:all)
126 @regions = Region.find(:all).select{|r|r.lat && r.lng} 137 @regions = Region.find(:all).select{|r|r.lat && r.lng}
127 @product_category = ProductCategory.find(params[:category]) if params[:category] 138 @product_category = ProductCategory.find(params[:category]) if params[:category]
128 @region = Region.find(params[:region]) if params[:region] 139 @region = Region.find(params[:region]) if params[:region]
  140 +
129 options = {} 141 options = {}
130 - options.merge! :include => :products, :conditions => ['products.product_category_id = ?', @product_category.id] if @product_category  
131 -  
132 options.merge! :origin => [params[:lat].to_f, params[:long].to_f], :within => params[:radius] if !params[:lat].blank? && !params[:long].blank? && !params[:radius].blank? 142 options.merge! :origin => [params[:lat].to_f, params[:long].to_f], :within => params[:radius] if !params[:lat].blank? && !params[:long].blank? && !params[:radius].blank?
133 -  
134 options.merge! :origin => [@region.lat, @region.lng], :within => params[:radius] if !params[:region].blank? && !params[:radius].blank? 143 options.merge! :origin => [@region.lat, @region.lng], :within => params[:radius] if !params[:region].blank? && !params[:radius].blank?
  144 + if @product_category
  145 + finder = CategoryFinder.new(@product_category)
  146 + product_ids = finder.find('products',nil)
  147 + options.merge! :include => :products, :conditions => ['products.id IN ?', product_ids ]
  148 + end
135 149
136 @enterprises = Enterprise.find(:all, options) 150 @enterprises = Enterprise.find(:all, options)
137 end 151 end
@@ -139,6 +153,7 @@ class SearchController < ApplicationController @@ -139,6 +153,7 @@ class SearchController < ApplicationController
139 ####################################################### 153 #######################################################
140 154
141 def popup 155 def popup
  156 + @regions = Region.find(:all).select{|r|r.lat && r.lng}
142 render :action => 'popup', :layout => false 157 render :action => 'popup', :layout => false
143 end 158 end
144 159
app/models/category_finder.rb
@@ -7,8 +7,8 @@ class CategoryFinder @@ -7,8 +7,8 @@ class CategoryFinder
7 7
8 attr_reader :category_ids 8 attr_reader :category_ids
9 9
10 - def find(asset, query)  
11 - find_in_categorized(asset.to_s.singularize.camelize.constantize, query) 10 + def find(asset, query, options={})
  11 + find_in_categorized(asset.to_s.singularize.camelize.constantize, query, options)
12 end 12 end
13 13
14 def recent(asset, limit = 10) 14 def recent(asset, limit = 10)
@@ -30,6 +30,13 @@ class CategoryFinder @@ -30,6 +30,13 @@ class CategoryFinder
30 protected 30 protected
31 31
32 def find_in_categorized(klass, query, options={}) 32 def find_in_categorized(klass, query, options={})
  33 + @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
  34 + if @region && options[:within]
  35 + options[:origin] = [@region.lat, @region.lng]
  36 + else
  37 + options.delete(:within)
  38 + end
  39 +
33 if query.nil? 40 if query.nil?
34 klass.find(:all, options_for_find(klass, options)) 41 klass.find(:all, options_for_find(klass, options))
35 else 42 else
app/models/enterprise.rb
@@ -6,4 +6,10 @@ class Enterprise < Organization @@ -6,4 +6,10 @@ class Enterprise < Organization
6 6
7 has_many :products, :dependent => :destroy 7 has_many :products, :dependent => :destroy
8 8
  9 + extra_data_for_index :product_categories
  10 +
  11 + def product_categories
  12 + products.map{|p| p.product_category.full_name.split('/') }.join(' ')
  13 + end
  14 +
9 end 15 end
app/models/environment_finder.rb
@@ -4,8 +4,14 @@ class EnvironmentFinder @@ -4,8 +4,14 @@ class EnvironmentFinder
4 @environment = env 4 @environment = env
5 end 5 end
6 6
7 - def find(asset, query)  
8 - @environment.send(asset).find_by_contents(query) 7 + def find(asset, query, options={})
  8 + @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
  9 + if @region && options[:within]
  10 + options[:origin] = [@region.lat, @region.lng]
  11 + else
  12 + options.delete(:within)
  13 + end
  14 + @environment.send(asset).find_by_contents(query, {}, options)
9 end 15 end
10 16
11 def recent(asset, limit = 10) 17 def recent(asset, limit = 10)
app/models/product.rb
@@ -8,6 +8,14 @@ class Product < ActiveRecord::Base @@ -8,6 +8,14 @@ class Product < ActiveRecord::Base
8 8
9 after_update :save_image 9 after_update :save_image
10 10
  11 + after_create do |p|
  12 + p.enterprise.save if p.enterprise
  13 + end
  14 +
  15 + after_update do |p|
  16 + p.enterprise.save if p.enterprise
  17 + end
  18 +
11 acts_as_searchable :fields => [ :name, :description, :category_full_name ] 19 acts_as_searchable :fields => [ :name, :description, :category_full_name ]
12 20
13 xss_terminate :only => [ :name, :description ] 21 xss_terminate :only => [ :name, :description ]
app/models/profile.rb
@@ -31,7 +31,19 @@ class Profile < ActiveRecord::Base @@ -31,7 +31,19 @@ class Profile < ActiveRecord::Base
31 31
32 acts_as_having_boxes 32 acts_as_having_boxes
33 33
34 - acts_as_searchable :fields => [ :name, :identifier ] 34 + acts_as_searchable :fields => [ :name, :identifier, :extra_data_for_index ]
  35 +
  36 + class_inheritable_accessor :extra_index_methods
  37 + self.extra_index_methods = []
  38 +
  39 + def extra_data_for_index
  40 + self.class.extra_index_methods.map { |meth| meth.to_proc.call(self) }
  41 + end
  42 +
  43 + def self.extra_data_for_index(sym = nil, &block)
  44 + self.extra_index_methods.push(sym) if sym
  45 + self.extra_index_methods.push(block) if block_given?
  46 + end
35 47
36 acts_as_having_settings :field => :data 48 acts_as_having_settings :field => :data
37 49
app/models/sellers_search_block.rb 0 → 100644
@@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
  1 +class SellersSearchBlock < Block
  2 +
  3 + def self.description
  4 + _('A search for enterprises by products selled and local')
  5 + end
  6 +
  7 + def content
  8 + lambda do
  9 + @categories = ProductCategory.find(:all)
  10 + @regions = Region.find(:all).select{|r|r.lat && r.lng}
  11 + render :file => 'search/_sellers_form'
  12 + end
  13 + end
  14 +end
app/views/search/_sellers_form.rhtml 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +<% form_tag({:controller => 'search', :action => 'index'}, {:method => 'get'}) do %>
  2 + <%= _('Category: ') %> <%= select_tag 'query', options_from_collection_for_select(@categories, :name, :name, @product_category), :width => 15 %><br/>
  3 + <%= _('Distance: ') %> <%= text_field_tag 'radius' %><br/>
  4 + <%= _('From: ') %> <%= text_field :region, :name, :id => 'search_region_block' %>
  5 + <div id='search_region_block_auto_complete' class='auto-complete'></div>
  6 + <%= auto_complete_field('search_region_block', :url => {:controller => 'search', :action => 'complete_region'}) %>
  7 + <br/>
  8 + <%= submit_tag _('Search') %>
  9 +<% end %>
app/views/search/complete_region.rhtml 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +<% unless @regions.empty? %>
  2 + <ul>
  3 + <% for region in @regions %>
  4 + <li><%= region.name %></li>
  5 + <% end %>
  6 + </ul>
  7 +<% end%>
app/views/search/sellers.rhtml
@@ -14,12 +14,7 @@ @@ -14,12 +14,7 @@
14 <% end %> 14 <% end %>
15 </h2> 15 </h2>
16 16
17 -<% form_tag({}, {:method => 'get'}) do %>  
18 - <%= _('Category: ') %> <%= select_tag 'category', options_from_collection_for_select(@categories, :id, :name, @product_category) %><br/>  
19 - <%= _('Distance from point: ') %> <%= text_field_tag 'radius' %><br/>  
20 - <%= _('Region') %> <%= select_tag 'region', options_from_collection_for_select(@regions, :id, :name, @region) %><br/>  
21 - <%= submit_tag _('Search') %>  
22 -<% end %> 17 +<%= render :partial => 'sellers_form' %>
23 18
24 <div class="search-results-innerbox search-results-type-profile common-profile-list-block %>"> 19 <div class="search-results-innerbox search-results-type-profile common-profile-list-block %>">
25 <ul> <%= render :partial => 'profile', :collection => @enterprises %> </ul> 20 <ul> <%= render :partial => 'profile', :collection => @enterprises %> </ul>
public/stylesheets/common.css
@@ -230,6 +230,8 @@ table.cms-articles th, table.cms-articles td { @@ -230,6 +230,8 @@ table.cms-articles th, table.cms-articles td {
230 230
231 /* for fields with auto-completion */ 231 /* for fields with auto-completion */
232 div.auto-complete { 232 div.auto-complete {
  233 + display: block;
  234 + float: none;
233 background: #729FCF; 235 background: #729FCF;
234 border: 2px solid #204A87; 236 border: 2px solid #204A87;
235 } 237 }
test/functional/search_controller_test.rb
@@ -397,6 +397,13 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -397,6 +397,13 @@ class SearchControllerTest &lt; Test::Unit::TestCase
397 assert_tag :tag => 'input', :attributes => { :type => 'submit', :name => 'search_whole_site_no' } 397 assert_tag :tag => 'input', :attributes => { :type => 'submit', :name => 'search_whole_site_no' }
398 end 398 end
399 399
  400 + should 'display option to search within a given point and distance' do
  401 + get :popup
  402 +
  403 + assert_tag :tag => 'input', :attributes => {:type => 'text', :name => 'radius'}
  404 + assert_tag :tag => 'input', :attributes => {:type => 'text', :name => 'region[name]'}
  405 + end
  406 +
400 should 'search in whole site when told so' do 407 should 'search in whole site when told so' do
401 parent = Category.create!(:name => 'randomcat', :environment => Environment.default) 408 parent = Category.create!(:name => 'randomcat', :environment => Environment.default)
402 Category.create!(:name => 'randomchild', :environment => Environment.default, :parent => parent) 409 Category.create!(:name => 'randomchild', :environment => Environment.default, :parent => parent)
@@ -547,7 +554,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -547,7 +554,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
547 assert_tag :tag => 'h1', :content => /Search results for &quot;a sample search&quot; in &quot;Child Category&quot;/ 554 assert_tag :tag => 'h1', :content => /Search results for &quot;a sample search&quot; in &quot;Child Category&quot;/
548 end 555 end
549 556
550 - should 'search in categoty hierachy' do 557 + should 'search in category hierachy' do
551 parent = Category.create!(:name => 'Parent Category', :environment => Environment.default) 558 parent = Category.create!(:name => 'Parent Category', :environment => Environment.default)
552 child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent) 559 child = Category.create!(:name => 'Child Category', :environment => Environment.default, :parent => parent)
553 560
@@ -760,78 +767,34 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -760,78 +767,34 @@ class SearchControllerTest &lt; Test::Unit::TestCase
760 assert_not_includes assigns(:results)[:comments], comment4 767 assert_not_includes assigns(:results)[:comments], comment4
761 end 768 end
762 769
763 - should 'list all sellers' do  
764 - ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1')  
765 - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2')  
766 -  
767 - get :sellers  
768 -  
769 - assert_includes assigns('enterprises'), ent1  
770 - assert_includes assigns('enterprises'), ent2  
771 - end  
772 -  
773 should 'find enterprise by product category' do 770 should 'find enterprise by product category' do
774 ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1') 771 ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1')
775 - prod_cat = ProductCategory.create!(:name => 'pc-test', :environment => Environment.default) 772 + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default)
776 prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) 773 prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat)
777 774
778 ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') 775 ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2')
779 776
780 - get :sellers, :category => prod_cat.id  
781 -  
782 - assert_includes assigns('enterprises'), ent1  
783 - assert_not_includes assigns('enterprises'), ent2  
784 - end  
785 -  
786 - should 'find enterprise by origin and radius' do  
787 - ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)  
788 - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0)  
789 -  
790 - get :sellers, :lat => 45.0, :long => 45.0, :radius => 10 777 + get :index, :query => prod_cat.name
791 778
792 - assert_includes assigns('enterprises'), ent1  
793 - assert_not_includes assigns('enterprises'), ent2 779 + assert_includes assigns('results')[:enterprises], ent1
  780 + assert_not_includes assigns('results')[:enterprises], ent2
794 end 781 end
795 782
796 - should 'find enterprise by an region with georeference' do 783 + should 'find profiles by radius and region' do
797 region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) 784 region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)
798 - ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)  
799 - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0) 785 + ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)
  786 + p1 = create_user('test2').person
  787 + p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save!
  788 + ent2 = Enterprise.create!(:name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0)
  789 + p2 = create_user('test4').person
  790 + p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save!
800 791
801 - get :sellers, :region => region.id, :radius => 10 792 + get :index, :region => { :name => region.name }, :radius => 10, :query => 'test'
802 793
803 - assert_includes assigns('enterprises'), ent1  
804 - assert_not_includes assigns('enterprises'), ent2  
805 - end  
806 -  
807 - should 'find enterprise by region and product category' do  
808 - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)  
809 - ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)  
810 - prod_cat = ProductCategory.create!(:name => 'pc-test', :environment => Environment.default)  
811 - prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat)  
812 -  
813 - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0)  
814 -  
815 - get :sellers, :region => region.id, :radius => 10, :category => prod_cat.id  
816 -  
817 - assert_includes assigns('enterprises'), ent1  
818 - assert_not_includes assigns('enterprises'), ent2  
819 - end  
820 -  
821 - should 'find enterprise by region and product category in brazilian portuguese' do  
822 - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)  
823 - ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)  
824 - prod_cat = ProductCategory.create!(:name => 'pc-test', :environment => Environment.default)  
825 - prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat)  
826 -  
827 - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :lat => 30.0, :lng => 30.0)  
828 -  
829 - assert_nothing_raised do  
830 - get :sellers, :region => region.id, :radius => 10, :category => prod_cat.id, :lang => 'pt_BR'  
831 - end  
832 -  
833 - assert_includes assigns('enterprises'), ent1  
834 - assert_not_includes assigns('enterprises'), ent2 794 + assert_includes assigns('results')[:enterprises], ent1
  795 + assert_not_includes assigns('results')[:enterprises], ent2
  796 + assert_includes assigns('results')[:people], p1
  797 + assert_not_includes assigns('results')[:people], p2
835 end 798 end
836 799
837 should 'not show term "Category:" before product category' do 800 should 'not show term "Category:" before product category' do
@@ -852,4 +815,29 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -852,4 +815,29 @@ class SearchControllerTest &lt; Test::Unit::TestCase
852 assert_tag :tag => 'img', :attributes => { :src => /rails_thumb\.png/ } 815 assert_tag :tag => 'img', :attributes => { :src => /rails_thumb\.png/ }
853 end 816 end
854 817
  818 + should 'complete region name' do
  819 + r1 = Region.create!(:name => 'One region', :environment => Environment.default, :lat => 111.07, :lng => '88.9')
  820 + r2 = Region.create!(:name => 'Another region', :environment => Environment.default, :lat => 111.07, :lng => '88.9')
  821 +
  822 + get :complete_region, :region => { :name => 'one' }
  823 + assert_includes assigns(:regions), r1
  824 + assert_tag :tag => 'ul', :descendant => { :tag => 'li', :content => 'One region' }
  825 + end
  826 +
  827 + should 'render completion results without layout' do
  828 + get :complete_region, :region => { :name => 'test' }
  829 + assert_no_tag :tag => 'body'
  830 + end
  831 +
  832 + should 'complete only georeferenced regions' do
  833 + r1 = Region.create!(:name => 'One region', :environment => Environment.default, :lat => 111.07, :lng => '88.9')
  834 + r2 = Region.create!(:name => 'Another region', :environment => Environment.default)
  835 +
  836 + get :complete_region, :region => { :name => 'region' }
  837 + assert_includes assigns(:regions), r1
  838 + assert_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r1.name }
  839 + assert_not_includes assigns(:regions), r2
  840 + assert_no_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r2.name }
  841 + end
  842 +
855 end 843 end
test/unit/category_finder_test.rb
@@ -252,4 +252,24 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -252,4 +252,24 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
252 assert_not_includes list, c2 252 assert_not_includes list, c2
253 end 253 end
254 254
  255 + should 'find person and enterprise by radius and region' do
  256 + finder = CategoryFinder.new(@category)
  257 +
  258 + region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)
  259 + ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0, :categories => [@category])
  260 + p1 = create_user('test2').person
  261 + p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.categories = [@category]; p1.save!
  262 + ent2 = Enterprise.create!(:name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0, :categories => [@category])
  263 + p2 = create_user('test4').person
  264 + p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.categories = [@category]; p2.save!
  265 +
  266 + ents = finder.find(:enterprises, 'test', :within => 10, :region => region.id)
  267 + people = finder.find(:people, 'test', :within => 10, :region => region.id)
  268 +
  269 + assert_includes ents, ent1
  270 + assert_not_includes ents, ent2
  271 + assert_includes people, p1
  272 + assert_not_includes people, p2
  273 + end
  274 +
255 end 275 end
test/unit/enterprise_test.rb
@@ -76,6 +76,33 @@ class EnterpriseTest &lt; Test::Unit::TestCase @@ -76,6 +76,33 @@ class EnterpriseTest &lt; Test::Unit::TestCase
76 assert_equal 5, e.blocks.size 76 assert_equal 5, e.blocks.size
77 end 77 end
78 78
  79 + should 'be found in search for its product categories' do
  80 + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1')
  81 + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default)
  82 + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat)
  83 +
  84 + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2')
  85 +
  86 + result = Enterprise.find_by_contents(prod_cat.name)
  87 +
  88 + assert_includes result, ent1
  89 + assert_not_includes result, ent2
  90 + end
  91 +
  92 + should 'be found in search for its product categories hierarchy' do
  93 + ent1 = Enterprise.create!(:name => 'test1', :identifier => 'test1')
  94 + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default)
  95 + prod_child = ProductCategory.create!(:name => 'pchild', :environment => Environment.default, :parent => prod_cat)
  96 + prod = ent1.products.create!(:name => 'teste', :product_category => prod_child)
  97 +
  98 + ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2')
  99 +
  100 + result = Enterprise.find_by_contents(prod_cat.name)
  101 +
  102 + assert_includes result, ent1
  103 + assert_not_includes result, ent2
  104 + end
  105 +
79 should 'allow to add new members' do 106 should 'allow to add new members' do
80 o = Enterprise.create!(:name => 'my test profile', :identifier => 'mytestprofile') 107 o = Enterprise.create!(:name => 'my test profile', :identifier => 'mytestprofile')
81 p = create_user('mytestuser').person 108 p = create_user('mytestuser').person
test/unit/environment_finder_test.rb
@@ -131,4 +131,24 @@ class EnvironmentFinderTest &lt; ActiveSupport::TestCase @@ -131,4 +131,24 @@ class EnvironmentFinderTest &lt; ActiveSupport::TestCase
131 assert_not_includes found, ent2 131 assert_not_includes found, ent2
132 end 132 end
133 133
  134 + should 'find person and enterprise by radius and region' do
  135 + finder = EnvironmentFinder.new(Environment.default)
  136 +
  137 + region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)
  138 + ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)
  139 + p1 = create_user('test2').person
  140 + p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save!
  141 + ent2 = Enterprise.create!(:name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0)
  142 + p2 = create_user('test4').person
  143 + p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save!
  144 +
  145 + ents = finder.find(:enterprises, 'test', :within => 10, :region => region.id)
  146 + people = finder.find(:people, 'test', :within => 10, :region => region.id)
  147 +
  148 + assert_includes ents, ent1
  149 + assert_not_includes ents, ent2
  150 + assert_includes people, p1
  151 + assert_not_includes people, p2
  152 + end
  153 +
134 end 154 end
test/unit/profile_test.rb
@@ -69,7 +69,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -69,7 +69,7 @@ class ProfileTest &lt; Test::Unit::TestCase
69 p.identifier = 'other_profile' 69 p.identifier = 'other_profile'
70 end 70 end
71 end 71 end
72 - 72 +
73 should 'provide access to home page' do 73 should 'provide access to home page' do
74 profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile') 74 profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile')
75 assert_kind_of Article, profile.home_page 75 assert_kind_of Article, profile.home_page
@@ -87,7 +87,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -87,7 +87,7 @@ class ProfileTest &lt; Test::Unit::TestCase
87 def test_can_have_affiliated_people 87 def test_can_have_affiliated_people
88 pr = Profile.create(:name => 'composite_profile', :identifier => 'composite') 88 pr = Profile.create(:name => 'composite_profile', :identifier => 'composite')
89 pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person 89 pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person
90 - 90 +
91 member_role = Role.new(:name => 'new_member_role') 91 member_role = Role.new(:name => 'new_member_role')
92 assert member_role.save 92 assert member_role.save
93 assert pr.affiliate(pe, member_role) 93 assert pr.affiliate(pe, member_role)
@@ -164,7 +164,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -164,7 +164,7 @@ class ProfileTest &lt; Test::Unit::TestCase
164 assert_raise ActiveRecord::AssociationTypeMismatch do 164 assert_raise ActiveRecord::AssociationTypeMismatch do
165 profile.articles << 1 165 profile.articles << 1
166 end 166 end
167 - 167 +
168 assert_nothing_raised do 168 assert_nothing_raised do
169 profile.articles << Article.new(:name => 'testing article') 169 profile.articles << Article.new(:name => 'testing article')
170 end 170 end
@@ -206,7 +206,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -206,7 +206,7 @@ class ProfileTest &lt; Test::Unit::TestCase
206 206
207 assert Profile.find_by_contents('small').include?(small) 207 assert Profile.find_by_contents('small').include?(small)
208 assert Profile.find_by_contents('big').include?(big) 208 assert Profile.find_by_contents('big').include?(big)
209 - 209 +
210 both = Profile.find_by_contents('profile testing') 210 both = Profile.find_by_contents('profile testing')
211 assert both.include?(small) 211 assert both.include?(small)
212 assert both.include?(big) 212 assert both.include?(big)
@@ -229,7 +229,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -229,7 +229,7 @@ class ProfileTest &lt; Test::Unit::TestCase
229 229
230 profile_boxes = profile.boxes.size 230 profile_boxes = profile.boxes.size
231 profile_blocks = profile.blocks.size 231 profile_blocks = profile.blocks.size
232 - 232 +
233 assert profile_boxes > 0, 'profile should have some boxes' 233 assert profile_boxes > 0, 'profile should have some boxes'
234 assert profile_blocks > 0, 'profile should have some blocks' 234 assert profile_blocks > 0, 'profile should have some blocks'
235 235
@@ -488,6 +488,37 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -488,6 +488,37 @@ class ProfileTest &lt; Test::Unit::TestCase
488 assert c.display_info_to?(p) 488 assert c.display_info_to?(p)
489 end 489 end
490 490
  491 + should 'be able to add extra data for index' do
  492 + klass = Class.new(Profile)
  493 + klass.any_instance.expects(:random_method)
  494 + klass.extra_data_for_index :random_method
  495 +
  496 + klass.new.extra_data_for_index
  497 + end
  498 +
  499 + should 'be able to add a block as extra data for index' do
  500 + klass = Class.new(Profile)
  501 + result = mock
  502 + klass.extra_data_for_index do |obj|
  503 + result
  504 + end
  505 +
  506 + assert_includes klass.new.extra_data_for_index, result
  507 + end
  508 +
  509 + should 'actually index by results of extra_data_for_index' do
  510 +
  511 + class ::TestingExtraDataForIndex < Profile
  512 + extra_data_for_index do |obj|
  513 + 'sample indexed text'
  514 + end
  515 + end
  516 +
  517 + profile = TestingExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile')
  518 +
  519 + assert_includes TestingExtraDataForIndex.find_by_contents('sample'), profile
  520 + end
  521 +
491 private 522 private
492 523
493 def assert_invalid_identifier(id) 524 def assert_invalid_identifier(id)
test/unit/sellers_search_block_test.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class SellersSearchBlockTest < ActiveSupport::TestCase
  4 + # Replace this with your real tests.
  5 + def test_truth
  6 + assert true
  7 + end
  8 +end