Commit 9b733e186fd8205ec1a5afc1ce933c8d877b60d7
1 parent
85198748
Exists in
master
and in
28 other branches
Adapt acts_as_searcheable to Solr
Change acts_as_searcheable to always index schema_path. If multitenancy if off or the database isn't PostgreSQL, then schema_path method returns an empty string and is not used at find_by_contents. Change find_by_contents adding options separating options from paginate, solr and rails's find. With Solr search capabilities, combined search (solr + rails find) won't be necessary in the future. Also change find_by_contents to return a hash where there are the facets found and also the results. Related tests pass with this change
Showing
19 changed files
with
124 additions
and
109 deletions
Show diff stats
app/controllers/my_profile/profile_members_controller.rb
@@ -126,11 +126,11 @@ class ProfileMembersController < MyProfileController | @@ -126,11 +126,11 @@ class ProfileMembersController < MyProfileController | ||
126 | if !params[:query] || params[:query].length <= 2 | 126 | if !params[:query] || params[:query].length <= 2 |
127 | @users_found = [] | 127 | @users_found = [] |
128 | elsif params[:scope] == 'all_users' | 128 | elsif params[:scope] == 'all_users' |
129 | - @users_found = Person.find_by_contents(params[:query] + '*').select {|user| !profile.members.include?(user)} | 129 | + @users_found = Person.find_by_contents(params[:query] + '*')[:results].select {|user| !profile.members.include?(user)} |
130 | @button_alt = _('Add member') | 130 | @button_alt = _('Add member') |
131 | @add_action = {:action => 'add_member'} | 131 | @add_action = {:action => 'add_member'} |
132 | elsif params[:scope] == 'new_admins' | 132 | elsif params[:scope] == 'new_admins' |
133 | - @users_found = Person.find_by_contents(params[:query] + '*').select {|user| profile.members.include?(user) && !profile.admins.include?(user)} | 133 | + @users_found = Person.find_by_contents(params[:query] + '*')[:results].select {|user| profile.members.include?(user) && !profile.admins.include?(user)} |
134 | @button_alt = _('Add member') | 134 | @button_alt = _('Add member') |
135 | @add_action = {:action => 'add_admin'} | 135 | @add_action = {:action => 'add_admin'} |
136 | end | 136 | end |
app/controllers/public/browse_controller.rb
@@ -19,7 +19,7 @@ class BrowseController < PublicController | @@ -19,7 +19,7 @@ class BrowseController < PublicController | ||
19 | @results = @environment.people.visible.send(@filter) | 19 | @results = @environment.people.visible.send(@filter) |
20 | 20 | ||
21 | if !params[:query].blank? | 21 | if !params[:query].blank? |
22 | - @results = @results.find_by_contents(params[:query]) | 22 | + @results = @results.find_by_contents(params[:query])[:results] |
23 | end | 23 | end |
24 | @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) | 24 | @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) |
25 | end | 25 | end |
@@ -31,7 +31,7 @@ class BrowseController < PublicController | @@ -31,7 +31,7 @@ class BrowseController < PublicController | ||
31 | @results = @environment.communities.visible.send(@filter) | 31 | @results = @environment.communities.visible.send(@filter) |
32 | 32 | ||
33 | if !params[:query].blank? | 33 | if !params[:query].blank? |
34 | - @results = @results.find_by_contents(params[:query]) | 34 | + @results = @results.find_by_contents(params[:query])[:results] |
35 | end | 35 | end |
36 | @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) | 36 | @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) |
37 | end | 37 | end |
app/models/article.rb
@@ -388,7 +388,7 @@ class Article < ActiveRecord::Base | @@ -388,7 +388,7 @@ class Article < ActiveRecord::Base | ||
388 | end | 388 | end |
389 | 389 | ||
390 | def comments_updated | 390 | def comments_updated |
391 | - ferret_update | 391 | + solr_save |
392 | end | 392 | end |
393 | 393 | ||
394 | def accept_category?(cat) | 394 | def accept_category?(cat) |
app/models/category_finder.rb
@@ -30,8 +30,8 @@ class CategoryFinder | @@ -30,8 +30,8 @@ class CategoryFinder | ||
30 | if query.blank? | 30 | if query.blank? |
31 | asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) | 31 | asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) |
32 | else | 32 | else |
33 | - ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | ||
34 | - asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options, date_range)) | 33 | + pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} |
34 | + asset_class(asset).find_by_contents(query, pg_options, {}, options_for_find(asset_class(asset), options, date_range))[:results] | ||
35 | end | 35 | end |
36 | end | 36 | end |
37 | 37 |
app/models/enterprise.rb
app/models/environment_finder.rb
@@ -48,14 +48,14 @@ class EnvironmentFinder | @@ -48,14 +48,14 @@ class EnvironmentFinder | ||
48 | end | 48 | end |
49 | end | 49 | end |
50 | else | 50 | else |
51 | - ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | 51 | + pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} |
52 | if product_category && asset == :products | 52 | if product_category && asset == :products |
53 | # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source | 53 | # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source |
54 | - @environment.send(asset).find_by_contents(query, ferret_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id })) | 54 | + @environment.send(asset).find_by_contents(query, pg_options, {}, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id }))[:results] |
55 | elsif product_category && asset == :enterprises | 55 | elsif product_category && asset == :enterprises |
56 | - @environment.send(asset).find_by_contents(query, ferret_options, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :include => 'products', :conditions => "product_categorizations.category_id = (#{product_category.id})")) | 56 | + @environment.send(asset).find_by_contents(query, pg_options, {}, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :include => 'products', :conditions => "product_categorizations.category_id = (#{product_category.id})"))[:results] |
57 | else | 57 | else |
58 | - @environment.send(asset).find_by_contents(query, ferret_options, options) | 58 | + @environment.send(asset).find_by_contents(query, pg_options, {}, options)[:results] |
59 | end | 59 | end |
60 | end | 60 | end |
61 | end | 61 | end |
app/models/region.rb
@@ -5,10 +5,9 @@ class Region < Category | @@ -5,10 +5,9 @@ class Region < Category | ||
5 | require_dependency 'enterprise' # enterprises can also be validators | 5 | require_dependency 'enterprise' # enterprises can also be validators |
6 | 6 | ||
7 | # searches for organizations that could become validators for this region. | 7 | # searches for organizations that could become validators for this region. |
8 | - # <tt>search</tt> is passed as is to ferret's find_by_contents on Organizatio | ||
9 | - # find_by_contents on Organization class. | 8 | + # <tt>search</tt> is passed as is to find_by_contents on Organization. |
10 | def search_possible_validators(search) | 9 | def search_possible_validators(search) |
11 | - Organization.find_by_contents(search).reject {|item| self.validator_ids.include?(item.id) } | 10 | + Organization.find_by_contents(search)[:results].reject {|item| self.validator_ids.include?(item.id) } |
12 | end | 11 | end |
13 | 12 | ||
14 | def has_validator? | 13 | def has_validator? |
lib/acts_as_searchable.rb
@@ -2,56 +2,59 @@ module ActsAsSearchable | @@ -2,56 +2,59 @@ module ActsAsSearchable | ||
2 | 2 | ||
3 | module ClassMethods | 3 | module ClassMethods |
4 | def acts_as_searchable(options = {}) | 4 | def acts_as_searchable(options = {}) |
5 | - if Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql? | ||
6 | - options[:additional_fields] ||= {} | ||
7 | - options[:additional_fields] = Hash[*options[:additional_fields].collect{ |v| [v, {}] }.flatten] if options[:additional_fields].is_a?(Array) | ||
8 | - options[:additional_fields].merge!(:schema_name => { :index => :untokenized }) | 5 | + if (!options[:fields]) |
6 | + options[:additional_fields] |= [{:schema_name => :string}] | ||
7 | + else | ||
8 | + options[:fields] << {:schema_name => :string} | ||
9 | end | 9 | end |
10 | - acts_as_ferret({ :remote => true }.merge(options)) | 10 | + acts_as_solr options |
11 | extend FindByContents | 11 | extend FindByContents |
12 | send :include, InstanceMethods | 12 | send :include, InstanceMethods |
13 | end | 13 | end |
14 | 14 | ||
15 | module InstanceMethods | 15 | module InstanceMethods |
16 | def schema_name | 16 | def schema_name |
17 | - ActiveRecord::Base.connection.schema_search_path | 17 | + (Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : '' |
18 | end | 18 | end |
19 | end | 19 | end |
20 | 20 | ||
21 | module FindByContents | 21 | module FindByContents |
22 | 22 | ||
23 | def schema_name | 23 | def schema_name |
24 | - ActiveRecord::Base.connection.schema_search_path | 24 | + (Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : '' |
25 | end | 25 | end |
26 | 26 | ||
27 | - def find_by_contents(query, ferret_options = {}, db_options = {}) | ||
28 | - pg_options = {} | ||
29 | - if ferret_options[:page] | ||
30 | - pg_options[:page] = ferret_options.delete(:page) | ||
31 | - end | ||
32 | - if ferret_options[:per_page] | ||
33 | - pg_options[:per_page] = ferret_options.delete(:per_page) | ||
34 | - end | ||
35 | - | ||
36 | - ferret_options[:limit] = :all | ||
37 | - | ||
38 | - ferret_query = (Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? "+schema_name:\"#{schema_name}\" AND #{query}" : query | ||
39 | - # FIXME this is a HORRIBLE HACK | ||
40 | - ids = find_ids_with_ferret(ferret_query, ferret_options)[1][0..8000].map{|r|r[:id].to_i} | ||
41 | - | ||
42 | - if ids.empty? | ||
43 | - ids << -1 | ||
44 | - end | 27 | + def find_by_contents(query, pg_options = {}, options = {}, db_options = {}) |
28 | + pg_options[:page] ||= 1 | ||
29 | + options[:limit] = 1000000; | ||
30 | + options[:scores] = true; | ||
45 | 31 | ||
46 | - if db_options[:conditions] | ||
47 | - db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" | 32 | + query = !schema_name.empty? ? "+schema_name:\"#{schema_name}\" AND #{query}" : query |
33 | + solr_result = find_by_solr(query, options) | ||
34 | + if solr_result.nil? | ||
35 | + results = facets = [] | ||
48 | else | 36 | else |
49 | - db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" | 37 | + facets = options.include?(:facets) ? solr_result.facets : {} |
38 | + if db_options.empty? | ||
39 | + results = solr_result.results.paginate(pg_options) | ||
40 | + else | ||
41 | + ids = solr_result.results.map{|r|r[:id].to_i} | ||
42 | + if ids.empty? | ||
43 | + ids << -1 | ||
44 | + end | ||
45 | + | ||
46 | + if db_options[:conditions] | ||
47 | + db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" | ||
48 | + else | ||
49 | + db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" | ||
50 | + end | ||
51 | + | ||
52 | + result = find(:all, db_options) | ||
53 | + results = result.paginate(pg_options) | ||
54 | + end | ||
50 | end | 55 | end |
51 | 56 | ||
52 | - pg_options[:page] ||= 1 | ||
53 | - result = find(:all, db_options) | ||
54 | - result.paginate(pg_options) | 57 | + {:results => results, :facets => facets} |
55 | end | 58 | end |
56 | end | 59 | end |
57 | end | 60 | end |
test/test_helper.rb
@@ -47,6 +47,10 @@ class Test::Unit::TestCase | @@ -47,6 +47,10 @@ class Test::Unit::TestCase | ||
47 | include AuthenticatedTestHelper | 47 | include AuthenticatedTestHelper |
48 | 48 | ||
49 | fixtures :environments, :roles | 49 | fixtures :environments, :roles |
50 | + | ||
51 | + def self.setup | ||
52 | + ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*')) | ||
53 | + end | ||
50 | 54 | ||
51 | def self.all_fixtures | 55 | def self.all_fixtures |
52 | Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |item| | 56 | Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |item| |
@@ -190,7 +194,6 @@ class Test::Unit::TestCase | @@ -190,7 +194,6 @@ class Test::Unit::TestCase | ||
190 | adapter.any_instance.stubs(:adapter_name).returns('PostgreSQL') | 194 | adapter.any_instance.stubs(:adapter_name).returns('PostgreSQL') |
191 | adapter.any_instance.stubs(:schema_search_path).returns(schema_name) | 195 | adapter.any_instance.stubs(:schema_search_path).returns(schema_name) |
192 | Noosfero::MultiTenancy.stubs(:on?).returns(true) | 196 | Noosfero::MultiTenancy.stubs(:on?).returns(true) |
193 | - reload_for_ferret | ||
194 | end | 197 | end |
195 | 198 | ||
196 | def uses_sqlite | 199 | def uses_sqlite |
@@ -199,20 +202,6 @@ class Test::Unit::TestCase | @@ -199,20 +202,6 @@ class Test::Unit::TestCase | ||
199 | Noosfero::MultiTenancy.stubs(:on?).returns(false) | 202 | Noosfero::MultiTenancy.stubs(:on?).returns(false) |
200 | end | 203 | end |
201 | 204 | ||
202 | - def reload_for_ferret | ||
203 | - ActsAsFerret.send(:remove_const, :DEFAULT_FIELD_OPTIONS) | ||
204 | - load File.join(RAILS_ROOT, 'lib', 'acts_as_searchable.rb') | ||
205 | - load File.join(RAILS_ROOT, 'vendor', 'plugins', 'acts_as_ferret', 'lib', 'acts_as_ferret.rb') | ||
206 | - [Article, Profile, Product].each do |clazz| | ||
207 | - inst_meth = clazz.instance_methods.reject{ |m| m =~ /_to_ferret$/ } | ||
208 | - clazz.stubs(:instance_methods).returns(inst_meth) | ||
209 | - end | ||
210 | - #FIXME Is there a way to avoid this replication from model code? | ||
211 | - Article.acts_as_searchable :additional_fields => [ :comment_data ] | ||
212 | - Profile.acts_as_searchable :additional_fields => [ :extra_data_for_index ] | ||
213 | - Product.acts_as_searchable :fields => [ :name, :description, :category_full_name ] | ||
214 | - end | ||
215 | - | ||
216 | end | 205 | end |
217 | 206 | ||
218 | module NoosferoTestHelper | 207 | module NoosferoTestHelper |
test/unit/article_test.rb
@@ -5,6 +5,7 @@ class ArticleTest < Test::Unit::TestCase | @@ -5,6 +5,7 @@ class ArticleTest < Test::Unit::TestCase | ||
5 | fixtures :environments | 5 | fixtures :environments |
6 | 6 | ||
7 | def setup | 7 | def setup |
8 | + Test::Unit::TestCase::setup | ||
8 | @profile = create_user('testing').person | 9 | @profile = create_user('testing').person |
9 | end | 10 | end |
10 | attr_reader :profile | 11 | attr_reader :profile |
@@ -356,7 +357,7 @@ class ArticleTest < Test::Unit::TestCase | @@ -356,7 +357,7 @@ class ArticleTest < Test::Unit::TestCase | ||
356 | 357 | ||
357 | should 'reindex when comments are changed' do | 358 | should 'reindex when comments are changed' do |
358 | a = Article.new | 359 | a = Article.new |
359 | - a.expects(:ferret_update) | 360 | + a.expects(:solr_save) |
360 | a.comments_updated | 361 | a.comments_updated |
361 | end | 362 | end |
362 | 363 | ||
@@ -365,7 +366,7 @@ class ArticleTest < Test::Unit::TestCase | @@ -365,7 +366,7 @@ class ArticleTest < Test::Unit::TestCase | ||
365 | art = owner.articles.build(:name => 'ytest'); art.save! | 366 | art = owner.articles.build(:name => 'ytest'); art.save! |
366 | c1 = art.comments.build(:title => 'a nice comment', :body => 'anything', :author => owner); c1.save! | 367 | c1 = art.comments.build(:title => 'a nice comment', :body => 'anything', :author => owner); c1.save! |
367 | 368 | ||
368 | - assert_includes Article.find_by_contents('nice'), art | 369 | + assert_includes Article.find_by_contents('nice')[:results], art |
369 | end | 370 | end |
370 | 371 | ||
371 | should 'index comments body together with article' do | 372 | should 'index comments body together with article' do |
@@ -373,7 +374,7 @@ class ArticleTest < Test::Unit::TestCase | @@ -373,7 +374,7 @@ class ArticleTest < Test::Unit::TestCase | ||
373 | art = owner.articles.build(:name => 'ytest'); art.save! | 374 | art = owner.articles.build(:name => 'ytest'); art.save! |
374 | c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! | 375 | c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! |
375 | 376 | ||
376 | - assert_includes Article.find_by_contents('anything'), art | 377 | + assert_includes Article.find_by_contents('anything')[:results], art |
377 | end | 378 | end |
378 | 379 | ||
379 | should 'cache children count' do | 380 | should 'cache children count' do |
@@ -1506,24 +1507,24 @@ class ArticleTest < Test::Unit::TestCase | @@ -1506,24 +1507,24 @@ class ArticleTest < Test::Unit::TestCase | ||
1506 | should 'index by schema name when database is postgresql' do | 1507 | should 'index by schema name when database is postgresql' do |
1507 | uses_postgresql 'schema_one' | 1508 | uses_postgresql 'schema_one' |
1508 | art1 = Article.create!(:name => 'some thing', :profile_id => @profile.id) | 1509 | art1 = Article.create!(:name => 'some thing', :profile_id => @profile.id) |
1509 | - assert_equal Article.find_by_contents('thing'), [art1] | 1510 | + assert_equal Article.find_by_contents('thing')[:results], [art1] |
1510 | uses_postgresql 'schema_two' | 1511 | uses_postgresql 'schema_two' |
1511 | art2 = Article.create!(:name => 'another thing', :profile_id => @profile.id) | 1512 | art2 = Article.create!(:name => 'another thing', :profile_id => @profile.id) |
1512 | - assert_not_includes Article.find_by_contents('thing'), art1 | ||
1513 | - assert_includes Article.find_by_contents('thing'), art2 | 1513 | + assert_not_includes Article.find_by_contents('thing')[:results], art1 |
1514 | + assert_includes Article.find_by_contents('thing')[:results], art2 | ||
1514 | uses_postgresql 'schema_one' | 1515 | uses_postgresql 'schema_one' |
1515 | - assert_includes Article.find_by_contents('thing'), art1 | ||
1516 | - assert_not_includes Article.find_by_contents('thing'), art2 | 1516 | + assert_includes Article.find_by_contents('thing')[:results], art1 |
1517 | + assert_not_includes Article.find_by_contents('thing')[:results], art2 | ||
1517 | uses_sqlite | 1518 | uses_sqlite |
1518 | end | 1519 | end |
1519 | 1520 | ||
1520 | should 'not index by schema name when database is not postgresql' do | 1521 | should 'not index by schema name when database is not postgresql' do |
1521 | uses_sqlite | 1522 | uses_sqlite |
1522 | art1 = Article.create!(:name => 'some thing', :profile_id => @profile.id) | 1523 | art1 = Article.create!(:name => 'some thing', :profile_id => @profile.id) |
1523 | - assert_equal Article.find_by_contents('thing'), [art1] | 1524 | + assert_equal Article.find_by_contents('thing')[:results], [art1] |
1524 | art2 = Article.create!(:name => 'another thing', :profile_id => @profile.id) | 1525 | art2 = Article.create!(:name => 'another thing', :profile_id => @profile.id) |
1525 | - assert_includes Article.find_by_contents('thing'), art1 | ||
1526 | - assert_includes Article.find_by_contents('thing'), art2 | 1526 | + assert_includes Article.find_by_contents('thing')[:results], art1 |
1527 | + assert_includes Article.find_by_contents('thing')[:results], art2 | ||
1527 | end | 1528 | end |
1528 | 1529 | ||
1529 | should 'get images paths in article body' do | 1530 | should 'get images paths in article body' do |
test/unit/category_finder_test.rb
@@ -3,11 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,11 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class CategoryFinderTest < ActiveSupport::TestCase | 3 | class CategoryFinderTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | def setup | 5 | def setup |
6 | + Test::Unit::TestCase::setup | ||
6 | @category = Category.create!(:name => 'my category', :environment => Environment.default) | 7 | @category = Category.create!(:name => 'my category', :environment => Environment.default) |
7 | @finder = CategoryFinder.new(@category) | 8 | @finder = CategoryFinder.new(@category) |
8 | @product_category = fast_create(ProductCategory, :name => 'Products') | 9 | @product_category = fast_create(ProductCategory, :name => 'Products') |
9 | 10 | ||
10 | - Profile.rebuild_index | 11 | + Profile.rebuild_solr_index |
11 | end | 12 | end |
12 | 13 | ||
13 | should 'search for articles in a specific category' do | 14 | should 'search for articles in a specific category' do |
test/unit/enterprise_test.rb
@@ -4,6 +4,7 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -4,6 +4,7 @@ class EnterpriseTest < Test::Unit::TestCase | ||
4 | fixtures :profiles, :environments, :users | 4 | fixtures :profiles, :environments, :users |
5 | 5 | ||
6 | def setup | 6 | def setup |
7 | + Test::Unit::TestCase::setup | ||
7 | @product_category = fast_create(ProductCategory, :name => 'Products') | 8 | @product_category = fast_create(ProductCategory, :name => 'Products') |
8 | end | 9 | end |
9 | 10 | ||
@@ -91,7 +92,7 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -91,7 +92,7 @@ class EnterpriseTest < Test::Unit::TestCase | ||
91 | 92 | ||
92 | ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') | 93 | ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') |
93 | 94 | ||
94 | - result = Enterprise.find_by_contents(prod_cat.name) | 95 | + result = Enterprise.find_by_contents(prod_cat.name)[:results] |
95 | 96 | ||
96 | assert_includes result, ent1 | 97 | assert_includes result, ent1 |
97 | assert_not_includes result, ent2 | 98 | assert_not_includes result, ent2 |
@@ -105,7 +106,7 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -105,7 +106,7 @@ class EnterpriseTest < Test::Unit::TestCase | ||
105 | 106 | ||
106 | ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') | 107 | ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') |
107 | 108 | ||
108 | - result = Enterprise.find_by_contents(prod_cat.name) | 109 | + result = Enterprise.find_by_contents(prod_cat.name)[:results] |
109 | 110 | ||
110 | assert_includes result, ent1 | 111 | assert_includes result, ent1 |
111 | assert_not_includes result, ent2 | 112 | assert_not_includes result, ent2 |
@@ -406,6 +407,12 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -406,6 +407,12 @@ class EnterpriseTest < Test::Unit::TestCase | ||
406 | 407 | ||
407 | assert_equal product.inputs, enterprise.inputs | 408 | assert_equal product.inputs, enterprise.inputs |
408 | end | 409 | end |
410 | + | ||
411 | + should 'reindex when products are changed' do | ||
412 | + a = Enterprise.new | ||
413 | + a.expects(:solr_save) | ||
414 | + a.product_updated | ||
415 | + end | ||
409 | 416 | ||
410 | should "the followed_by? be true only to members" do | 417 | should "the followed_by? be true only to members" do |
411 | e = fast_create(Enterprise) | 418 | e = fast_create(Enterprise) |
test/unit/environment_finder_test.rb
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class EnvironmentFinderTest < ActiveSupport::TestCase | 3 | class EnvironmentFinderTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | def setup | 5 | def setup |
6 | + Test::Unit::TestCase::setup | ||
6 | @product_category = fast_create(ProductCategory, :name => 'Products') | 7 | @product_category = fast_create(ProductCategory, :name => 'Products') |
7 | end | 8 | end |
8 | 9 |
test/unit/environment_test.rb
@@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class EnvironmentTest < Test::Unit::TestCase | 3 | class EnvironmentTest < Test::Unit::TestCase |
4 | fixtures :environments | 4 | fixtures :environments |
5 | 5 | ||
6 | + def setup | ||
7 | + Test::Unit::TestCase::setup | ||
8 | + end | ||
9 | + | ||
6 | def test_exists_default_and_it_is_unique | 10 | def test_exists_default_and_it_is_unique |
7 | Environment.delete_all | 11 | Environment.delete_all |
8 | vc = Environment.new(:name => 'Test Community') | 12 | vc = Environment.new(:name => 'Test Community') |
@@ -444,7 +448,7 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -444,7 +448,7 @@ class EnvironmentTest < Test::Unit::TestCase | ||
444 | should 'find by contents from articles' do | 448 | should 'find by contents from articles' do |
445 | environment = fast_create(Environment) | 449 | environment = fast_create(Environment) |
446 | assert_nothing_raised do | 450 | assert_nothing_raised do |
447 | - environment.articles.find_by_contents('') | 451 | + environment.articles.find_by_contents('')[:results] |
448 | end | 452 | end |
449 | end | 453 | end |
450 | 454 | ||
@@ -561,7 +565,7 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -561,7 +565,7 @@ class EnvironmentTest < Test::Unit::TestCase | ||
561 | Enterprise.create!(:name => 'test ' + n, :identifier => 'test_' + n) | 565 | Enterprise.create!(:name => 'test ' + n, :identifier => 'test_' + n) |
562 | end | 566 | end |
563 | 567 | ||
564 | - assert_equal 20, env.enterprises.find_by_contents('test').total_entries | 568 | + assert_equal 20, env.enterprises.find_by_contents('test')[:results].total_entries |
565 | end | 569 | end |
566 | 570 | ||
567 | should 'set replace_enterprise_template_when_enable on environment' do | 571 | should 'set replace_enterprise_template_when_enable on environment' do |
test/unit/event_test.rb
@@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
2 | 2 | ||
3 | class EventTest < ActiveSupport::TestCase | 3 | class EventTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | + def setup | ||
6 | + Test::Unit::TestCase::setup | ||
7 | + end | ||
8 | + | ||
5 | should 'be an article' do | 9 | should 'be an article' do |
6 | assert_kind_of Article, Event.new | 10 | assert_kind_of Article, Event.new |
7 | end | 11 | end |
@@ -59,13 +63,13 @@ class EventTest < ActiveSupport::TestCase | @@ -59,13 +63,13 @@ class EventTest < ActiveSupport::TestCase | ||
59 | should 'be indexed by title' do | 63 | should 'be indexed by title' do |
60 | profile = create_user('testuser').person | 64 | profile = create_user('testuser').person |
61 | e = Event.create!(:name => 'my surprisingly nice event', :start_date => Date.new(2008, 06, 06), :profile => profile) | 65 | e = Event.create!(:name => 'my surprisingly nice event', :start_date => Date.new(2008, 06, 06), :profile => profile) |
62 | - assert_includes Event.find_by_contents('surprisingly'), e | 66 | + assert_includes Event.find_by_contents('surprisingly')[:results], e |
63 | end | 67 | end |
64 | 68 | ||
65 | should 'be indexed by body' do | 69 | should 'be indexed by body' do |
66 | profile = create_user('testuser').person | 70 | profile = create_user('testuser').person |
67 | e = Event.create!(:name => 'bli', :start_date => Date.new(2008, 06, 06), :profile => profile, :body => 'my surprisingly long description about my freaking nice event') | 71 | e = Event.create!(:name => 'bli', :start_date => Date.new(2008, 06, 06), :profile => profile, :body => 'my surprisingly long description about my freaking nice event') |
68 | - assert_includes Event.find_by_contents('surprisingly'), e | 72 | + assert_includes Event.find_by_contents('surprisingly')[:results], e |
69 | end | 73 | end |
70 | 74 | ||
71 | should 'use its own icon' do | 75 | should 'use its own icon' do |
test/unit/product_test.rb
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class ProductTest < Test::Unit::TestCase | 3 | class ProductTest < Test::Unit::TestCase |
4 | 4 | ||
5 | def setup | 5 | def setup |
6 | + Test::Unit::TestCase::setup | ||
6 | @product_category = fast_create(ProductCategory, :name => 'Products') | 7 | @product_category = fast_create(ProductCategory, :name => 'Products') |
7 | end | 8 | end |
8 | 9 | ||
@@ -92,7 +93,7 @@ class ProductTest < Test::Unit::TestCase | @@ -92,7 +93,7 @@ class ProductTest < Test::Unit::TestCase | ||
92 | p.stubs(:category_full_name).returns('interesting category') | 93 | p.stubs(:category_full_name).returns('interesting category') |
93 | p.save! | 94 | p.save! |
94 | 95 | ||
95 | - assert_includes Product.find_by_contents('interesting'), p | 96 | + assert_includes Product.find_by_contents('interesting')[:results], p |
96 | end | 97 | end |
97 | 98 | ||
98 | should 'have same lat and lng of its enterprise' do | 99 | should 'have same lat and lng of its enterprise' do |
@@ -355,24 +356,24 @@ class ProductTest < Test::Unit::TestCase | @@ -355,24 +356,24 @@ class ProductTest < Test::Unit::TestCase | ||
355 | should 'index by schema name when database is postgresql' do | 356 | should 'index by schema name when database is postgresql' do |
356 | uses_postgresql 'schema_one' | 357 | uses_postgresql 'schema_one' |
357 | p1 = Product.create!(:name => 'some thing', :product_category => @product_category) | 358 | p1 = Product.create!(:name => 'some thing', :product_category => @product_category) |
358 | - assert_equal Product.find_by_contents('thing'), [p1] | 359 | + assert_equal Product.find_by_contents('thing')[:results], [p1] |
359 | uses_postgresql 'schema_two' | 360 | uses_postgresql 'schema_two' |
360 | p2 = Product.create!(:name => 'another thing', :product_category => @product_category) | 361 | p2 = Product.create!(:name => 'another thing', :product_category => @product_category) |
361 | - assert_not_includes Product.find_by_contents('thing'), p1 | ||
362 | - assert_includes Product.find_by_contents('thing'), p2 | 362 | + assert_not_includes Product.find_by_contents('thing')[:results], p1 |
363 | + assert_includes Product.find_by_contents('thing')[:results], p2 | ||
363 | uses_postgresql 'schema_one' | 364 | uses_postgresql 'schema_one' |
364 | - assert_includes Product.find_by_contents('thing'), p1 | ||
365 | - assert_not_includes Product.find_by_contents('thing'), p2 | 365 | + assert_includes Product.find_by_contents('thing')[:results], p1 |
366 | + assert_not_includes Product.find_by_contents('thing')[:results], p2 | ||
366 | uses_sqlite | 367 | uses_sqlite |
367 | end | 368 | end |
368 | 369 | ||
369 | should 'not index by schema name when database is not postgresql' do | 370 | should 'not index by schema name when database is not postgresql' do |
370 | uses_sqlite | 371 | uses_sqlite |
371 | p1 = Product.create!(:name => 'some thing', :product_category => @product_category) | 372 | p1 = Product.create!(:name => 'some thing', :product_category => @product_category) |
372 | - assert_equal Product.find_by_contents('thing'), [p1] | 373 | + assert_equal Product.find_by_contents('thing')[:results], [p1] |
373 | p2 = Product.create!(:name => 'another thing', :product_category => @product_category) | 374 | p2 = Product.create!(:name => 'another thing', :product_category => @product_category) |
374 | - assert_includes Product.find_by_contents('thing'), p1 | ||
375 | - assert_includes Product.find_by_contents('thing'), p2 | 375 | + assert_includes Product.find_by_contents('thing')[:results], p1 |
376 | + assert_includes Product.find_by_contents('thing')[:results], p2 | ||
376 | end | 377 | end |
377 | 378 | ||
378 | end | 379 | end |
test/unit/profile_test.rb
@@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class ProfileTest < Test::Unit::TestCase | 3 | class ProfileTest < Test::Unit::TestCase |
4 | fixtures :profiles, :environments, :users, :roles, :domains | 4 | fixtures :profiles, :environments, :users, :roles, :domains |
5 | 5 | ||
6 | + def setup | ||
7 | + Test::Unit::TestCase::setup | ||
8 | + end | ||
9 | + | ||
6 | def test_identifier_validation | 10 | def test_identifier_validation |
7 | p = Profile.new | 11 | p = Profile.new |
8 | p.valid? | 12 | p.valid? |
@@ -100,8 +104,8 @@ class ProfileTest < Test::Unit::TestCase | @@ -100,8 +104,8 @@ class ProfileTest < Test::Unit::TestCase | ||
100 | def test_find_by_contents | 104 | def test_find_by_contents |
101 | p = create(Profile, :name => 'wanted') | 105 | p = create(Profile, :name => 'wanted') |
102 | 106 | ||
103 | - assert Profile.find_by_contents('wanted').include?(p) | ||
104 | - assert ! Profile.find_by_contents('not_wanted').include?(p) | 107 | + assert Profile.find_by_contents('wanted')[:results].include?(p) |
108 | + assert ! Profile.find_by_contents('not_wanted')[:results].include?(p) | ||
105 | end | 109 | end |
106 | 110 | ||
107 | should 'remove pages when removing profile' do | 111 | should 'remove pages when removing profile' do |
@@ -192,10 +196,10 @@ class ProfileTest < Test::Unit::TestCase | @@ -192,10 +196,10 @@ class ProfileTest < Test::Unit::TestCase | ||
192 | small = create(Profile, :name => 'A small profile for testing') | 196 | small = create(Profile, :name => 'A small profile for testing') |
193 | big = create(Profile, :name => 'A big profile for testing') | 197 | big = create(Profile, :name => 'A big profile for testing') |
194 | 198 | ||
195 | - assert Profile.find_by_contents('small').include?(small) | ||
196 | - assert Profile.find_by_contents('big').include?(big) | 199 | + assert Profile.find_by_contents('small')[:results].include?(small) |
200 | + assert Profile.find_by_contents('big')[:results].include?(big) | ||
197 | 201 | ||
198 | - both = Profile.find_by_contents('profile testing') | 202 | + both = Profile.find_by_contents('profile testing')[:results] |
199 | assert both.include?(small) | 203 | assert both.include?(small) |
200 | assert both.include?(big) | 204 | assert both.include?(big) |
201 | end | 205 | end |
@@ -517,18 +521,18 @@ class ProfileTest < Test::Unit::TestCase | @@ -517,18 +521,18 @@ class ProfileTest < Test::Unit::TestCase | ||
517 | should 'actually index by results of extra_data_for_index' do | 521 | should 'actually index by results of extra_data_for_index' do |
518 | profile = TestingExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile') | 522 | profile = TestingExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile') |
519 | 523 | ||
520 | - assert_includes TestingExtraDataForIndex.find_by_contents('sample'), profile | 524 | + assert_includes TestingExtraDataForIndex.find_by_contents('sample')[:results], profile |
521 | end | 525 | end |
522 | 526 | ||
523 | should 'index profile identifier for searching' do | 527 | should 'index profile identifier for searching' do |
524 | Profile.destroy_all | 528 | Profile.destroy_all |
525 | p = create(Profile, :identifier => 'lalala') | 529 | p = create(Profile, :identifier => 'lalala') |
526 | - assert_includes Profile.find_by_contents('lalala'), p | 530 | + assert_includes Profile.find_by_contents('lalala')[:results], p |
527 | end | 531 | end |
528 | 532 | ||
529 | should 'index profile name for searching' do | 533 | should 'index profile name for searching' do |
530 | p = create(Profile, :name => 'Interesting Profile') | 534 | p = create(Profile, :name => 'Interesting Profile') |
531 | - assert_includes Profile.find_by_contents('interesting'), p | 535 | + assert_includes Profile.find_by_contents('interesting')[:results], p |
532 | end | 536 | end |
533 | 537 | ||
534 | should 'enabled by default on creation' do | 538 | should 'enabled by default on creation' do |
@@ -1671,24 +1675,24 @@ class ProfileTest < Test::Unit::TestCase | @@ -1671,24 +1675,24 @@ class ProfileTest < Test::Unit::TestCase | ||
1671 | should 'index by schema name when database is postgresql' do | 1675 | should 'index by schema name when database is postgresql' do |
1672 | uses_postgresql 'schema_one' | 1676 | uses_postgresql 'schema_one' |
1673 | p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing') | 1677 | p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing') |
1674 | - assert_equal Profile.find_by_contents('thing'), [p1] | 1678 | + assert_equal Profile.find_by_contents('thing')[:results], [p1] |
1675 | uses_postgresql 'schema_two' | 1679 | uses_postgresql 'schema_two' |
1676 | p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing') | 1680 | p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing') |
1677 | - assert_not_includes Profile.find_by_contents('thing'), p1 | ||
1678 | - assert_includes Profile.find_by_contents('thing'), p2 | 1681 | + assert_not_includes Profile.find_by_contents('thing')[:results], p1 |
1682 | + assert_includes Profile.find_by_contents('thing')[:results], p2 | ||
1679 | uses_postgresql 'schema_one' | 1683 | uses_postgresql 'schema_one' |
1680 | - assert_includes Profile.find_by_contents('thing'), p1 | ||
1681 | - assert_not_includes Profile.find_by_contents('thing'), p2 | 1684 | + assert_includes Profile.find_by_contents('thing')[:results], p1 |
1685 | + assert_not_includes Profile.find_by_contents('thing')[:results], p2 | ||
1682 | uses_sqlite | 1686 | uses_sqlite |
1683 | end | 1687 | end |
1684 | 1688 | ||
1685 | should 'not index by schema name when database is not postgresql' do | 1689 | should 'not index by schema name when database is not postgresql' do |
1686 | uses_sqlite | 1690 | uses_sqlite |
1687 | p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing') | 1691 | p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing') |
1688 | - assert_equal Profile.find_by_contents('thing'), [p1] | 1692 | + assert_equal Profile.find_by_contents('thing')[:results], [p1] |
1689 | p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing') | 1693 | p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing') |
1690 | - assert_includes Profile.find_by_contents('thing'), p1 | ||
1691 | - assert_includes Profile.find_by_contents('thing'), p2 | 1694 | + assert_includes Profile.find_by_contents('thing')[:results], p1 |
1695 | + assert_includes Profile.find_by_contents('thing')[:results], p2 | ||
1692 | end | 1696 | end |
1693 | 1697 | ||
1694 | should 'know if url is the profile homepage' do | 1698 | should 'know if url is the profile homepage' do |
test/unit/tiny_mce_article_test.rb
@@ -3,7 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,7 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class TinyMceArticleTest < Test::Unit::TestCase | 3 | class TinyMceArticleTest < Test::Unit::TestCase |
4 | 4 | ||
5 | def setup | 5 | def setup |
6 | - Article.rebuild_index | 6 | + Test::Unit::TestCase::setup |
7 | + Article.rebuild_solr_index | ||
7 | @profile = create_user('zezinho').person | 8 | @profile = create_user('zezinho').person |
8 | end | 9 | end |
9 | attr_reader :profile | 10 | attr_reader :profile |
@@ -23,8 +24,8 @@ class TinyMceArticleTest < Test::Unit::TestCase | @@ -23,8 +24,8 @@ class TinyMceArticleTest < Test::Unit::TestCase | ||
23 | 24 | ||
24 | should 'be found when searching for articles by query' do | 25 | should 'be found when searching for articles by query' do |
25 | tma = TinyMceArticle.create!(:name => 'test tinymce article', :body => '---', :profile => profile) | 26 | tma = TinyMceArticle.create!(:name => 'test tinymce article', :body => '---', :profile => profile) |
26 | - assert_includes TinyMceArticle.find_by_contents('article'), tma | ||
27 | - assert_includes Article.find_by_contents('article'), tma | 27 | + assert_includes TinyMceArticle.find_by_contents('article')[:results], tma |
28 | + assert_includes Article.find_by_contents('article')[:results], tma | ||
28 | end | 29 | end |
29 | 30 | ||
30 | should 'not sanitize target attribute' do | 31 | should 'not sanitize target attribute' do |
vendor/plugins/acts_as_solr/lib/parser_methods.rb
@@ -87,7 +87,7 @@ module ActsAsSolr #:nodoc: | @@ -87,7 +87,7 @@ module ActsAsSolr #:nodoc: | ||
87 | 87 | ||
88 | def solr_type_condition | 88 | def solr_type_condition |
89 | subclasses.inject("(#{solr_configuration[:type_field]}:#{self.name}") do |condition, subclass| | 89 | subclasses.inject("(#{solr_configuration[:type_field]}:#{self.name}") do |condition, subclass| |
90 | - condition << " OR #{solr_configuration[:type_field]}:#{subclass.name}" | 90 | + condition << (subclass.name.empty? ? "" : " OR #{solr_configuration[:type_field]}:#{subclass.name}") |
91 | end << ')' | 91 | end << ')' |
92 | end | 92 | end |
93 | 93 | ||
@@ -198,4 +198,4 @@ module ActsAsSolr #:nodoc: | @@ -198,4 +198,4 @@ module ActsAsSolr #:nodoc: | ||
198 | end | 198 | end |
199 | 199 | ||
200 | end | 200 | end |
201 | -end | ||
202 | \ No newline at end of file | 201 | \ No newline at end of file |
202 | +end |