Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added primary_key support for has_one same as has_many #218

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/associations/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ def has_one(association_id, options = {})
define_method(association_id) do
options = {
:class_name => association_id.to_s.classify,
:foreign_key => self.class.to_s.foreign_key
:foreign_key => self.class.to_s.foreign_key,
:primary_key => self.class.primary_key
}.merge(options)

scope = options[:class_name].constantize

if scope.respond_to?(:scoped) && options[:conditions]
scope = scope.scoped(:conditions => options[:conditions])
end
scope.send("find_by_#{options[:foreign_key]}", id)
scope.send("find_by_#{options[:foreign_key]}", send(options[:primary_key]))
end
end

Expand Down
67 changes: 57 additions & 10 deletions spec/associations/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,67 @@ class SchoolStatus < ActiveHash::Base

describe "#has_one" do
context "with ActiveHash children" do
before do
City.has_one :author
Author.field :city_id
context "with default options" do
before do
Author.field :city_id
City.has_one :author
end

it "find the correct records" do
author = Author.create :city_id => 1
city = City.create :id => 1
expect(city.author).to eq(author)
end

it "returns nil when there are no records" do
Author.create :city_id => 10
city = City.create :id => 1
expect(city.author).to be_nil
end
end

it "find the correct records" do
city = City.create :id => 1
author = Author.create :city_id => 1
expect(city.author).to eq(author)
context "with a primary_key option" do
before do
Author.field :city_id
City.field :author_identifier
City.has_one :author, :primary_key => :author_identifier
end

it "find the correct records" do
Author.create :city_id => 1
author = Author.create :city_id => 10
city = City.create :id => 1, :author_identifier => 10
expect(city.author).to eq(author)
end

it "returns nil when there are no records" do
Author.create :city_id => 1
city = City.create :id => 1, :author_identifier => 10
expect(city.author).to be_nil
end
end

it "returns nil when there are no records" do
city = City.create :id => 1
expect(city.author).to be_nil
context "with a foreign_key option" do
before do
Author.field :city_id
Author.field :city_identifier
City.has_one :author, :foreign_key => :city_identifier
end

it "find the correct records" do
Author.create :city_id => 1, :city_identifier => 1
author = Author.create :city_id => 1, :city_identifier => 10
Author.create :city_id => 10, :city_identifier => 5
city = City.create :id => 10
expect(city.author).to eq(author)
end

it "returns nil when there are no records" do
Author.create :city_id => 1, :city_identifier => 1
Author.create :city_id => 10, :city_identifier => 5
city = City.create :id => 10
expect(city.author).to be_nil
end
end
end
end
Expand Down