Skip to content

Commit a974797

Browse files
AdrianCurtinCopilot
andcommitted
Update queries for in and not in with parse pointers
Co-Authored-By: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 32b5f35 commit a974797

3 files changed

Lines changed: 89 additions & 19 deletions

File tree

Gemfile.lock

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ GEM
5353
base64 (0.3.0)
5454
benchmark (0.4.1)
5555
bigdecimal (3.2.2)
56-
binding_of_caller (1.0.0)
57-
debug_inspector (>= 0.0.1)
56+
binding_of_caller (1.0.1)
57+
debug_inspector (>= 1.2.0)
5858
builder (3.3.0)
59-
byebug (11.1.3)
59+
byebug (12.0.0)
6060
case_transform (0.2)
6161
activesupport
6262
coderay (1.1.3)
6363
concurrent-ruby (1.3.5)
6464
connection_pool (2.5.3)
6565
crass (1.0.6)
66-
debug_inspector (1.1.0)
67-
dotenv (2.8.1)
66+
debug_inspector (1.2.0)
67+
dotenv (3.1.8)
6868
drb (2.2.3)
6969
erubi (1.13.1)
7070
faraday (2.13.4)
@@ -81,8 +81,7 @@ GEM
8181
loofah (2.24.1)
8282
crass (~> 1.0.2)
8383
nokogiri (>= 1.12.0)
84-
method_source (1.0.0)
85-
mini_portile2 (2.8.9)
84+
method_source (1.1.0)
8685
minitest (5.25.5)
8786
minitest-reporters (1.7.1)
8887
ansi
@@ -92,8 +91,21 @@ GEM
9291
moneta (1.6.0)
9392
net-http (0.6.0)
9493
uri
95-
nokogiri (1.18.9)
96-
mini_portile2 (~> 2.8.2)
94+
nokogiri (1.18.9-aarch64-linux-gnu)
95+
racc (~> 1.4)
96+
nokogiri (1.18.9-aarch64-linux-musl)
97+
racc (~> 1.4)
98+
nokogiri (1.18.9-arm-linux-gnu)
99+
racc (~> 1.4)
100+
nokogiri (1.18.9-arm-linux-musl)
101+
racc (~> 1.4)
102+
nokogiri (1.18.9-arm64-darwin)
103+
racc (~> 1.4)
104+
nokogiri (1.18.9-x86_64-darwin)
105+
racc (~> 1.4)
106+
nokogiri (1.18.9-x86_64-linux-gnu)
107+
racc (~> 1.4)
108+
nokogiri (1.18.9-x86_64-linux-musl)
97109
racc (~> 1.4)
98110
parallel (1.27.0)
99111
pry (0.14.2)
@@ -118,25 +130,26 @@ GEM
118130
rails-html-sanitizer (1.6.2)
119131
loofah (~> 2.21)
120132
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
121-
rake (13.0.6)
122-
redcarpet (3.5.1)
123-
redis (5.0.6)
124-
redis-client (>= 0.9.0)
125-
redis-client (0.12.1)
133+
rake (13.3.0)
134+
redcarpet (3.6.1)
135+
redis (5.4.1)
136+
redis-client (>= 0.22.0)
137+
redis-client (0.25.2)
126138
connection_pool
127139
ruby-progressbar (1.13.0)
128-
rufo (0.13.0)
140+
rufo (0.18.1)
129141
securerandom (0.4.1)
130142
tzinfo (2.0.6)
131143
concurrent-ruby (~> 1.0)
132144
uri (1.0.3)
133145
useragent (0.16.11)
134-
webrick (1.7.0)
135-
yard (0.9.28)
136-
webrick (~> 1.7.0)
146+
yard (0.9.37)
137147

138148
PLATFORMS
139149
ruby
150+
x86_64-darwin
151+
x86_64-linux-gnu
152+
x86_64-linux-musl
140153

141154
DEPENDENCIES
142155
byebug
@@ -154,4 +167,4 @@ DEPENDENCIES
154167
yard (>= 0.9.11)
155168

156169
BUNDLED WITH
157-
2.3.19
170+
2.5.23

lib/parse/query.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,42 @@ def convert_constraints_for_aggregation(constraints)
19531953
converted_value[op] = "#{op_value['className']}$#{op_value['objectId']}"
19541954
elsif op_value.is_a?(Parse::Pointer)
19551955
converted_value[op] = "#{op_value.parse_class}$#{op_value.id}"
1956+
elsif op_value.is_a?(Array) && (op.to_s == "$in" || op.to_s == "$nin")
1957+
# Handle arrays of pointers for $in and $nin operators
1958+
# If the aggregation_field starts with _p_, it's a pointer field
1959+
is_pointer_field = aggregation_field.start_with?('_p_')
1960+
1961+
converted_value[op] = op_value.map do |item|
1962+
if item.is_a?(Hash) && item["__type"] == "Pointer"
1963+
"#{item['className']}$#{item['objectId']}"
1964+
elsif item.is_a?(Parse::Pointer)
1965+
"#{item.parse_class}$#{item.id}"
1966+
elsif is_pointer_field && item.is_a?(String)
1967+
# For pointer fields with string IDs, we need to infer the class name
1968+
# Try to get it from the Parse::Pointer if one exists in the array
1969+
class_name = nil
1970+
op_value.each do |v|
1971+
if v.is_a?(Parse::Pointer)
1972+
class_name = v.parse_class
1973+
break
1974+
elsif v.is_a?(Hash) && v["__type"] == "Pointer"
1975+
class_name = v["className"]
1976+
break
1977+
end
1978+
end
1979+
1980+
if class_name
1981+
"#{class_name}$#{item}"
1982+
else
1983+
# Try to infer from field name (e.g., _p_team -> Team)
1984+
field_name = aggregation_field.sub(/^_p_/, '')
1985+
inferred_class = field_name.capitalize
1986+
"#{inferred_class}$#{item}"
1987+
end
1988+
else
1989+
item
1990+
end
1991+
end
19561992
else
19571993
converted_value[op] = op_value
19581994
end

test/lib/parse/distinct_conversion_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ def test_distinct_mixed_pointer_and_regular_strings
105105
end
106106
end
107107

108+
def test_distinct_with_return_pointers_converts_to_pointers
109+
# Test with return_pointers: true option
110+
values = ["Team$abc123", "Team$def456", "Team$ghi789"]
111+
112+
@query.stub :compile_where, {} do
113+
@query.stub :aggregate, mock_aggregation(values) do
114+
result = @query.distinct(:project, return_pointers: true)
115+
116+
# Should return Parse::Pointer objects when explicitly requested
117+
assert_equal 3, result.size
118+
assert_kind_of Parse::Pointer, result.first
119+
assert_equal "Team", result[0].parse_class
120+
assert_equal "abc123", result[0].id
121+
assert_equal "Team", result[1].parse_class
122+
assert_equal "def456", result[1].id
123+
assert_equal "Team", result[2].parse_class
124+
assert_equal "ghi789", result[2].id
125+
end
126+
end
127+
end
128+
108129
def test_pointer_string_regex_pattern
109130
# Test the regex pattern used for detecting MongoDB pointer strings
110131
valid_patterns = ["Team$abc123", "User$def456", "MyClass$12345abc", "A$1"]

0 commit comments

Comments
 (0)