Skip to content

Commit

Permalink
Added gem building rake task. Completed specs for ThinkingSphinx::Act…
Browse files Browse the repository at this point in the history
…iveRecord::Delta. Added delta field to spec's Person model. Tweaked charset and morphology settings to allow index-specific values.
  • Loading branch information
pat committed Apr 21, 2008
1 parent 9f9e58b commit 70448dc
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
gem_deploy.rake
pkg
coverage
coverage
*.tmproj
34 changes: 33 additions & 1 deletion Rakefile
Expand Up @@ -7,9 +7,13 @@ end

require 'rake/rdoctask'
require 'spec/rake/spectask'
require 'rake/gempackagetask'

# allow require of spec/spec_helper
$:.unshift File.dirname(__FILE__) + '/../'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'

require 'thinking_sphinx'

desc 'Generate documentation'
Rake::RDocTask.new(:rdoc) do |rdoc|
Expand All @@ -31,4 +35,32 @@ Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems', '--exclude', 'riddle']
end

spec = Gem::Specification.new do |s|
s.name = "thinking_sphinx"
s.version = ThinkingSphinx::Version::String
s.summary = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
s.description = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
s.author = "Pat Allan"
s.email = "pat@freelancing-gods.com"
s.homepage = "http://ts.freelancing-gods.com"
s.has_rdoc = true
s.rdoc_options << "--title" << "Thinking Sphinx -- Rails/Merb Sphinx Plugin" <<
"--line-numbers"
s.rubyforge_project = "thinking-sphinx"
s.test_files = FileList["spec/**/*_spec.rb"]
s.files = FileList[
"lib/**/*.rb",
"LICENCE",
"README",
"tasks/**/*.rb",
"tasks/**/*.rake"
]
end

Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
p.need_tar = true
p.need_zip = true
end
10 changes: 7 additions & 3 deletions lib/thinking_sphinx/active_record/delta.rb
Expand Up @@ -66,10 +66,14 @@ def toggle_delta
# if running in the test environment.
#
def index_delta
unless ThinkingSphinx::Configuration.environment == "test" || !ThinkingSphinx.deltas_enabled?
configuration = ThinkingSphinx::Configuration.new
system "indexer --config #{configuration.config_file} --rotate #{self.class.name.downcase}_delta"
if ThinkingSphinx::Configuration.environment == "test" ||
!ThinkingSphinx.deltas_enabled?
return true
end

configuration = ThinkingSphinx::Configuration.new
system "indexer --config #{configuration.config_file} --rotate #{self.class.name.downcase}_delta"

true
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/thinking_sphinx/configuration.rb
Expand Up @@ -156,13 +156,13 @@ def build(file_path=nil)
index #{model.name.downcase}_core
{
#{source_list}
morphology = #{self.morphology}
morphology = #{index.options[:morphology] || self.morphology}
path = #{self.searchd_file_path}/#{model.name.downcase}_core
charset_type = #{self.charset_type}
charset_type = #{index.options[:charset_type] || self.charset_type}
INDEX
unless self.charset_table.nil?
unless index.options[:charset_type].nil? && self.charset_table.nil?
file.write <<-INDEX
charset_table = #{self.charset_table}
charset_table = #{index.options[:charset_type] || self.charset_table}
INDEX
end

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/structure.sql
Expand Up @@ -14,6 +14,7 @@ CREATE TABLE `people` (
`birthday` datetime NOT NULL,
`team_id` int(11) NULL,
`team_type` varchar(50) NULL,
`delta` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
184 changes: 184 additions & 0 deletions spec/unit/thinking_sphinx/active_record/delta_spec.rb
@@ -0,0 +1,184 @@
require 'spec/spec_helper'

describe "ThinkingSphinx::ActiveRecord::Delta" do
describe "after_commit callback" do
before :each do
Person.stub_method(:write_inheritable_array => true)
end

after :each do
Person.unstub_method(:write_inheritable_array)
end

it "should add callbacks" do
Person.after_commit :toggle_delta

Person.should have_received(:write_inheritable_array).with(
:after_commit, [:toggle_delta]
)
end
end

describe "save_with_after_commit_callback method" do
before :each do
@person = Person.new
@person.stub_methods(
:save_without_after_commit_callback => true,
:callback => true
)
end

it "should call the normal save method" do
@person.save

@person.should have_received(:save_without_after_commit_callback)
end

it "should call the callbacks if the save was successful" do
@person.save

@person.should have_received(:callback).with(:after_commit)
end

it "shouldn't call the callbacks if the save failed" do
@person.stub_method(:save_without_after_commit_callback => false)

@person.save

@person.should_not have_received(:callback)
end

it "should return the normal save's result" do
@person.save.should be_true

@person.stub_method(:save_without_after_commit_callback => false)

@person.save.should be_false
end
end

describe "save_with_after_commit_callback! method" do
before :each do
@person = Person.new
@person.stub_methods(
:save_without_after_commit_callback! => true,
:callback => true
)
end

it "should call the normal save! method" do
@person.save!

@person.should have_received(:save_without_after_commit_callback!)
end

it "should call the callbacks if the save! was successful" do
@person.save!

@person.should have_received(:callback).with(:after_commit)
end

it "shouldn't call the callbacks if the save! failed" do
@person.stub_method(:save_without_after_commit_callback! => false)

@person.save!

@person.should_not have_received(:callback)
end

it "should return the normal save's result" do
@person.save!.should be_true

@person.stub_method(:save_without_after_commit_callback! => false)

@person.save!.should be_false
end
end

describe "destroy_with_after_commit_callback method" do
before :each do
@person = Person.new
@person.stub_methods(
:destroy_without_after_commit_callback => true,
:callback => true
)
end

it "should call the normal destroy method" do
@person.destroy

@person.should have_received(:destroy_without_after_commit_callback)
end

it "should call the callbacks if the destroy was successful" do
@person.destroy

@person.should have_received(:callback).with(:after_commit)
end

it "shouldn't call the callbacks if the destroy failed" do
@person.stub_method(:destroy_without_after_commit_callback => false)

@person.destroy

@person.should_not have_received(:callback)
end

it "should return the normal save's result" do
@person.destroy.should be_true

@person.stub_method(:destroy_without_after_commit_callback => false)

@person.destroy.should be_false
end
end

describe "toggle_delta method" do
it "should set the delta value to true" do
@person = Person.new

@person.delta.should be_false
@person.send(:toggle_delta)
@person.delta.should be_true
end
end

describe "index_delta method" do
before :each do
ThinkingSphinx::Configuration.stub_method(:environment => "spec")
ThinkingSphinx.stub_method(:deltas_enabled? => true)

@person = Person.new
@person.stub_method(:system => true)
end

after :each do
ThinkingSphinx::Configuration.unstub_method(:environment)
ThinkingSphinx.unstub_method(:deltas_enabled?)
end

it "shouldn't index if delta indexing is disabled" do
ThinkingSphinx.stub_method(:deltas_enabled? => false)

@person.send(:index_delta)

@person.should_not have_received(:system)
end

it "shouldn't index if the environment is 'test'" do
ThinkingSphinx::Configuration.stub_method(:environment => "test")

@person.send(:index_delta)

@person.should_not have_received(:system)
end

it "should call indexer for the delta index" do
@person.send(:index_delta)

@person.should have_received(:system).with(
"indexer --config #{ThinkingSphinx::Configuration.new.config_file} --rotate person_delta"
)
end
end
end

0 comments on commit 70448dc

Please sign in to comment.