-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle aliased assocs and struct builder properly
- Loading branch information
Showing
4 changed files
with
92 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,39 +84,90 @@ | |
end | ||
|
||
context "many-to-one" do | ||
before do | ||
factories.define(:task) do |f| | ||
f.title { "Foo" } | ||
f.association(:user) | ||
context "with an association that is not aliased" do | ||
before do | ||
factories.define(:task) do |f| | ||
f.title { "Foo" } | ||
f.association(:user) | ||
end | ||
|
||
factories.define(:user) do |f| | ||
f.first_name "Jane" | ||
f.last_name "Doe" | ||
f.email "[email protected]" | ||
f.timestamps | ||
|
||
f.association(:tasks) | ||
end | ||
end | ||
|
||
factories.define(:user) do |f| | ||
f.first_name "Jane" | ||
f.last_name "Doe" | ||
f.email "[email protected]" | ||
f.timestamps | ||
it "creates a struct with associated parent" do | ||
task = factories.structs[:task, title: "Bar"] | ||
|
||
f.association(:tasks) | ||
expect(task.title).to eql("Bar") | ||
expect(task.user.first_name).to eql("Jane") | ||
end | ||
end | ||
|
||
it "creates a struct with associated parent" do | ||
task = factories.structs[:task, title: "Bar"] | ||
it "does not build associated struct if it's set to nil explicitly" do | ||
task = factories.structs[:task, user: nil] | ||
|
||
expect(task.title).to eql("Bar") | ||
expect(task.user.first_name).to eql("Jane") | ||
end | ||
expect(task.user).to be_nil | ||
end | ||
|
||
it "does not persist associated struct if it's set to nil explicitly" do | ||
task = factories[:task, user: nil] | ||
|
||
expect(task.user).to be_nil | ||
end | ||
|
||
it "does not build associated struct if it's set to nil explicitly" do | ||
task = factories[:task, user: nil] | ||
it "creates the associated record with provided attributes" do | ||
task = factories[:task, user: {first_name: "John"}] | ||
|
||
expect(task.user).to be_nil | ||
expect(task.user.first_name).to eql("John") | ||
end | ||
end | ||
|
||
it "creates the associated record with provided attributes" do | ||
task = factories[:task, user: {first_name: "John"}] | ||
context "with an aliased association" do | ||
before do | ||
factories.define(:task) do |f| | ||
f.title { "Foo" } | ||
f.association(:author) | ||
end | ||
|
||
factories.define(:user) do |f| | ||
f.first_name "Jane" | ||
f.last_name "Doe" | ||
f.email "[email protected]" | ||
f.timestamps | ||
|
||
f.association(:tasks) | ||
end | ||
end | ||
|
||
it "creates a struct with associated parent" do | ||
task = factories.structs[:task, title: "Bar"] | ||
|
||
expect(task.title).to eql("Bar") | ||
expect(task.author.first_name).to eql("Jane") | ||
end | ||
|
||
it "does not build associated struct if it's set to nil explicitly" do | ||
task = factories.structs[:task, author: nil] | ||
|
||
expect(task.author).to be_nil | ||
end | ||
|
||
it "does not persist associated struct if it's set to nil explicitly" do | ||
task = factories[:task, author: nil] | ||
|
||
expect(task.user.first_name).to eql("John") | ||
expect(task.author).to be_nil | ||
end | ||
|
||
it "creates the associated record with provided attributes" do | ||
task = factories[:task, author: {first_name: "John"}] | ||
|
||
expect(task.author.first_name).to eql("John") | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
schema(infer: true) do | ||
associations do | ||
belongs_to :user | ||
belongs_to :user, as: :author | ||
end | ||
end | ||
end | ||
|