Commit 9c9311bd1a49f25451d112d18696f9b57508f640

Authored by Rodrigo Souto
1 parent 82ae16fc

[pluginize-solr] Moving solr reindex to a plugin

Starting pluginization of solr!
app/models/article.rb
... ... @@ -469,10 +469,6 @@ class Article < ActiveRecord::Base
469 469 allow_post_content?(user) || user && allow_members_to_edit && user.is_member_of?(profile)
470 470 end
471 471  
472   - def comments_updated
473   - solr_save
474   - end
475   -
476 472 def accept_category?(cat)
477 473 !cat.is_a?(ProductCategory)
478 474 end
... ...
app/models/category.rb
... ... @@ -106,7 +106,6 @@ class Category < ActiveRecord::Base
106 106 # ordered/query-boosted fields
107 107 {:name_sortable => :string},
108 108 ]
109   - after_save_reindex [:articles, :profiles], :with => :delayed_job
110 109 handle_asynchronously :solr_save
111 110  
112 111 end
... ...
app/models/certifier.rb
... ... @@ -24,6 +24,4 @@ class Certifier < ActiveRecord::Base
24 24 self.name.downcase.transliterate <=> b.name.downcase.transliterate
25 25 end
26 26  
27   - after_save_reindex [:products], :with => :delayed_job
28   -
29 27 end
... ...
app/models/comment.rb
... ... @@ -74,12 +74,6 @@ class Comment &lt; ActiveRecord::Base
74 74 self.find(:all, :order => 'created_at desc, id desc', :limit => limit)
75 75 end
76 76  
77   - after_save :notify_article
78   - after_destroy :notify_article
79   - def notify_article
80   - article.comments_updated if article.kind_of?(Article)
81   - end
82   -
83 77 after_create :new_follower
84 78 def new_follower
85 79 if source.kind_of?(Article)
... ...
app/models/enterprise.rb
... ... @@ -14,7 +14,6 @@ class Enterprise &lt; Organization
14 14  
15 15 has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people'
16 16  
17   - after_save_reindex [:products], :with => :delayed_job
18 17 extra_data_for_index :product_categories
19 18 def product_categories
20 19 products.includes(:product_category).map{|p| p.category_full_name}.compact
... ...
app/models/product.rb
... ... @@ -294,6 +294,5 @@ class Product &lt; ActiveRecord::Base
294 294 ], :facets => facets_option_for_solr,
295 295 :boost => proc{ |p| boost = 1; Boosts.each{ |b| boost = boost * (1 - ((1 - b[2].call(p)) * b[1])) }; boost}
296 296 handle_asynchronously :solr_save
297   - after_save_reindex [:enterprise], :with => :delayed_job
298 297  
299 298 end
... ...
app/models/product_category.rb
... ... @@ -11,6 +11,4 @@ class ProductCategory &lt; Category
11 11 top_category ? top_category.children : top_level_for(env).select{|c|c.kind_of?(ProductCategory)}
12 12 end
13 13  
14   - after_save_reindex [:products], :with => :delayed_job
15   -
16 14 end
... ...
app/models/profile.rb
... ... @@ -963,7 +963,7 @@ private :generate_url, :url_options
963 963 {:categories => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
964 964 ], :facets => facets_option_for_solr,
965 965 :boost => proc{ |p| 10 if p.enabled }
966   - after_save_reindex [:articles], :with => :delayed_job
  966 +
967 967 handle_asynchronously :solr_save
968 968  
969 969 def control_panel_settings_button
... ...
app/models/qualifier.rb
... ... @@ -15,6 +15,4 @@ class Qualifier &lt; ActiveRecord::Base
15 15 self.name.downcase.transliterate <=> b.name.downcase.transliterate
16 16 end
17 17  
18   - after_save_reindex [:products], :with => :delayed_job
19   -
20 18 end
... ...
plugins/solr/lib/ext/article.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'article'
  2 +
  3 +class Article
  4 + def solr_plugin_comments_updated
  5 + solr_save
  6 + end
  7 +end
... ...
plugins/solr/lib/ext/category.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +require_dependency 'category'
  2 +
  3 +class Category
  4 + after_save_reindex [:articles, :profiles], :with => :delayed_job
  5 +end
... ...
plugins/solr/lib/ext/certifier.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +require_dependency 'certifier'
  2 +
  3 +class Certifier
  4 + after_save_reindex [:products], :with => :delayed_job
  5 +end
  6 +
