Skip to content

Commit

Permalink
adds spec for non-standard keys with AR #conditions_to_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Oct 12, 2010
1 parent f687910 commit a02d914
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/orm_adapter/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def conditions_to_fields(conditions)
fields = {}
conditions.each do |key, value|
if value.is_a?(ActiveRecord::Base) && (assoc = klass.reflect_on_association(key.to_sym)) && assoc.belongs_to?
fields[assoc.association_foreign_key] = value.send(value.class.primary_key)
fields[assoc.options[:foreign_type]] = value.class.base_class.name if assoc.options[:polymorphic]
fields[assoc.primary_key_name] = value.send(value.class.primary_key)
fields[assoc.options[:foreign_type]] = value.class.base_class.name.to_s if assoc.options[:polymorphic]
else
fields[key] = value
end
Expand Down
22 changes: 21 additions & 1 deletion spec/orm_adapter/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,34 @@ class Note < AbstractNoteClass
end

specify "#model_classes should return all of the non abstract model classes (that are not in except_classes)" do
subject.model_classes.should == [User, Note]
subject.model_classes.should include(User, Note)
end
end

it_should_behave_like "example app with orm_adapter" do
let(:user_class) { User }
let(:note_class) { Note }
end

describe "#conditions_to_fields" do
describe "with non-standard association keys" do
class PerverseNote < Note
belongs_to :user, :foreign_key => 'owner_id'
belongs_to :pwner, :polymorphic => true, :foreign_key => 'owner_id', :foreign_type => 'owner_type'
end

let(:user) { User.create! }
let(:adapter) { PerverseNote.to_adapter }

it "should convert polymorphic object in conditions to the appropriate fields" do
adapter.send(:conditions_to_fields, :pwner => user).should == {'owner_id' => user.id, 'owner_type' => user.class.name}
end

it "should convert belongs_to object in conditions to the appropriate fields" do
adapter.send(:conditions_to_fields, :user => user).should == {'owner_id' => user.id}
end
end
end
end
end
end

0 comments on commit a02d914

Please sign in to comment.