Permalink
Browse files

only create/update record in index if it matches xapit find conditions

  • Loading branch information...
1 parent fe6af8c commit f3ba992836e80f5ce987753fb3f78dae854088df @ryanb committed Jun 23, 2009
View
@@ -1,3 +1,5 @@
+* only create/update record in index if it matches xapit find conditions
+
*0.2.3* (June 18th, 2009)
* adding compatability with Xapit Sync
@@ -5,8 +5,8 @@ def self.for_class?(member_class)
member_class.ancestors.map(&:to_s).include? "ActiveRecord::Base"
end
- def find_single(id)
- @target.find(id)
+ def find_single(id, *args)
+ @target.find_by_id(id, *args)
end
def find_multiple(ids)
@@ -95,12 +95,18 @@ def position_of_field(field_attribute)
# Add a single record to the index
def create_record(member_id)
- @indexer.add_member(@member_class.xapit_adapter.find_single(member_id))
+ member = @member_class.xapit_adapter.find_single(member_id, *@args)
+ @indexer.add_member(member) if member
end
# Update a single record in the index
def update_record(member_id)
- @indexer.update_member(@member_class.xapit_adapter.find_single(member_id))
+ member = @member_class.xapit_adapter.find_single(member_id, *@args)
+ if member
+ @indexer.update_member(member)
+ else
+ destroy_record(member_id)
+ end
end
# Remove a single record from the index
@@ -10,9 +10,9 @@
it "should pass find_single to find method to target" do
target = Object.new
- mock(target).find(1) { :record }
+ mock(target).find_by_id(1, :conditions => "foo") { :record }
adapter = Xapit::ActiveRecordAdapter.new(target)
- adapter.find_single(1).should == :record
+ adapter.find_single(1, :conditions => "foo").should == :record
end
it "should pass find_multiple to find method to target" do
@@ -81,20 +81,32 @@
XapitMember.search("Bad Record").should == []
end
- it "should remove a record from the index" do
- member = XapitMember.new(:name => "Bad Record!")
+ it "should update a record in the index" do
+ member = XapitMember.new(:name => "New Record!")
@index.text :name
@index.index_all
- @index.destroy_record(member.id)
- XapitMember.search("Bad Record").should == []
+ member.update_attribute(:name, "Changed Record!")
+ @index.update_record(member.id)
+ XapitMember.search("Changed Record").should == [member]
end
- it "should update a record in the index" do
+ it "should not create record index if member isn't found" do
+ Xapit::Config.writable_database # make sure the database is built
+ member = XapitMember.new(:name => "New Record!")
+ stub(XapitMember).find { nil }
+ @index.text :name
+ @index.create_record(member.id)
+ XapitMember.search("New Record").should be_empty
+ end
+
+ it "should remove record from index when updating a member which doesn't exist" do
member = XapitMember.new(:name => "New Record!")
@index.text :name
@index.index_all
+ stub(XapitMember).find { nil }
member.update_attribute(:name, "Changed Record!")
@index.update_record(member.id)
- XapitMember.search("Changed Record").should == [member]
+ XapitMember.search("New Record").should be_empty
+ XapitMember.search("Changed Record").should be_empty
end
end
View
@@ -26,6 +26,10 @@ def self.find(ids)
end
end
+ def self.find_by_id(id)
+ find(id)
+ end
+
def initialize(attributes = {})
@@records ||= []
@id = @@records.size + 1

0 comments on commit f3ba992

Please sign in to comment.