diff --git a/README.md b/README.md index 75090f8..d688670 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,12 @@ will issue this query: Nesting `with_annotation` blocks will concatenate the comment strings. +#### Customizing key-value formatting + +To change the default key-value separator (from `,`), set `Marginalia::Comment.key_value_separator`. +To surround all values with single quotes (`'`) and escape internal quotes as `\'`, +set `Marginalia::Comment.quote_values = :single`. + ## Contributing Start by bundling and creating the test database: diff --git a/lib/marginalia.rb b/lib/marginalia.rb index 7d20b25..dbcfe37 100644 --- a/lib/marginalia.rb +++ b/lib/marginalia.rb @@ -114,6 +114,14 @@ def record_query_comment end end + def self.with_comment_settings(settings) + prev = settings.keys.map { |key| [key, Marginalia::Comment.send(key)] } + settings.each { |key, value| Marginalia::Comment.send(:"#{key}=", value) } + yield + ensure + prev.each { |(key, value)| Marginalia::Comment.send(:"#{key}=", value) } + end + def self.with_annotation(comment, &block) Marginalia::Comment.inline_annotations.push(comment) block.call if block.present? diff --git a/lib/marginalia/comment.rb b/lib/marginalia/comment.rb index 2817d4d..8c3a9ca 100644 --- a/lib/marginalia/comment.rb +++ b/lib/marginalia/comment.rb @@ -3,6 +3,17 @@ module Marginalia module Comment mattr_accessor :components, :lines_to_ignore, :prepend_comment + + + # @return [String] String used for separating keys and values. + mattr_accessor :key_value_separator + self.key_value_separator = ':' + + # @return [false, :single] If `:single`, surrounds values with single quotes + # (') and escapes internal single quotes as \'. + mattr_accessor :quote_values + self.quote_values = false + Marginalia::Comment.components ||= [:application, :controller, :action] def self.update!(controller = nil) @@ -17,12 +28,19 @@ def self.update_adapter!(adapter) self.marginalia_adapter = adapter end + # @param [String] value + # @return [String] + def self.quote_value(value) + quote_values ? "'#{value.gsub("'", "\\\\'")}'" : value + end + def self.construct_comment ret = '' self.components.each do |c| component_value = self.send(c) if component_value.present? - ret << "#{c.to_s}:#{component_value.to_s}," + ret << "#{c}#{key_value_separator}"\ + "#{quote_value(component_value.to_s)}," end end ret.chop! diff --git a/test/query_comments_test.rb b/test/query_comments_test.rb index 525b675..6da9208 100644 --- a/test/query_comments_test.rb +++ b/test/query_comments_test.rb @@ -366,6 +366,21 @@ def test_add_comments_to_beginning_of_query Marginalia::Comment.prepend_comment = nil end + def test_comment_key_value_separator + Marginalia.with_comment_settings(key_value_separator: '=') do + ActiveRecord::Base.connection.execute "select id from posts" + assert_equal 'select id from posts /*application=rails*/', @queries.first + end + end + + def test_comment_quote_values_single + Marginalia.application_name = "Joe's app" + Marginalia.with_comment_settings(quote_values: :single) do + ActiveRecord::Base.connection.execute "select id from posts" + assert_equal "select id from posts /*application:'Joe\\'s app'*/", @queries.first + end + end + def teardown Marginalia.application_name = nil Marginalia::Comment.lines_to_ignore = nil