Permalink
Browse files

Making sure indexes are still tracked when classes are cached.

  • Loading branch information...
1 parent e12e823 commit 1506e6bf4c7b39fc6b0e9e6b54f1a7561c3166a1 @pat pat committed Nov 29, 2009
@@ -177,6 +177,12 @@ def add_sphinx_index(index)
subclasses.each { |klass| klass.add_sphinx_index(index) }
end
+ def has_sphinx_indexes?
+ sphinx_indexes &&
+ sphinx_index_blocks &&
+ (sphinx_indexes.length > 0 || sphinx_index_blocks.length > 0)
+ end
+
def indexed_by_sphinx?
sphinx_indexes && sphinx_indexes.length > 0
end
@@ -6,7 +6,8 @@ def initialize
end
def prepare
- load_models
+ load_models unless cached?
+ add_indexed_models if cached?
end
def define_indexes
@@ -32,15 +33,23 @@ def superclass_indexed_models
private
+ def cached?
+ defined?(Rails) &&
+ Rails::VERSION::STRING.to_f > 2.1 &&
+ Rails.configuration.cache_classes
+ end
+
+ def add_indexed_models
+ Object.subclasses_of(ActiveRecord::Base).each do |klass|
+ add_indexed_model klass if klass.has_sphinx_indexes?
+ end
+ end
+
# Make sure all models are loaded - without reloading any that
# ActiveRecord::Base is already aware of (otherwise we start to hit some
# messy dependencies issues).
#
def load_models
- return if defined?(Rails) &&
- Rails::VERSION::STRING.to_f > 2.1 &&
- Rails.configuration.cache_classes
-
ThinkingSphinx::Configuration.instance.model_directories.each do |base|
Dir["#{base}**/*.rb"].each do |file|
model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')
@@ -16,6 +16,9 @@
Alpha.sphinx_indexes.replace @existing_alpha_indexes
Beta.sphinx_indexes.replace @existing_beta_indexes
+ Alpha.send :defined_indexes=, true
+ Beta.send :defined_indexes=, true
+
Alpha.sphinx_index_blocks.clear
Beta.sphinx_index_blocks.clear
end
@@ -585,4 +588,27 @@
Parent.sphinx_offset.should == 1
end
end
+
+ describe '.has_sphinx_indexes?' do
+ it "should return true if there are sphinx indexes defined" do
+ Alpha.sphinx_indexes.replace [stub('index')]
+ Alpha.sphinx_index_blocks.replace []
+
+ Alpha.should have_sphinx_indexes
+ end
+
+ it "should return true if there are sphinx index blocks defined" do
+ Alpha.sphinx_indexes.replace []
+ Alpha.sphinx_index_blocks.replace [stub('lambda')]
+
+ Alpha.should have_sphinx_indexes
+ end
+
+ it "should return false if there are no sphinx indexes or blocks" do
+ Alpha.sphinx_indexes.clear
+ Alpha.sphinx_index_blocks.clear
+
+ Alpha.should_not have_sphinx_indexes
+ end
+ end
end

0 comments on commit 1506e6b

Please sign in to comment.