pp_sql 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f27b89855f77001300508a599f026baa13e5399
4
- data.tar.gz: 7cb4493dd7d2b14cdb9403d4841edd3d240f5168
3
+ metadata.gz: b4372a6aad0cedf3145eff498f19164e31e2918c
4
+ data.tar.gz: a8596ffe4a78038d3817fe0b6127761cf66777e7
5
5
  SHA512:
6
- metadata.gz: e8b1f3e5e92fc75bffed713b5cad6921306623bf129b06dff4256d9d333fcb1f2865245bc728624965cc390c589948b514db96f5597152f0fd924ce65b5f5b9e
7
- data.tar.gz: 702f39163b95445dc07aaba57ea72fd8dd6772a7e918ae9756db6bcae6eef12d13abf49bfffe5ebc0d76253b97528fb01866579d8f9a82068159d8bae1a2afd7
6
+ metadata.gz: ae497360ecd64748259be104332eb38ae2a07a833e58e63f222bf8b14c4ec9523ea40495622f1c95ca52c6d0a21a3891a60e8b6d7cdc1551b4282b06ddbafe62
7
+ data.tar.gz: 4208494b16e764d76460c59e6038c7af289c1749118791c17f3f13a342ec9d05b8d60a7a333376805c26d25dd4ca66f0e83c5fab7afbffb6bdba97e9f62bdcd5
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # PpSql
2
2
 
3
- Replace stanpard `ActieRecord#to_sql` method with [`anbt-sql-formatter`](https://github.com/sonota88/anbt-sql-formatter)
4
- gem for pretty SQL code output in console. For example:
3
+ Replace stanpard `ActiveRecord#to_sql` method with [`anbt-sql-formatter`](https://github.com/sonota88/anbt-sql-formatter)
4
+ gem for pretty SQL code output in console. Rails log will be formatted also. For example:
5
+
5
6
  ```
6
7
  # was
7
8
  User.all # =>
@@ -15,13 +16,17 @@ SELECT
15
16
  `users`
16
17
  ```
17
18
 
19
+ ## Require
20
+
21
+ Ruby 2.0+
22
+
18
23
  ## Usage
19
24
 
20
25
  ```
21
26
  Post.first.to_sql
22
27
  ```
23
28
 
24
- If you need yo use it in some custome strings, you may include this funcional with
29
+ If you need yo use it in some custom strings, you may include this funcionality with
25
30
 
26
31
  ```
27
32
  String.send :include, PpSql::ToSqlBeautify
@@ -1,3 +1,3 @@
1
1
  module PpSql
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/pp_sql.rb CHANGED
@@ -1,13 +1,5 @@
1
1
  module PpSql
2
- module ToSqlBeautify
3
- def to_sql
4
- _sql_formatter.format(super)
5
- end
6
-
7
- def pp_sql
8
- puts to_sql
9
- end
10
-
2
+ module Formatter
11
3
  private
12
4
 
13
5
  def _sql_formatter
@@ -20,10 +12,47 @@ module PpSql
20
12
  @_sql_formatter = AnbtSql::Formatter.new(rule)
21
13
  end
22
14
  end
15
+ module ToSqlBeautify
16
+ include Formatter
17
+ def to_sql
18
+ _sql_formatter.format(super)
19
+ end
20
+
21
+ def pp_sql
22
+ puts to_sql
23
+ end
24
+ end
25
+ module LogSubscriberPrettyPrint
26
+ include Formatter
27
+ def sql(event)
28
+ return unless logger.debug?
29
+
30
+ self.class.runtime += event.duration
31
+
32
+ payload = event.payload
33
+
34
+ return if ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
35
+
36
+ name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
37
+ sql = payload[:sql]
38
+ binds = nil
39
+
40
+ unless (payload[:binds] || []).empty?
41
+ binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
42
+ end
43
+
44
+ name = colorize_payload_name(name, payload[:name])
45
+ # only this line was rewritten from the AR
46
+ sql = color(_sql_formatter.format(sql.dup), sql_color(sql), true)
47
+
48
+ debug " #{name} #{sql}#{binds}"
49
+ end
50
+ end
23
51
  class Railtie < Rails::Railtie
24
52
  initializer "pp_sql.override_to_sql" do
25
53
  ActiveSupport.on_load(:active_record) do
26
54
  ActiveRecord::Relation.send(:prepend, ToSqlBeautify)
55
+ ActiveRecord::LogSubscriber.send(:prepend, LogSubscriberPrettyPrint)
27
56
  end
28
57
  end
29
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pp_sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kvokka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-05 00:00:00.000000000 Z
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails