Skip to content

Commit fc9053a

Browse files
committed
Add tracking of join options for jsonapi-authorization gem
1 parent bf4b4cd commit fc9053a

File tree

2 files changed

+69
-49
lines changed

2 files changed

+69
-49
lines changed

lib/jsonapi/active_relation/join_manager.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,15 @@ def perform_joins(records, options)
147147
related_resource_klass = join_details[:related_resource_klass]
148148
join_type = relationship_details[:join_type]
149149

150+
join_options = {
151+
relationship: relationship,
152+
relationship_details: relationship_details,
153+
related_resource_klass: related_resource_klass,
154+
}
155+
150156
if relationship == :root
151157
unless source_relationship
152-
add_join_details('', {alias: resource_klass._table_name, join_type: :root})
158+
add_join_details('', {alias: resource_klass._table_name, join_type: :root, join_options: join_options})
153159
end
154160
next
155161
end
@@ -163,7 +169,7 @@ def perform_joins(records, options)
163169
options: options)
164170
}
165171

166-
details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type}
172+
details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type, join_options: join_options}
167173

168174
if relationship == source_relationship
169175
if relationship.polymorphic? && relationship.belongs_to?

test/unit/active_relation_resource_finder/join_manager_test.rb

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_no_added_joins
2323
records = join_manager.join(records, {})
2424
assert_equal 'SELECT "posts".* FROM "posts"', records.to_sql
2525

26-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
26+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
2727
end
2828

2929
def test_add_single_join
@@ -32,8 +32,22 @@ def test_add_single_join
3232
records = PostResource.records({})
3333
records = join_manager.join(records, {})
3434
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', records.to_sql
35-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
36-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)))
35+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
36+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)).except!(:join_options))
37+
end
38+
39+
def test_joins_have_join_options
40+
filters = {'tags' => ['1']}
41+
join_manager = JSONAPI::ActiveRelation::JoinManager.new(resource_klass: PostResource, filters: filters)
42+
records = PostResource.records({})
43+
records = join_manager.join(records, {})
44+
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', records.to_sql
45+
46+
source_join_options = join_manager.source_join_details[:join_options]
47+
assert_array_equals [:relationship, :relationship_details, :related_resource_klass], source_join_options.keys
48+
49+
relationship_join_options = join_manager.join_details_by_relationship(PostResource._relationship(:tags))[:join_options]
50+
assert_array_equals [:relationship, :relationship_details, :related_resource_klass], relationship_join_options.keys
3751
end
3852

3953
def test_add_single_sort_join
@@ -43,8 +57,8 @@ def test_add_single_sort_join
4357
records = join_manager.join(records, {})
4458

4559
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', records.to_sql
46-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
47-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)))
60+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
61+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)).except!(:join_options))
4862
end
4963

5064
def test_add_single_sort_and_filter_join
@@ -54,8 +68,8 @@ def test_add_single_sort_and_filter_join
5468
records = PostResource.records({})
5569
records = join_manager.join(records, {})
5670
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id"', records.to_sql
57-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
58-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)))
71+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
72+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)).except!(:join_options))
5973
end
6074

6175
def test_add_sibling_joins
@@ -69,9 +83,9 @@ def test_add_sibling_joins
6983
records = join_manager.join(records, {})
7084

7185
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "posts_tags" ON "posts_tags"."post_id" = "posts"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "posts_tags"."tag_id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id"', records.to_sql
72-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
73-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)))
74-
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:author)))
86+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
87+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:tags)).except!(:join_options))
88+
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(PostResource._relationship(:author)).except!(:join_options))
7589
end
7690

7791

@@ -82,7 +96,7 @@ def test_add_joins_source_relationship
8296
records = join_manager.join(records, {})
8397

8498
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"', records.to_sql
85-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
99+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details.except!(:join_options))
86100
end
87101

88102

@@ -96,7 +110,7 @@ def test_add_joins_source_relationship_with_custom_apply
96110

97111
assert_equal sql, records.to_sql
98112

99-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
113+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details.except!(:join_options))
100114
end
101115

102116
def test_add_nested_scoped_joins
@@ -110,11 +124,11 @@ def test_add_nested_scoped_joins
110124
records = Api::V10::PostResource.records({})
111125
records = join_manager.join(records, {})
112126

113-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
114-
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
115-
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)))
116-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)))
117-
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)))
127+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
128+
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)).except!(:join_options))
129+
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)).except!(:join_options))
130+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)).except!(:join_options))
131+
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)).except!(:join_options))
118132

