Commit 56f7c497e0f6e0739e7e676e8fbf6d6481bcdb6c

Authored by Rodrigo Souto
1 parent b1f54f79

[pluginize-solr] Moving extra_data_for_index to plugin

app/models/enterprise.rb
... ... @@ -14,7 +14,6 @@ class Enterprise < Organization
14 14  
15 15 has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people'
16 16  
17   - extra_data_for_index :product_categories
18 17 def product_categories
19 18 products.includes(:product_category).map{|p| p.category_full_name}.compact
20 19 end
... ...
app/models/profile.rb
... ... @@ -127,18 +127,6 @@ class Profile < ActiveRecord::Base
127 127 scrap.nil? ? Scrap.all_scraps(self) : Scrap.all_scraps(self).find(scrap)
128 128 end
129 129  
130   - class_inheritable_accessor :extra_index_methods
131   - self.extra_index_methods = []
132   -
133   - def extra_data_for_index
134   - self.class.extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten
135   - end
136   -
137   - def self.extra_data_for_index(sym = nil, &block)
138   - self.extra_index_methods.push(sym) if sym
139   - self.extra_index_methods.push(block) if block_given?
140   - end
141   -
142 130 acts_as_having_settings :field => :data
143 131  
144 132 settings_items :redirect_l10n, :type => :boolean, :default => false
... ...
plugins/solr/lib/ext/enterprise.rb
... ... @@ -2,4 +2,5 @@ require_dependency 'enterprise'
2 2  
3 3 class Enterprise
4 4 after_save_reindex [:products], :with => :delayed_job
  5 + solr_plugin_extra_data_for_index :product_categories
5 6 end
... ...
plugins/solr/lib/ext/profile.rb
... ... @@ -12,7 +12,7 @@ class Profile
12 12 }, :category_query => proc { |c| "solr_plugin_category_filter:#{c.id}" },
13 13 :order => [:solr_plugin_f_region, :solr_plugin_f_categories, :solr_plugin_f_enabled]
14 14  
15   - acts_as_searchable :fields => facets_fields_for_solr + [:extra_data_for_index,
  15 + acts_as_searchable :fields => facets_fields_for_solr + [:solr_plugin_extra_data_for_index,
16 16 # searched fields
17 17 {:name => {:type => :text, :boost => 2.0}},
18 18 {:identifier => :text}, {:nickname => :text},
... ... @@ -31,6 +31,18 @@ class Profile
31 31 ], :facets => facets_option_for_solr,
32 32 :boost => proc{ |p| 10 if p.enabled }
33 33  
  34 + class_inheritable_accessor :solr_plugin_extra_index_methods
  35 + self.solr_plugin_extra_index_methods = []
  36 +
  37 + def solr_plugin_extra_data_for_index
  38 + self.class.solr_plugin_extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten
  39 + end
  40 +
  41 + def self.solr_plugin_extra_data_for_index(sym = nil, &block)
  42 + self.solr_plugin_extra_index_methods.push(sym) if sym
  43 + self.solr_plugin_extra_index_methods.push(block) if block_given?
  44 + end
  45 +
34 46 private
35 47  
36 48 def self.solr_plugin_f_categories_label_proc(environment)
... ...
plugins/solr/test/unit/profile_test.rb
... ... @@ -55,4 +55,34 @@ class ProfileTest < ActiveSupport::TestCase
55 55 in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id)
56 56 assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs
57 57 end
  58 +
  59 + should 'be able to add extra data for index' do
  60 + klass = Class.new(Profile)
  61 + klass.any_instance.expects(:random_method)
  62 + klass.solr_plugin_extra_data_for_index :random_method
  63 +
  64 + klass.new.solr_plugin_extra_data_for_index
  65 + end
  66 +
  67 + should 'be able to add a block as extra data for index' do
  68 + klass = Class.new(Profile)
  69 + result = Object.new
  70 + klass.solr_plugin_extra_data_for_index do |obj|
  71 + result
  72 + end
  73 +
  74 + assert_includes klass.new.solr_plugin_extra_data_for_index, result
  75 + end
  76 +
  77 + should 'actually index by results of solr_plugin_extra_data_for_index' do
  78 + TestSolr.enable
  79 + class ExtraDataForIndex < Profile
  80 + solr_plugin_extra_data_for_index do |obj|
  81 + 'sample indexed text'
  82 + end
  83 + end
  84 + profile = ExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile')
  85 +
  86 + assert_includes ExtraDataForIndex.find_by_contents('sample')[:results], profile
  87 + end
58 88 end
... ...
test/mocks/test/testing_extra_data_for_index.rb
... ... @@ -1,5 +0,0 @@
1   -class TestingExtraDataForIndex < Profile
2   - extra_data_for_index do |obj|
3   - 'sample indexed text'
4   - end
5   -end
test/unit/profile_test.rb
... ... @@ -498,34 +498,6 @@ class ProfileTest &lt; ActiveSupport::TestCase
498 498 assert p.display_info_to?(admin)
499 499 end
500 500  
501   - should 'be able to add extra data for index' do
502   - klass = Class.new(Profile)
503   - klass.any_instance.expects(:random_method)
504   - klass.extra_data_for_index :random_method
505   -
506   - klass.new.extra_data_for_index
507   - end
508   -
509   - should 'be able to add a block as extra data for index' do
510   - klass = Class.new(Profile)
511   - result = Object.new
512   - klass.extra_data_for_index do |obj|
513   - result
514   - end
515   -
516   - assert_includes klass.new.extra_data_for_index, result
517   - end
518   -
519   - # TestingExtraDataForIndex is a subclass of profile that adds a block as
520   - # content to be added to the index. The block returns "sample indexed text"
521   - # see test/mocks/test/testing_extra_data_for_index.rb
522   - should 'actually index by results of extra_data_for_index' do
523   - TestSolr.enable
524   - profile = TestingExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile')
525   -
526   - assert_includes TestingExtraDataForIndex.find_by_contents('sample')[:results], profile
527   - end
528   -
529 501 should 'index profile identifier for searching' do
530 502 TestSolr.enable
531 503 Profile.destroy_all
... ...