diff options
author | Peter Boling <peter.boling@gmail.com> | 2015-07-17 18:09:48 -0700 |
---|---|---|
committer | Peter Boling <peter.boling@gmail.com> | 2015-07-17 18:09:48 -0700 |
commit | 4fda1f2fade4dafb4f94d1f2a682750d2db5da53 (patch) | |
tree | cabc4205edccdf8505926cd57cd439183d48ec96 | |
parent | bec65fa261b9803c19edbbe9dc89836e1edf78c7 (diff) | |
download | rails-4fda1f2fade4dafb4f94d1f2a682750d2db5da53.tar.gz rails-4fda1f2fade4dafb4f94d1f2a682750d2db5da53.tar.bz2 rails-4fda1f2fade4dafb4f94d1f2a682750d2db5da53.zip |
Remove extraneous `then` from case statement
- CR feedback from @egilburg
Additionally
- Move logic for colorizing the payload name into a separate method
- Make some `ActiveRecord::LogSubscriber` instance methods private for clarity:
- `colorize_payload_name`
- `sql_color`
- `logger`
- Improve Changelog Documentation
GH #20885
-rw-r--r-- | activerecord/CHANGELOG.md | 22 | ||||
-rw-r--r-- | activerecord/lib/active_record/log_subscriber.rb | 31 |
2 files changed, 39 insertions, 14 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e130ebc632..5016d7c682 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,6 +1,22 @@ -* Improve sql logging coloration in `ActiveRecord::LogSubscriber`. +* Fix and improve sql logging coloration in `ActiveRecord::LogSubscriber`. + GH#20885 + Fixes coloring for SQL statements generated with Ruby heredoc, + which often have spaces preceding the initial SQL verb, like: + + sql = <<-EOS + SELECT * FROM THINGS + WHERE ID IN ( + SELECT ID FROM THINGS + ) + EOS + + Make some `ActiveRecord::LogSubscriber` instance methods private for clarity: + - `colorize_payload_name` + - `sql_color` + - `logger` + Improves coloring for statements like: # Become WHITE @@ -12,7 +28,9 @@ # Becomes RED ROLLBACK - Reinstates the coloration of the `payload[:name]`. + Reinstates the coloration of the `payload[:name]` + via new method `colorize_payload_name`. + Instead of simple alternating colors, adds meaning: - `MAGENTA` for `"SQL"` or `blank?` payload names - `CYAN` for Model Load/Exists diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index 9e3f1bfb9e..9241b8eb76 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -47,31 +47,37 @@ module ActiveRecord binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect end - if payload[:name].blank? || payload[:name] == "SQL" # SQL vs Model Load/Exists - name = color(name, MAGENTA, true) - else - name = color(name, CYAN, true) - end + name = colorize_payload_name(name, payload[:name]) sql = color(sql, sql_color(sql), true) debug " #{name} #{sql}#{binds}" end + private + + def colorize_payload_name(name, payload_name) + if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists + color(name, MAGENTA, true) + else + color(name, CYAN, true) + end + end + def sql_color(sql) case sql - when /\A\s*rollback/mi then + when /\A\s*rollback/mi RED - when /\s*.*?select .*for update/mi, /\A\s*lock/mi then + when /\s*.*?select .*for update/mi, /\A\s*lock/mi WHITE - when /\A\s*select/i then + when /\A\s*select/i BLUE - when /\A\s*insert/i then + when /\A\s*insert/i GREEN - when /\A\s*update/i then + when /\A\s*update/i YELLOW - when /\A\s*delete/i then + when /\A\s*delete/i RED - when /transaction\s*\Z/i then + when /transaction\s*\Z/i CYAN else MAGENTA @@ -81,6 +87,7 @@ module ActiveRecord def logger ActiveRecord::Base.logger end + end end |