activerecord-cause 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a18619979f3889a97e7280fe083029b4f83c42c4
4
- data.tar.gz: 3163a8a41f7aaa493fbeab493d6dface52e2f9e6
3
+ metadata.gz: 3633af93848d0ee412d2e055aba320859a69bfbd
4
+ data.tar.gz: 90d3a3d9c599e197acfaac3f049030cb1adb5012
5
5
  SHA512:
6
- metadata.gz: 5195069b3cb3f905d625d26ee8c75ea92ecab47c3d5056cfdcc30969b59163e0b499365fdb7ee946da10a76bb8f4e1a32bdc6a3e14e954da40a3ea0dfac71033
7
- data.tar.gz: d081b98ce509d9c768186888621967d1d5d1261765da82f0f26cbebdd25796b49593951d0ce1d13dafa1626dd4dd70cd1bfb1862d74859cd202c508e04c423c1
6
+ metadata.gz: 9bcd4c7215f7bcb314e300f5474b94e04526f6ab5e5bf96b7fbde6bd785ee3d04fb1aa49c4546f885788aae29d82dace79c2412447adb1d9e00da4318e547d4c
7
+ data.tar.gz: 2618b0b4f6a67128620083b57180b4f784fe0a04c4f73a8c92306b84db37c34194b62786b6c3def4583cadfc61427ec7cbb6f697ca138f12e2ccf4fbd0421dc7
@@ -6,5 +6,5 @@ rvm:
6
6
  gemfile:
7
7
  - gemfiles/activerecord-42.gemfile
8
8
  - gemfiles/activerecord-502.gemfile
9
- - gemfiles/activerecord-50.gemfile
9
+ - gemfiles/activerecord-503.gemfile
10
10
  - gemfiles/activerecord-51.gemfile
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activerecord", "~> 5.0.0"
3
+ gem "activerecord", "~> 5.0.3"
4
4
 
5
5
  gemspec :path => "../"
@@ -27,26 +27,9 @@ module ActiveRecord
27
27
  return unless logger.debug?
28
28
 
29
29
  payload = event.payload
30
-
31
30
  return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
32
31
 
33
- if ActiveRecord.version >= Gem::Version.new("5.0.0.beta")
34
- sql_for_ar5(event)
35
- else
36
- sql_for_ar4(event)
37
- end
38
- end
39
-
40
- private
41
-
42
- def sql_for_ar4(event)
43
- payload = event.payload
44
- locations = caller_locations.select do |l|
45
- ActiveRecord::Cause.match_paths.any? do |re|
46
- re.match(l.absolute_path)
47
- end
48
- end
49
-
32
+ locations = get_locations
50
33
  return if locations.empty?
51
34
 
52
35
  if ActiveRecord::Cause.log_mode != :all
@@ -54,22 +37,14 @@ module ActiveRecord
54
37
  end
55
38
 
56
39
  locations.each do |loc|
57
- name = "#{payload[:name]} (ActiveRecord::Cause)"
58
- sql = payload[:sql]
59
- binds = nil
40
+ @is_odd = nil
60
41
 
61
42
  unless (payload[:binds] || []).empty?
62
- binds = " " + payload[:binds].map { |col,v|
63
- render_bind(col, v)
64
- }.inspect
43
+ binds = get_binds(payload)
65
44
  end
66
45
 
67
- if odd?
68
- name = color(name, CYAN, true)
69
- sql = color(sql, nil, true)
70
- else
71
- name = color(name, MAGENTA, true)
72
- end
46
+ name = name_with_color(payload[:name])
47
+ sql = sql_with_color(payload[:sql])
73
48
  cause = color(loc.to_s, nil, true)
74
49
 
75
50
  output =
@@ -83,41 +58,11 @@ module ActiveRecord
83
58
  end
84
59
  end
85
60
 
86
- def sql_for_ar5(event)
87
- payload = event.payload
88
- locations = get_locations
89
- return if locations.empty?
90
-
91
- if ActiveRecord::Cause.log_mode != :all
92
- locations = locations.take(1)
93
- end
94
-
95
- locations.each do |loc|
96
- name = "#{payload[:name]} (ActiveRecord::Cause)"
97
- sql = payload[:sql]
98
- binds = nil
99
-
100
- unless (payload[:binds] || []).empty?
101
- binds = if ActiveRecord.version >= Gem::Version.new("5.0.3")
102
- " " + payload[:binds].zip(payload[:type_casted_binds]).map { |attr, value| render_bind(attr, value) }.inspect
103
- else
104
- " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
105
- end
106
- end
107
-
108
- name = colorize_payload_name(name, payload[:name])
109
- sql = color(sql, sql_color(sql), true)
110
- cause = color(loc.to_s, nil, true)
111
-
112
- output =
113
- if ActiveRecord::Cause.log_with_sql
114
- " #{name} #{sql}#{binds} caused by #{cause}"
115
- else
116
- " #{name} caused by #{cause}"
117
- end
61
+ private
118
62
 
119
- debug(output)
120
- end
63
+ def is_odd?
64
+ return @is_odd unless @is_odd.nil?
65
+ @is_odd = odd?
121
66
  end
122
67
 
123
68
  def get_locations
@@ -128,6 +73,79 @@ module ActiveRecord
128
73
  end
129
74
  end
130
75
  end
76
+
77
+ def get_binds(payload)
78
+ raise NotImplementedError
79
+ end
80
+
81
+ def name_with_color(payload_name)
82
+ raise NotImplementedError
83
+ end
84
+
85
+ def sql_with_color(payload_sql)
86
+ raise NotImplementedError
87
+ end
88
+ end
89
+
90
+ class LogSubscriberAR4 < ActiveRecord::Cause::LogSubscriber
91
+ def sql(event)
92
+ super
93
+ end
94
+
95
+ private
96
+
97
+ def get_binds(payload)
98
+ " " + payload[:binds].map { |col,v|
99
+ render_bind(col, v)
100
+ }.inspect
101
+ end
102
+
103
+ def name_with_color(payload_name)
104
+ name = "#{payload_name} (ActiveRecord::Cause)"
105
+ if is_odd?
106
+ color(name, CYAN, true)
107
+ else
108
+ color(name, MAGENTA, true)
109
+ end
110
+ end
111
+
112
+ def sql_with_color(payload_sql)
113
+ is_odd? ? color(payload_sql, nil, true) : payload_sql
114
+ end
115
+ end
116
+
117
+ class LogSubscriberAR502 < ActiveRecord::Cause::LogSubscriber
118
+ def sql(event)
119
+ super
120
+ end
121
+
122
+ private
123
+
124
+ def get_binds(payload)
125
+ " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
126
+ end
127
+
128
+ def name_with_color(payload_name)
129
+ name = "#{payload_name} (ActiveRecord::Cause)"
130
+ colorize_payload_name(name, payload_name)
131
+ end
132
+
133
+ def sql_with_color(sql)
134
+ color(sql, sql_color(sql), true)
135
+ end
136
+ end
137
+
138
+ class LogSubscriberAR503 < LogSubscriberAR502
139
+ def sql(event)
140
+ super
141
+ end
142
+
143
+ private
144
+
145
+ def get_binds(payload)
146
+ casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
147
+ " " + payload[:binds].zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
148
+ end
131
149
  end
132
150
  end
133
151
  end
@@ -135,5 +153,11 @@ end
135
153
  require "activerecord/cause/railtie" if defined?(Rails)
136
154
 
137
155
  ActiveSupport.on_load(:active_record) do
138
- ActiveRecord::Cause::LogSubscriber.attach_to :active_record
156
+ if ActiveRecord.version >= Gem::Version.new("5.0.3")
157
+ ActiveRecord::Cause::LogSubscriberAR503.attach_to :active_record
158
+ elsif ActiveRecord.version >= Gem::Version.new("5.0.0")
159
+ ActiveRecord::Cause::LogSubscriberAR502.attach_to :active_record
160
+ else
161
+ ActiveRecord::Cause::LogSubscriberAR4.attach_to :active_record
162
+ end
139
163
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Cause
3
- VERSION = "0.5.0"
3
+ VERSION = "0.5.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-cause
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-19 00:00:00.000000000 Z
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -97,8 +97,8 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - gemfiles/activerecord-42.gemfile
100
- - gemfiles/activerecord-50.gemfile
101
100
  - gemfiles/activerecord-502.gemfile
101
+ - gemfiles/activerecord-503.gemfile
102
102
  - gemfiles/activerecord-51.gemfile
103
103
  - lib/activerecord-cause.rb
104
104
  - lib/activerecord/cause.rb