119133
# Now test with different order for the filters
120134
filters = {
@@ -127,11 +141,11 @@ def test_add_nested_scoped_joins
127141
records = Api::V10::PostResource.records({})
128142
records = join_manager.join(records, {})
129143

130-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
131-
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
132-
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)))
133-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)))
134-
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)))
144+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
145+
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)).except!(:join_options))
146+
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)).except!(:join_options))
147+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)).except!(:join_options))
148+
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)).except!(:join_options))
135149
end
136150

137151
def test_add_nested_joins_with_fields
@@ -145,11 +159,11 @@ def test_add_nested_joins_with_fields
145159
records = Api::V10::PostResource.records({})
146160
records = join_manager.join(records, {})
147161

148-
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
149-
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
150-
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)))
151-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)))
152-
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)))
162+
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details.except!(:join_options))
163+
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)).except!(:join_options))
164+
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)).except!(:join_options))
165+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)).except!(:join_options))
166+
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:author)).except!(:join_options))
153167
end
154168

155169
def test_add_joins_with_sub_relationship
@@ -160,10 +174,10 @@ def test_add_joins_with_sub_relationship
160174
records = Api::V10::PostResource.records({})
161175
records = join_manager.join(records, {})
162176

163-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
164-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
165-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)))
166-
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PersonResource._relationship(:comments)))
177+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details.except!(:join_options))
178+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)).except!(:join_options))
179+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:tags)).except!(:join_options))
180+
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PersonResource._relationship(:comments)).except!(:join_options))
167181
end
168182

169183
def test_add_joins_with_sub_relationship_and_filters
@@ -181,11 +195,11 @@ def test_add_joins_with_sub_relationship_and_filters
181195
records = PostResource.records({})
182196
records = join_manager.join(records, {})
183197

184-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
185-
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(PostResource._relationship(:comments)))
186-
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:author)))
187-
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:tags)))
188-
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(PersonResource._relationship(:comments)))
198+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details.except!(:join_options))
199+
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(PostResource._relationship(:comments)).except!(:join_options))
200+
assert_hash_equals({alias: 'people', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:author)).except!(:join_options))
201+
assert_hash_equals({alias: 'tags', join_type: :left}, join_manager.join_details_by_relationship(CommentResource._relationship(:tags)).except!(:join_options))
202+
assert_hash_equals({alias: 'comments_people', join_type: :left}, join_manager.join_details_by_relationship(PersonResource._relationship(:comments)).except!(:join_options))
189203
end
190204

191205
def test_polymorphic_join_belongs_to_just_source
@@ -196,10 +210,10 @@ def test_polymorphic_join_belongs_to_just_source
196210
records = join_manager.join(records, {})
197211

198212
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', records.to_sql
199-
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.source_join_details('products'))
200-
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.source_join_details('documents'))
201-
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products'))
202-
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents'))
213+
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.source_join_details('products').except!(:join_options))
214+
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.source_join_details('documents').except!(:join_options))
215+
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products').except!(:join_options))
216+
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents').except!(:join_options))
203217
end
204218

205219
def test_polymorphic_join_belongs_to_filter
@@ -210,9 +224,9 @@ def test_polymorphic_join_belongs_to_filter
210224
records = join_manager.join(records, {})
211225

212226
# assert_equal 'SELECT "pictures".* FROM "pictures" LEFT OUTER JOIN "products" ON "products"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Product\' LEFT OUTER JOIN "documents" ON "documents"."id" = "pictures"."imageable_id" AND "pictures"."imageable_type" = \'Document\'', records.to_sql
213-
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details)
214-
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products'))
215-
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents'))
227+
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details.except!(:join_options))
228+
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products').except!(:join_options))
229+
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents').except!(:join_options))
216230
end
217231

218232
def test_polymorphic_join_belongs_to_filter_on_resource
@@ -228,9 +242,9 @@ def test_polymorphic_join_belongs_to_filter_on_resource
228242
records = PictureResource.records({})
229243
records = join_manager.join(records, {})
230244

231-
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details)
232-
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products'))
233-
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents'))
234-
assert_hash_equals({alias: 'file_properties', join_type: :left}, join_manager.join_details_by_relationship(PictureResource._relationship(:file_properties)))
245+
assert_hash_equals({alias: 'pictures', join_type: :root}, join_manager.source_join_details.except!(:join_options))
246+
assert_hash_equals({alias: 'products', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'products').except!(:join_options))
247+
assert_hash_equals({alias: 'documents', join_type: :left}, join_manager.join_details_by_polymorphic_relationship(PictureResource._relationship(:imageable), 'documents').except!(:join_options))
248+
assert_hash_equals({alias: 'file_properties', join_type: :left}, join_manager.join_details_by_relationship(PictureResource._relationship(:file_properties)).except!(:join_options))
235249
end
236250
end

0 commit comments

Comments
 (0)