... ...
plugins/solr/lib/ext/comment.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +require_dependency 'comment'
  2 +
  3 +class Comment
  4 + after_save :solr_plugin_notify_article
  5 + after_destroy :solr_plugin_notify_article
  6 +
  7 + def solr_plugin_notify_article
  8 + article.solr_plugin_comments_updated if article.kind_of?(Article)
  9 + end
  10 +end
... ...
plugins/solr/lib/ext/enterprise.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +require_dependency 'enterprise'
  2 +
  3 +class Enterprise
  4 + after_save_reindex [:products], :with => :delayed_job
  5 +end
... ...
plugins/solr/lib/ext/product.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +require_dependency 'product'
  2 +
  3 +class Product
  4 + after_save_reindex [:enterprise], :with => :delayed_job
  5 +end
... ...
plugins/solr/lib/ext/product_category.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +require_dependency 'product_category'
  2 +
  3 +class ProductCategory
  4 + after_save_reindex [:products], :with => :delayed_job
  5 +end
  6 +
... ...
plugins/solr/lib/ext/profile.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +require_dependency 'profile'
  2 +
  3 +class Profile
  4 + after_save_reindex [:articles], :with => :delayed_job
  5 +end
... ...
plugins/solr/lib/ext/qualifier.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +require_dependency 'qualifier'
  2 +
  3 +class Qualifier
  4 + after_save_reindex [:products], :with => :delayed_job
  5 +end
  6 +
... ...
plugins/solr/lib/solr_plugin.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class SolrPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + "Solr"
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("Uses Solr as search engine.")
  9 + end
  10 +
  11 +end
  12 +
  13 +Dir[File.join(SolrPlugin.root_path, 'lib', 'ext', '*.rb')].each {|file| require_dependency file }
... ...
plugins/solr/test/unit/category_test.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +require 'test_helper'
  2 +
  3 +class CategoryTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex articles after saving' do
  12 + cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  13 + art = Article.create!(:name => 'something', :profile_id => fast_create(Person).id)
  14 + art.add_category cat
  15 + cat.reload
  16 +
  17 + solr_doc = art.to_solr_doc
  18 + Article.any_instance.expects(:to_solr_doc).returns(solr_doc)
  19 + cat.save!
  20 + end
  21 +end
... ...
plugins/solr/test/unit/certifier_test.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +require 'test_helper'
  2 +
  3 +class CertifierTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex products after saving' do
  12 + product = mock
  13 + Certifier.any_instance.stubs(:products).returns([product])
  14 + Certifier.expects(:solr_batch_add).with(includes(product))
  15 + cert = fast_create(Certifier)
  16 + cert.save!
  17 + end
  18 +end
  19 +
... ...
plugins/solr/test/unit/comment_test.rb 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +require 'test_helper'
  2 +
  3 +class CommentTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'notify article to reindex after saving' do
  12 + owner = create_user('testuser').person
  13 + article = owner.articles.create!(:name => 'test', :body => '...')
  14 +
  15 + article.expects(:solr_plugin_comments_updated)
  16 +
  17 + c1 = article.comments.new(:title => "A comment", :body => '...', :author => owner)
  18 + c1.stubs(:article).returns(article)
  19 + c1.save!
  20 + end
  21 +
  22 + should 'notify article to reindex after being removed' do
  23 + owner = create_user('testuser').person
  24 + article = owner.articles.create!(:name => 'test', :body => '...')
  25 + c1 = article.comments.create!(:title => "A comment", :body => '...', :author => owner)
  26 +
  27 + c1.stubs(:article).returns(article)
  28 + article.expects(:solr_plugin_comments_updated)
  29 + c1.destroy
  30 + end
  31 +end
  32 +
... ...
plugins/solr/test/unit/enterprise_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require 'test_helper'
  2 +
  3 +class EnterpriseTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + @product_category = fast_create(ProductCategory)
  8 + end
  9 +
  10 + attr_accessor :environment, :product_category
  11 +
  12 + should 'reindex when products are changed' do
  13 + enterprise = fast_create(Enterprise)
  14 + product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => product_category.id)
  15 + Product.expects(:solr_batch_add_association).with(product, :enterprise)
  16 + product.update_attribute :name, "novo nome"
  17 + end
  18 +end
