Skip to content

Commit 46e43f5

Browse files
authored
Merge pull request rails#43078 from Shopify/ar-query-log-default-context
Make ActiveRecord::QueryLogs default behavior to return the context content
2 parents e12514a + 9f035e2 commit 46e43f5

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def tag_content
176176
key, value_input = tag
177177

178178
val = case value_input
179-
when nil then tag_value(key) if taggings.has_key? key
179+
when nil then tag_value(key)
180180
when Proc then instance_exec(&value_input)
181181
else value_input
182182
end
@@ -186,12 +186,14 @@ def tag_content
186186
end
187187

188188
def tag_value(key)
189-
value = taggings[key]
190-
191-
if value.respond_to?(:call)
192-
instance_exec(&taggings[key])
189+
if value = taggings[key]
190+
if value.respond_to?(:call)
191+
instance_exec(&taggings[key])
192+
else
193+
value
194+
end
193195
else
194-
value
196+
context[key]
195197
end
196198
end
197199

activerecord/test/cases/query_logs_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ def test_ensure_context_has_symbol_keys
137137
ActiveRecord::QueryLogs.update_context(application_name: nil)
138138
end
139139

140+
def test_default_tag_behavior
141+
ActiveRecord::QueryLogs.tags = [:application, :foo]
142+
ActiveRecord::QueryLogs.set_context(foo: "bar") do
143+
assert_sql(%r{/\*application:active_record,foo:bar\*/}) do
144+
Dashboard.first
145+
end
146+
end
147+
assert_sql(%r{/\*application:active_record\*/}) do
148+
Dashboard.first
149+
end
150+
end
151+
140152
def test_inline_tags_only_affect_block
141153
# disable regular comment tags
142154
ActiveRecord::QueryLogs.tags = []

0 commit comments

Comments
 (0)