Skip to content

Commit

Permalink
Making sure destroy/update hooks are added if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Nov 28, 2009
1 parent 3914cd5 commit e12e823
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/thinking_sphinx/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def define_index(name = nil, &block)

ThinkingSphinx.context.add_indexed_model self

if sphinx_index_blocks.empty?
before_validation :define_indexes
before_destroy :define_indexes
end

self.sphinx_index_blocks << lambda {
index = ThinkingSphinx::Index::Builder.generate self, name, &block
add_sphinx_callbacks_and_extend(index.delta?)
Expand Down Expand Up @@ -311,5 +316,9 @@ def sphinx_document_id
def sphinx_index_name(suffix)
"#{self.class.source_of_sphinx_index.name.underscore.tr(':/\\', '_')}_#{suffix}"
end

def define_indexes
self.class.define_indexes
end
end
end
30 changes: 28 additions & 2 deletions spec/thinking_sphinx/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,33 @@
end

context 'callbacks' do
it "should add a toggle_deleted callback" do
it "should add a before_validation callback to define_indexes" do
Alpha.should_receive(:before_validation).with(:define_indexes)

Alpha.define_index { }
end

it "should not add a before_validation callback twice" do
Alpha.should_receive(:before_validation).with(:define_indexes).once

Alpha.define_index { }
Alpha.define_index { }
end

it "should add a before_destroy callback to define_indexes" do
Alpha.should_receive(:before_destroy).with(:define_indexes)

Alpha.define_index { }
end

it "should not add a before_destroy callback twice" do
Alpha.should_receive(:before_destroy).with(:define_indexes).once

Alpha.define_index { }
Alpha.define_index { }
end

it "should add a toggle_deleted callback when defined" do
Alpha.should_receive(:after_destroy).with(:toggle_deleted)

Alpha.define_index { indexes :name }
Expand All @@ -84,7 +110,7 @@
Alpha.define_indexes
end

it "should add a update_attribute_values callback" do
it "should add a update_attribute_values callback when defined" do
Alpha.should_receive(:after_commit).with(:update_attribute_values)

Alpha.define_index { indexes :name }
Expand Down

0 comments on commit e12e823

Please sign in to comment.