From 56f7c497e0f6e0739e7e676e8fbf6d6481bcdb6c Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 12 Dec 2012 00:23:53 +0000 Subject: [PATCH] [pluginize-solr] Moving extra_data_for_index to plugin --- app/models/enterprise.rb | 1 - app/models/profile.rb | 12 ------------ plugins/solr/lib/ext/enterprise.rb | 1 + plugins/solr/lib/ext/profile.rb | 14 +++++++++++++- plugins/solr/test/unit/profile_test.rb | 30 ++++++++++++++++++++++++++++++ test/mocks/test/testing_extra_data_for_index.rb | 5 ----- test/unit/profile_test.rb | 28 ---------------------------- 7 files changed, 44 insertions(+), 47 deletions(-) delete mode 100644 test/mocks/test/testing_extra_data_for_index.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index bbcf53c..c22ec27 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -14,7 +14,6 @@ class Enterprise < Organization has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people' - extra_data_for_index :product_categories def product_categories products.includes(:product_category).map{|p| p.category_full_name}.compact end diff --git a/app/models/profile.rb b/app/models/profile.rb index 3fde3f6..616d02d 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -127,18 +127,6 @@ class Profile < ActiveRecord::Base scrap.nil? ? Scrap.all_scraps(self) : Scrap.all_scraps(self).find(scrap) end - class_inheritable_accessor :extra_index_methods - self.extra_index_methods = [] - - def extra_data_for_index - self.class.extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten - end - - def self.extra_data_for_index(sym = nil, &block) - self.extra_index_methods.push(sym) if sym - self.extra_index_methods.push(block) if block_given? - end - acts_as_having_settings :field => :data settings_items :redirect_l10n, :type => :boolean, :default => false diff --git a/plugins/solr/lib/ext/enterprise.rb b/plugins/solr/lib/ext/enterprise.rb index 4bb35f7..609efe7 100644 --- a/plugins/solr/lib/ext/enterprise.rb +++ b/plugins/solr/lib/ext/enterprise.rb @@ -2,4 +2,5 @@ require_dependency 'enterprise' class Enterprise after_save_reindex [:products], :with => :delayed_job + solr_plugin_extra_data_for_index :product_categories end diff --git a/plugins/solr/lib/ext/profile.rb b/plugins/solr/lib/ext/profile.rb index 4a3f146..879c252 100644 --- a/plugins/solr/lib/ext/profile.rb +++ b/plugins/solr/lib/ext/profile.rb @@ -12,7 +12,7 @@ class Profile }, :category_query => proc { |c| "solr_plugin_category_filter:#{c.id}" }, :order => [:solr_plugin_f_region, :solr_plugin_f_categories, :solr_plugin_f_enabled] - acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index, + acts_as_searchable :fields => facets_fields_for_solr + [:solr_plugin_extra_data_for_index, # searched fields {:name => {:type => :text, :boost => 2.0}}, {:identifier => :text}, {:nickname => :text}, @@ -31,6 +31,18 @@ class Profile ], :facets => facets_option_for_solr, :boost => proc{ |p| 10 if p.enabled } + class_inheritable_accessor :solr_plugin_extra_index_methods + self.solr_plugin_extra_index_methods = [] + + def solr_plugin_extra_data_for_index + self.class.solr_plugin_extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten + end + + def self.solr_plugin_extra_data_for_index(sym = nil, &block) + self.solr_plugin_extra_index_methods.push(sym) if sym + self.solr_plugin_extra_index_methods.push(block) if block_given? + end + private def self.solr_plugin_f_categories_label_proc(environment) diff --git a/plugins/solr/test/unit/profile_test.rb b/plugins/solr/test/unit/profile_test.rb index fdcf027..a7f9ce6 100644 --- a/plugins/solr/test/unit/profile_test.rb +++ b/plugins/solr/test/unit/profile_test.rb @@ -55,4 +55,34 @@ class ProfileTest < ActiveSupport::TestCase in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id) assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs end + + should 'be able to add extra data for index' do + klass = Class.new(Profile) + klass.any_instance.expects(:random_method) + klass.solr_plugin_extra_data_for_index :random_method + + klass.new.solr_plugin_extra_data_for_index + end + + should 'be able to add a block as extra data for index' do + klass = Class.new(Profile) + result = Object.new + klass.solr_plugin_extra_data_for_index do |obj| + result + end + + assert_includes klass.new.solr_plugin_extra_data_for_index, result + end + + should 'actually index by results of solr_plugin_extra_data_for_index' do + TestSolr.enable + class ExtraDataForIndex < Profile + solr_plugin_extra_data_for_index do |obj| + 'sample indexed text' + end + end + profile = ExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile') + + assert_includes ExtraDataForIndex.find_by_contents('sample')[:results], profile + end end diff --git a/test/mocks/test/testing_extra_data_for_index.rb b/test/mocks/test/testing_extra_data_for_index.rb deleted file mode 100644 index 11f4bb5..0000000 --- a/test/mocks/test/testing_extra_data_for_index.rb +++ /dev/null @@ -1,5 +0,0 @@ -class TestingExtraDataForIndex < Profile - extra_data_for_index do |obj| - 'sample indexed text' - end -end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index cba49cc..689eb2b 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -498,34 +498,6 @@ class ProfileTest < ActiveSupport::TestCase assert p.display_info_to?(admin) end - should 'be able to add extra data for index' do - klass = Class.new(Profile) - klass.any_instance.expects(:random_method) - klass.extra_data_for_index :random_method - - klass.new.extra_data_for_index - end - - should 'be able to add a block as extra data for index' do - klass = Class.new(Profile) - result = Object.new - klass.extra_data_for_index do |obj| - result - end - - assert_includes klass.new.extra_data_for_index, result - end - - # TestingExtraDataForIndex is a subclass of profile that adds a block as - # content to be added to the index. The block returns "sample indexed text" - # see test/mocks/test/testing_extra_data_for_index.rb - should 'actually index by results of extra_data_for_index' do - TestSolr.enable - profile = TestingExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile') - - assert_includes TestingExtraDataForIndex.find_by_contents('sample')[:results], profile - end - should 'index profile identifier for searching' do TestSolr.enable Profile.destroy_all -- libgit2 0.21.2