Skip to content

Commit

Permalink
Tests for HasMany#find
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Jan 20, 2009
1 parent 92c10bd commit a9dc863
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/conductor/associations/has_many.rb
Expand Up @@ -71,17 +71,6 @@ def deleted_records
original_records.reject { |record| records.include?(record) }
end

# Called from Conductor::Base#save! This is necessary because when the base_record is a new record
# the foreign key won't be automatically assigned in update_item.
def set_foreign_keys
records.each do |record|
if record.send(primary_key_name).nil?
record.send("#{primary_key_name}=", base_record.id)
end
end
end

# TODO: Individual test
def find(id)
records.find { |record| record.id == id }
end
Expand All @@ -96,6 +85,16 @@ def has_key_requirement?
!required_key.nil?
end

# Called from Conductor::Base#save! This is necessary because when the base_record is a new record
# the foreign key won't be automatically assigned in update_item.
def set_foreign_keys
records.each do |record|
if record.send(primary_key_name).nil?
record.send("#{primary_key_name}=", base_record.id)
end
end
end

private

def build_record(parameters)
Expand Down
15 changes: 15 additions & 0 deletions spec/associations/has_many_spec.rb
Expand Up @@ -129,4 +129,19 @@ module Conductor::Associations
end
end
end

describe HasMany, "with some records" do
before do
@association = Conductor::Associations::HasMany.new(stub_everything, :foo)
@association.stubs(:records).returns([stub(:id => 4), stub(:id => 9), stub(:id => 1)])
end

it "should find a record when given an id that exists" do
@association.find(1).should == @association.records[2]
end

it "should not find a record when given an id that does not exist" do
@association.find(3).should == nil
end
end
end

0 comments on commit a9dc863

Please sign in to comment.