-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
97 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "./spec_helper" | ||
|
||
include LazyLoadHelpers | ||
|
||
describe "Lazy loading associations" do | ||
it "can lazy load has_many and has_many through" do | ||
post = PostBox.save | ||
comment = CommentBox.new.post_id(post.id).save | ||
tag = TagBox.save | ||
tagging = TaggingBox.new.post_id(post.id).tag_id(tag.id).save | ||
|
||
post.comments!.should eq([comment]) | ||
post.tags!.should eq([tag]) | ||
end | ||
|
||
it "can lazy load has_one" do | ||
admin = AdminBox.save | ||
sign_in_credential = SignInCredentialBox.new.user_id(admin.id).save | ||
admin.sign_in_credential!.should eq(sign_in_credential) | ||
end | ||
|
||
it "can lazy load optional has_one" do | ||
user = UserBox.save | ||
user.sign_in_credential!.should be_nil | ||
end | ||
|
||
it "can lazy load belongs_to" do | ||
post = PostBox.save | ||
comment = CommentBox.new.post_id(post.id).save | ||
comment.post!.should eq(post) | ||
end | ||
|
||
it "can lazy load optional belongs_to" do | ||
employee = EmployeeBox.save | ||
employee.manager!.should be_nil | ||
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module LazyLoadHelpers | ||
private def with_lazy_load(enabled) | ||
begin | ||
LuckyRecord::Repo.configure do | ||
settings.lazy_load_enabled = enabled | ||
end | ||
|
||
yield | ||
ensure | ||
LuckyRecord::Repo.configure do | ||
settings.lazy_load_enabled = true | ||
end | ||
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