Skip to content

Commit cd39ecf

Browse files
authored
Add join method overloads that accept the query condition as argument (#1090)
This deprecates `#where_*` join methods to which the query condition was delegated.
1 parent 0d3e8b6 commit cd39ecf

4 files changed

Lines changed: 27 additions & 23 deletions

File tree

spec/avram/associations_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ describe Avram::Model do
118118
comment = CommentForCustomPost::SaveOperation.create!(body: "bar", post_id: post.id)
119119
comment.post_with_custom_table.should eq(post)
120120

121-
CommentForCustomPost::BaseQuery.new.where_post_with_custom_table(PostWithCustomTable::BaseQuery.new.id(post.id)).first.should eq(comment)
122-
PostWithCustomTable::BaseQuery.new.where_comments_for_custom_post(CommentForCustomPost::BaseQuery.new.id(comment.id)).first.should eq(post)
121+
CommentForCustomPost::BaseQuery.new.join_post_with_custom_table(PostWithCustomTable::BaseQuery.new.id(post.id)).first.should eq(comment)
122+
PostWithCustomTable::BaseQuery.new.join_comments_for_custom_post(CommentForCustomPost::BaseQuery.new.id(comment.id)).first.should eq(post)
123123
end
124124
end
125125

spec/avram/query_associations_spec.cr

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ describe "Query associations" do
5050
.create
5151

5252
posts = Post::BaseQuery.new
53-
.where_comments(CommentQuery.new.body_eq("matching"))
53+
.join_comments(CommentQuery.new.body_eq("matching"))
5454
posts.results.should eq([post_with_matching_comment])
5555

5656
posts = Post::BaseQuery.new
57-
.where_comments(Comment::BaseQuery.new.body("matching"))
57+
.join_comments(Comment::BaseQuery.new.body("matching"))
5858
posts.results.should eq([post_with_matching_comment])
5959
end
6060

@@ -72,8 +72,7 @@ describe "Query associations" do
7272
.create
7373

7474
posts = Post::BaseQuery.new
75-
.inner_join_comments
76-
.where_comments(Comment::BaseQuery.new.body.eq("matching"), auto_inner_join: false)
75+
.inner_join_comments(Comment::BaseQuery.new.body.eq("matching"))
7776
posts.to_sql[0].should contain "INNER JOIN"
7877
posts.results.should eq([post_with_matching_comment])
7978
end
@@ -92,8 +91,7 @@ describe "Query associations" do
9291
.create
9392

9493
posts = Post::BaseQuery.new
95-
.left_join_comments
96-
.where_comments(Comment::BaseQuery.new.body.eq("matching"), auto_inner_join: false)
94+
.left_join_comments(Comment::BaseQuery.new.body.eq("matching"))
9795
posts.to_sql[0].should contain "LEFT JOIN"
9896
posts.results.should eq([post_with_matching_comment])
9997
end
@@ -112,8 +110,7 @@ describe "Query associations" do
112110
.create
113111

114112
posts = Post::BaseQuery.new
115-
.right_join_comments
116-
.where_comments(Comment::BaseQuery.new.body.eq("matching"), auto_inner_join: false)
113+
.right_join_comments(where_comments: Comment::BaseQuery.new.body.eq("matching"))
117114
posts.to_sql[0].should contain "RIGHT JOIN"
118115
posts.results.should eq([post_with_matching_comment])
119116
end
@@ -132,15 +129,14 @@ describe "Query associations" do
132129
.create
133130

134131
posts = Post::BaseQuery.new
135-
.full_join_comments
136-
.where_comments(Comment::BaseQuery.new.body.eq("matching"), auto_inner_join: false)
132+
.full_join_comments(Comment::BaseQuery.new.body.eq("matching"))
137133
posts.to_sql[0].should contain "FULL JOIN"
138134
posts.results.should eq([post_with_matching_comment])
139135
end
140136

141137
it "can query associations on namespaced models" do
142138
orgs = NamedSpaced::Organization::BaseQuery.new
143-
.where_locations(NamedSpaced::Location::BaseQuery.new.name("Home"))
139+
.join_locations(NamedSpaced::Location::BaseQuery.new.name("Home"))
144140
orgs.to_sql[0].should contain "INNER JOIN"
145141

146142
staff = NamedSpaced::Staff::BaseQuery.new
@@ -154,9 +150,9 @@ describe "Query associations" do
154150

155151
line_item_query = LineItemQuery.new
156152
.id(item.id)
157-
.where_associated_products(ProductQuery.new.id(product.id))
153+
.join_associated_products(ProductQuery.new.id(product.id))
158154
result = LineItemProductQuery.new
159-
.where_line_item(line_item_query)
155+
.join_line_item(line_item_query)
160156
.find(line_item_product.id)
161157

162158
result.should eq(line_item_product)
@@ -169,9 +165,9 @@ describe "Query associations" do
169165

170166
line_item_query = LineItemQuery.new
171167
.id(item.id)
172-
.where_line_items_products(LineItemProductQuery.new.id(line_item_product.id))
168+
.join_line_items_products(LineItemProductQuery.new.id(line_item_product.id))
173169
result = ProductQuery.new
174-
.where_line_items(line_item_query)
170+
.join_line_items(line_item_query)
175171
.find(product.id)
176172

177173
result.should eq(product)

spec/avram/queryable_spec.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ describe Avram::Queryable do
13641364
manager1 = ManagerFactory.create(&.name("Mr. Krabs"))
13651365
manager2 = ManagerFactory.create(&.name("Mr. Robot"))
13661366
EmployeeFactory.create(&.name("Spongebob Alderson").manager_id(manager1.id))
1367-
Employee::BaseQuery.new.where_manager(Manager::BaseQuery.new.id(manager1.id)).update(manager_id: manager2.id)
1367+
Employee::BaseQuery.new.join_manager(Manager::BaseQuery.new.id(manager1.id)).update(manager_id: manager2.id)
13681368
manager1.employees!.first?.should be_nil
13691369
manager2.employees!.first.name.should eq("Spongebob Alderson")
13701370
end
@@ -1439,7 +1439,7 @@ describe Avram::Queryable do
14391439

14401440
describe "#asc_order" do
14411441
it "orders by a joined table" do
1442-
query = Post::BaseQuery.new.where_comments(Comment::BaseQuery.new.created_at.asc_order)
1442+
query = Post::BaseQuery.new.join_comments(Comment::BaseQuery.new.created_at.asc_order)
14431443
query.to_sql[0].should contain %(ORDER BY "comments"."created_at" ASC)
14441444
end
14451445

@@ -1515,7 +1515,7 @@ describe Avram::Queryable do
15151515
post = PostFactory.create
15161516
CommentFactory.create &.post_id(post.id)
15171517

1518-
original_query = Post::BaseQuery.new.where_comments(Comment::BaseQuery.new.created_at.asc_order)
1518+
original_query = Post::BaseQuery.new.join_comments(Comment::BaseQuery.new.created_at.asc_order)
15191519
new_query = original_query.clone.select_count
15201520

15211521
original_query.first.should_not eq nil
@@ -1631,7 +1631,7 @@ describe Avram::Queryable do
16311631
line_item = LineItemFactory.create &.name("Thing 1")
16321632
price = PriceFactory.create &.in_cents(100).line_item_id(line_item.id)
16331633

1634-
query = PriceQuery.new.where_line_item(LineItemQuery.new.name("Thing 1"))
1634+
query = PriceQuery.new.inner_join_line_item(LineItemQuery.new.name("Thing 1"))
16351635
query.first.should eq price
16361636
end
16371637
end
@@ -1641,7 +1641,7 @@ describe Avram::Queryable do
16411641
line_item = LineItemFactory.create &.name("Thing 1")
16421642
PriceFactory.create &.in_cents(100).line_item_id(line_item.id)
16431643

1644-
query = LineItemQuery.new.where_price(PriceQuery.new.in_cents(100))
1644+
query = LineItemQuery.new.inner_join_price(PriceQuery.new.in_cents(100))
16451645
query.first.should eq line_item
16461646
end
16471647
end

src/avram/base_query_template.cr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class Avram::BaseQueryTemplate
6666
inner_join_{{ assoc[:assoc_name] }}
6767
end
6868

69+
def join_{{ assoc[:assoc_name] }}(where_{{ assoc[:assoc_name] }} : {{ assoc[:type] }}::BaseQuery)
70+
inner_join_{{ assoc[:assoc_name] }}(where_{{ assoc[:assoc_name] }})
71+
end
72+
6973
{% for join_type in ["Inner", "Left", "Right", "Full"] %}
7074
def {{ join_type.downcase.id }}_join_{{ assoc[:assoc_name] }}
7175
{% if assoc[:relationship_type] == :belongs_to %}
@@ -102,9 +106,13 @@ class Avram::BaseQueryTemplate
102106
)
103107
{% end %}
104108
end
105-
{% end %}
106109

110+
def {{ join_type.downcase.id }}_join_{{ assoc[:assoc_name] }}(where_{{ assoc[:assoc_name] }} : {{ assoc[:type] }}::BaseQuery)
111+
{{ join_type.downcase.id }}_join_{{ assoc[:assoc_name] }}.merge_query(where_{{ assoc[:assoc_name] }}.query)
112+
end
113+
{% end %}
107114

115+
@[Deprecated("Use any of the join methods with a where_{{ assoc[:assoc_name] }} argument instead")]
108116
def where_{{ assoc[:assoc_name] }}(assoc_query : {{ assoc[:type] }}::BaseQuery, auto_inner_join : Bool = true)
109117
if auto_inner_join
110118
join_{{ assoc[:assoc_name] }}.merge_query(assoc_query.query)

0 commit comments

Comments
 (0)