Skip to content

Commit 8af7870

Browse files
committed
Improve taggings API by introducing a null object
Instead of adding chains of `&.` we should use a null object to allow the taggings definition to be more concise.
1 parent 18cd634 commit 8af7870

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

actionpack/lib/action_controller/railtie.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ class Railtie < Rails::Railtie # :nodoc:
101101

102102
ActiveSupport.on_load(:active_record) do
103103
ActiveRecord::QueryLogs.taggings.merge!(
104-
controller: -> { context[:controller]&.controller_name },
105-
action: -> { context[:controller]&.action_name },
106-
namespaced_controller: -> { context[:controller]&.class&.name }
104+
controller: -> { context[:controller].controller_name },
105+
action: -> { context[:controller].action_name },
106+
namespaced_controller: -> { context[:controller].class.name }
107107
)
108108
end
109109
end

activejob/lib/active_job/railtie.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class Railtie < Rails::Railtie # :nodoc:
5858
if app.config.active_record.query_log_tags_enabled && app.config.active_job.log_query_tags_around_perform != false
5959
app.config.active_record.query_log_tags << :job
6060

61-
ActiveSupport.on_load(:active_job) do
62-
include ActiveJob::QueryTags
63-
end
61+
ActiveSupport.on_load(:active_job) do
62+
include ActiveJob::QueryTags
63+
end
6464

65-
ActiveSupport.on_load(:active_record) do
66-
ActiveRecord::QueryLogs.taggings[:job] = -> { context[:job]&.class&.name }
65+
ActiveSupport.on_load(:active_record) do
66+
ActiveRecord::QueryLogs.taggings[:job] = -> { context[:job].class.name }
6767
end
6868
end
6969
end

activerecord/lib/active_record/query_logs.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ module QueryLogs
7272
mattr_accessor :cache_query_log_tags, instance_accessor: false, default: false
7373
thread_mattr_accessor :cached_comment, instance_accessor: false
7474

75+
class NullObject # :nodoc:
76+
def method_missing(method, *args, &block)
77+
NullObject.new
78+
end
79+
80+
def nil?
81+
true
82+
end
83+
84+
private
85+
def respond_to_missing?(method, include_private = false)
86+
true
87+
end
88+
end
89+
7590
class << self
7691
# Updates the context used to construct tags in the SQL comment.
7792
# Resets the cached comment if <tt>cache_query_log_tags</tt> is +true+.
@@ -87,7 +102,7 @@ def set_context(**options)
87102
update_context(**options)
88103
yield if block_given?
89104
ensure
90-
update_context(**options.transform_values! { nil })
105+
update_context(**options.transform_values! { NullObject.new })
91106
end
92107

93108
# Temporarily tag any query executed within `&block`. Can be nested.
@@ -139,11 +154,15 @@ def inline_comment
139154

140155
# Return the set of active inline tags from +with_tag+.
141156
def inline_tags
142-
context[:inline_tags] ||= []
157+
if context[:inline_tags].nil?
158+
context[:inline_tags] = []
159+
else
160+
context[:inline_tags]
161+
end
143162
end
144163

145164
def context
146-
Thread.current[:active_record_query_log_tags_context] ||= {}
165+
Thread.current[:active_record_query_log_tags_context] ||= Hash.new { NullObject.new }
147166
end
148167

149168
def escape_sql_comment(content)
@@ -153,11 +172,13 @@ def escape_sql_comment(content)
153172
def tag_content
154173
tags.flat_map { |i| [*i] }.filter_map do |tag|
155174
key, value_input = tag
175+
156176
val = case value_input
157177
when nil then tag_value(key) if taggings.has_key? key
158178
when Proc then instance_exec(&value_input)
159179
else value_input
160180
end
181+
161182
"#{key}:#{val}" unless val.nil?
162183
end.join(",")
163184
end

0 commit comments

Comments
 (0)