Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for association aliasing (#444)
- Loading branch information
Showing
7 changed files
with
163 additions
and
10 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
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
61 changes: 61 additions & 0 deletions
61
spec/integration/hanami/model/associations/relation_alias_spec.rb
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,61 @@ | ||
RSpec.describe 'Alias (:as) support for associations' do | ||
let(:users) { UserRepository.new } | ||
let(:posts) { PostRepository.new } | ||
let(:comments) { CommentRepository.new } | ||
|
||
it 'the attribute is named after the association' do | ||
user = users.create(name: 'Jules Verne') | ||
post = posts.create(title: 'World Traveling made easy', user_id: user.id) | ||
|
||
post_found = posts.find_with_author(post.id) | ||
expect(post_found.author).to eq(user) | ||
|
||
user_found = users.find_with_threads(user.id) | ||
expect(user_found.threads).to match_array([post]) | ||
end | ||
|
||
it 'it works with nested aggregates' do | ||
user = users.create(name: 'Jules Verne') | ||
post = posts.create(title: 'World Traveling made easy', user_id: user.id) | ||
commenter = users.create(name: 'Thomas Reid') | ||
comments.create(user_id: commenter.id, post_id: post.id) | ||
|
||
found = posts.feed_for(post.id) | ||
expect(found.author).to eq(user) | ||
expect(found.comments[0].user).to eq(commenter) | ||
end | ||
|
||
context '#assoc support (calling assoc by the alias)' do | ||
it 'for #belongs_to' do | ||
user = users.create(name: 'Jules Verne') | ||
post = posts.create(title: 'World Traveling made easy', user_id: user.id) | ||
commenter = users.create(name: 'Thomas Reid') | ||
comment = comments.create(user_id: commenter.id, post_id: post.id) | ||
|
||
found_author = posts.author_for(post) | ||
expect(found_author).to eq(user) | ||
|
||
found_commenter = comments.commenter_for(comment) | ||
expect(found_commenter).to eq(commenter) | ||
end | ||
|
||
it 'for #has_many' do | ||
user = users.create(name: 'Jules Verne') | ||
post = posts.create(title: 'World Traveling made easy', user_id: user.id) | ||
|
||
found_threads = users.threads_for(user) | ||
expect(found_threads).to match_array [post] | ||
end | ||
|
||
it 'for #has_many :through' do | ||
user = users.create(name: 'Jules Verne') | ||
post = posts.create(title: 'World Traveling made easy', user_id: user.id) | ||
commenter = users.create(name: 'Thomas Reid') | ||
comments.create(user_id: commenter.id, post_id: post.id) | ||
|
||
commenters = posts.commenters_for(post) | ||
|
||
expect(commenters).to match_array([commenter]) | ||
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
17 changes: 17 additions & 0 deletions
17
spec/support/fixtures/database_migrations/20171002201227_create_posts_and_comments.rb
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,17 @@ | ||
Hanami::Model.migration do | ||
change do | ||
drop_table? :posts | ||
create_table? :posts do | ||
primary_key :id | ||
column :title, String | ||
foreign_key :user_id, :users, on_delete: :cascade, null: false | ||
end | ||
drop_table? :comments | ||
create_table? :comments do | ||
primary_key :id | ||
|
||
foreign_key :user_id, :users, on_delete: :cascade, null: false | ||
foreign_key :post_id, :posts, on_delete: :cascade, null: false | ||
end | ||
end | ||
end |