Skip to content

Commit

Permalink
+ Picky::Client::ActiveRecord#remove specs
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Jan 6, 2012
1 parent 69501ef commit 9b2b06d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
8 changes: 3 additions & 5 deletions client/lib/picky-client/client/active_record.rb
Expand Up @@ -61,20 +61,18 @@ def initialize *attributes
# updated the data do we want to index.
#
model.after_commit do |object|
data = { 'id' => object.id }

if object.destroyed?
client.remove index_name, data
else
data = { 'id' => object.id }

(attributes || object.attributes.keys).each do |attr|
data[attr] = object.respond_to?(attr) &&
object.send(attr) ||
object[attr]
end

# TODO Name #replace?
#
client.index index_name, data
client.replace index_name, data
end
end

Expand Down
27 changes: 23 additions & 4 deletions client/spec/picky-client/client/active_record_spec.rb
Expand Up @@ -75,11 +75,19 @@ def after_commit
end

it 'calls the right method in the client' do
client.should_receive(:index).once.with 'some_index_name', { 'id' => 1, 'name' => 'Niko', 'surname' => 'Dittmann' }
client.should_receive(:replace).once.with 'some_index_name', { 'id' => 1, 'name' => 'Niko', 'surname' => 'Dittmann' }

ar.save
end

it 'calls the right method in the client' do
client.should_receive(:remove).once.with 'some_index_name', { 'id' => 1 }

ar.destroy
end

end

context 'with client and index' do
let(:ar_module) { described_class.new :client => client, :index => 'some_other_index_name' }
let(:ar) { fake_ar.extend(ar_module).new(1, 'Niko', 'Dittmann') }
Expand All @@ -89,10 +97,16 @@ def after_commit
end

it 'calls the right method in the client' do
client.should_receive(:index).once.with 'some_other_index_name', { 'id' => 1, 'name' => 'Niko', 'surname' => 'Dittmann' }
client.should_receive(:replace).once.with 'some_other_index_name', { 'id' => 1, 'name' => 'Niko', 'surname' => 'Dittmann' }

ar.save
end

it 'calls the right method in the client' do
client.should_receive(:remove).once.with 'some_other_index_name', { 'id' => 1 }

ar.destroy
end
end
context 'with client and specific attributes' do
let(:ar_module) { described_class.new 'name', :client => client }
Expand All @@ -103,10 +117,16 @@ def after_commit
end

it 'calls the right method in the client' do
client.should_receive(:index).once.with 'some_index_name', { 'id' => 1, 'name' => 'Niko' }
client.should_receive(:replace).once.with 'some_index_name', { 'id' => 1, 'name' => 'Niko' }

ar.save
end

it 'calls the right method in the client' do
client.should_receive(:remove).once.with 'some_index_name', { 'id' => 1 }

ar.destroy
end
end
context 'standard client' do
it 'instantiates the client correctly' do
Expand All @@ -119,7 +139,6 @@ def after_commit

described_class.new :host => 'some_host', :port => :some_port, :path => '/bla'
end

end
end

Expand Down

0 comments on commit 9b2b06d

Please sign in to comment.