... ...
plugins/solr/test/unit/product_category_test.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +require 'test_helper'
  2 +
  3 +class ProductCategoryTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex products after save' do
  12 + product = mock
  13 + ProductCategory.any_instance.stubs(:products).returns([product])
  14 + ProductCategory.expects(:solr_batch_add).with(includes(product))
  15 + pc = fast_create(ProductCategory)
  16 + pc.save!
  17 + end
  18 +end
  19 +
... ...
plugins/solr/test/unit/product_test.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +require 'test_helper'
  2 +
  3 +class ProductTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex enterprise after saving' do
  12 + ent = fast_create(Enterprise)
  13 + cat = fast_create(ProductCategory)
  14 + prod = Product.create!(:name => 'something', :enterprise_id => ent.id, :product_category_id => cat.id)
  15 + Product.expects(:solr_batch_add).with([ent])
  16 + prod.save!
  17 + end
  18 +end
  19 +
... ...
plugins/solr/test/unit/profile_test.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +require 'test_helper'
  2 +
  3 +class ProfileTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex articles after saving' do
  12 + profile = create(Person, :name => 'something', :user_id => fast_create(User).id)
  13 + art = profile.articles.build(:name => 'something')
  14 + Profile.expects(:solr_batch_add).with(includes(art))
  15 + profile.save!
  16 + end
  17 +end
... ...
plugins/solr/test/unit/qualifier_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require 'test_helper'
  2 +
  3 +class QualifierTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(SolrPlugin)
  7 + end
  8 +
  9 + attr_accessor :environment
  10 +
  11 + should 'reindex products after saving' do
  12 + product = mock
  13 + Qualifier.any_instance.stubs(:products).returns([product])
  14 + Qualifier.expects(:solr_batch_add).with(includes(product))
  15 + qual = fast_create(Qualifier)
  16 + qual.save!
  17 + end
  18 +end
... ...
test/unit/article_test.rb
... ... @@ -357,12 +357,6 @@ class ArticleTest &lt; ActiveSupport::TestCase
357 357 assert_equal true, a.display_to?(person)
358 358 end
359 359  
360   - should 'reindex when comments are changed' do
361   - a = Article.new
362   - a.expects(:solr_save)
363   - a.comments_updated
364   - end
365   -
366 360 should 'index comments title together with article' do
367 361 TestSolr.enable
368 362 owner = create_user('testuser').person
... ...
test/unit/category_test.rb
... ... @@ -541,15 +541,4 @@ class CategoryTest &lt; ActiveSupport::TestCase
541 541 c.save!
542 542 end
543 543  
544   - should 'reindex articles after saving' do
545   - cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
546   - art = Article.create!(:name => 'something', :profile_id => fast_create(Person).id)
547   - art.add_category cat
548   - cat.reload
549   -
550   - solr_doc = art.to_solr_doc
551   - Article.any_instance.expects(:to_solr_doc).returns(solr_doc)
552   - cat.save!
553   - end
554   -
555 544 end
... ...
test/unit/certifier_test.rb
... ... @@ -58,14 +58,6 @@ class CertifierTest &lt; ActiveSupport::TestCase
58 58 assert_equal [first, last], Certifier.all.sort
59 59 end
60 60  
61   - should 'reindex products after saving' do
62   - product = mock
63   - Certifier.any_instance.stubs(:products).returns([product])
64   - Certifier.expects(:solr_batch_add).with(includes(product))
65   - cert = fast_create(Certifier)
66   - cert.save!
67   - end
68   -
69 61 should 'set qualifier as self-certified when destroyed' do
70 62 pq = mock
71 63 Certifier.any_instance.stubs(:product_qualifiers).returns([pq])
... ...
test/unit/comment_test.rb
... ... @@ -175,27 +175,6 @@ class CommentTest &lt; ActiveSupport::TestCase
175 175 assert c.errors.invalid?(:email)
176 176 end
177 177  
178   - should 'notify article to reindex after saving' do
179   - owner = create_user('testuser').person
180   - article = owner.articles.create!(:name => 'test', :body => '...')
181   -
182   - article.expects(:comments_updated)
183   -
184   - c1 = article.comments.new(:title => "A comment", :body => '...', :author => owner)
185   - c1.stubs(:article).returns(article)
186   - c1.save!
187   - end
188   -
189   - should 'notify article to reindex after being removed' do
190   - owner = create_user('testuser').person
191   - article = owner.articles.create!(:name => 'test', :body => '...')
192   - c1 = article.comments.create!(:title => "A comment", :body => '...', :author => owner)
193   -
194   - c1.stubs(:article).returns(article)
195   - article.expects(:comments_updated)
196   - c1.destroy
197   - end
198   -
199 178 should 'generate links to comments on images with view set to true' do
200 179 owner = create_user('testuser').person
201 180 image = UploadedFile.create!(:profile => owner, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
... ...
test/unit/enterprise_test.rb
... ... @@ -443,13 +443,6 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
443 443 assert_equal product.inputs, enterprise.inputs
444 444 end
445 445  
446   - should 'reindex when products are changed' do
447   - enterprise = fast_create(Enterprise)
448   - product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => @product_category.id)
449   - Product.expects(:solr_batch_add_association).with(product, :enterprise)
450   - product.update_attribute :name, "novo nome"
451   - end
452   -
453 446 should "the followed_by? be true only to members" do
454 447 e = fast_create(Enterprise)
455 448 e.stubs(:closed?).returns(false)
... ... @@ -480,17 +473,6 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
480 473 assert_respond_to e, :production_costs
481 474 end
482 475  
483   - should 'reindex products with full category name after save' do
484   - product = mock
485   - products = mock
486   - product.expects(:category_full_name)
487   - products.stubs(:includes).returns([product])
488   - Enterprise.any_instance.stubs(:products).returns(products)
489   - Enterprise.expects(:solr_batch_add).with([products])
490   - ent = fast_create(Enterprise)
491   - ent.save!
492   - end
493   -
494 476 should 'return scraps as activities' do
495 477 person = fast_create(Person)
496 478 enterprise = fast_create(Enterprise)
... ...
test/unit/product_category_test.rb
... ... @@ -36,12 +36,4 @@ class ProductCategoryTest &lt; ActiveSupport::TestCase
36 36 assert_equal [c11], ProductCategory.menu_categories(c1, nil)
37 37 end
38 38  
39   - should 'reindex products after save' do
40   - product = mock
41   - ProductCategory.any_instance.stubs(:products).returns([product])
42   - ProductCategory.expects(:solr_batch_add).with(includes(product))
43   - pc = fast_create(ProductCategory)
44   - pc.save!
45   - end
46   -
47 39 end
... ...
test/unit/product_test.rb
... ... @@ -636,14 +636,6 @@ class ProductTest &lt; ActiveSupport::TestCase
636 636 assert_equal [in_name, in_desc], Product.find_by_contents('bananas')[:results].docs
637 637 end
638 638  
639   - should 'reindex enterprise after saving' do
640   - ent = fast_create(Enterprise)
641   - cat = fast_create(ProductCategory)
642   - prod = Product.create!(:name => 'something', :enterprise_id => ent.id, :product_category_id => cat.id)
643   - Product.expects(:solr_batch_add).with([ent])
644   - prod.save!
645   - end
646   -
647 639 should 'boost search results that include an image' do
648 640 TestSolr.enable
649 641 product_without_image = Product.create!(:name => 'product without image', :product_category => @product_category,
... ...
test/unit/profile_test.rb
... ... @@ -1909,13 +1909,6 @@ class ProfileTest &lt; ActiveSupport::TestCase
1909 1909 assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs
1910 1910 end
1911 1911  
1912   - should 'reindex articles after saving' do
1913   - profile = create(Person, :name => 'something', :user_id => fast_create(User).id)
1914   - art = profile.articles.build(:name => 'something')
1915   - Profile.expects(:solr_batch_add).with(includes(art))
1916   - profile.save!
1917   - end
1918   -
1919 1912 should 'respond to redirection_after_login' do
1920 1913 assert_respond_to Profile.new, :redirection_after_login
1921 1914 end
... ...
test/unit/qualifier_test.rb
... ... @@ -60,12 +60,4 @@ class QualifierTest &lt; ActiveSupport::TestCase
60 60 Qualifier.destroy_all
61 61 assert_equal [], product1.product_qualifiers(true)
62 62 end
63   -
64   - should 'reindex products after saving' do
65   - product = mock
66   - Qualifier.any_instance.stubs(:products).returns([product])
67   - Qualifier.expects(:solr_batch_add).with(includes(product))
68   - qual = fast_create(Qualifier)
69   - qual.save!
70   - end
71 63 end
... ...