marginalia 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of marginalia might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5352b63598e762229b9f6a9a7b4b0b27dc42a277
4
- data.tar.gz: d45c0855bf418e0dd277e553b5fe45d47b6d3a57
3
+ metadata.gz: 6f9f187276b089ab51a1f5e267f8e58e125b1ec6
4
+ data.tar.gz: 011dbacbca884d0c40b757e408603a51e4b75639
5
5
  SHA512:
6
- metadata.gz: 7895a8c8318f85bfb18aa2ba90f8223eaacd54b190d6adc45aff3f139c3ed6a494858c644095b2edcf608f42f9d0c5e4c48bbf432ac0a1c523b7d3c411e2ce1f
7
- data.tar.gz: 8e5493b2233a05245de9f6c4b1d346b08cbe3dd6de0021e037a73e1cea35da8599469198802c459306bbd0168974ebc210d1cf8d62f88d48a61e9df7bfa1ed33
6
+ metadata.gz: b4992f558f137d11af12f9cc4208dcfa7d9a2d8c2bf7be426bdca1668cf44aca39777206f4abe8f51cb12acbbea07683883ec4cc6956bf46a0ce7770594fe4b2
7
+ data.tar.gz: 639570873d23475c04fe0ba94bcb84c317efd4bbaee87d3a978dc013c8cd6191ce8ee077d6fc8001940d57267335f26c9846a419a5adc7faba77d5487866c52d
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.2.0
@@ -4,6 +4,7 @@ rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
6
  - 2.1
7
+ - 2.2
7
8
 
8
9
  sudo: false
9
10
 
data/Gemfile CHANGED
@@ -2,9 +2,7 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- version = ENV["RAILS_VERSION"] || "4.0.2"
6
- rails_api = ENV["TEST_RAILS_API"] == "true"
7
-
5
+ version = ENV["RAILS_VERSION"] || "4.2.0"
8
6
  rails = case version
9
7
  when "master"
10
8
  {:github => "rails/rails"}
@@ -14,6 +12,6 @@ end
14
12
 
15
13
  gem "rails", rails
16
14
 
17
- if rails_api
15
+ if ENV["TEST_RAILS_API"] == "true"
18
16
  gem "rails-api", "~> 0.2.1"
19
17
  end
data/README.md CHANGED
@@ -93,6 +93,7 @@ default. In addition, implementation is provided for:
93
93
  to exclude parts of the stacktrace from inclusion in the line comment.
94
94
  * `:controller_with_namespace` to include the full classname (including namespace)
95
95
  of the controller.
96
+ * `:job` to include the classname of the ActiveJob being performed.
96
97
 
97
98
  Pull requests for other included comment components are welcome.
98
99
 
@@ -21,6 +21,21 @@ module Marginalia
21
21
  alias_method :exec_query, :exec_query_with_marginalia
22
22
  end
23
23
  end
24
+
25
+ is_postgres = defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
26
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter == instrumented_class
27
+ # Instrument exec_delete and exec_update on AR 3.2+, since they don't
28
+ # call execute internally
29
+ if is_postgres && ActiveRecord::VERSION::STRING > "3.1"
30
+ if instrumented_class.method_defined?(:exec_delete)
31
+ alias_method :exec_delete_without_marginalia, :exec_delete
32
+ alias_method :exec_delete, :exec_delete_with_marginalia
33
+ end
34
+ if instrumented_class.method_defined?(:exec_update)
35
+ alias_method :exec_update_without_marginalia, :exec_update
36
+ alias_method :exec_update, :exec_update_with_marginalia
37
+ end
38
+ end
24
39
  end
25
40
  end
26
41
 
@@ -40,6 +55,28 @@ module Marginalia
40
55
  def exec_query_with_marginalia(sql, name = 'SQL', binds = [])
41
56
  exec_query_without_marginalia(annotate_sql(sql), name, binds)
42
57
  end
58
+
59
+ def exec_delete_with_marginalia(sql, name = 'SQL', binds = [])
60
+ exec_delete_without_marginalia(annotate_sql(sql), name, binds)
61
+ end
62
+
63
+ def exec_update_with_marginalia(sql, name = 'SQL', binds = [])
64
+ exec_update_without_marginalia(annotate_sql(sql), name, binds)
65
+ end
43
66
  end
44
67
 
68
+ module ActionControllerInstrumentation
69
+ def self.included(instrumented_class)
70
+ instrumented_class.class_eval do
71
+ around_filter :record_query_comment
72
+ end
73
+ end
74
+
75
+ def record_query_comment
76
+ Marginalia::Comment.update!(self)
77
+ yield
78
+ ensure
79
+ Marginalia::Comment.clear!
80
+ end
81
+ end
45
82
  end
@@ -6,7 +6,11 @@ module Marginalia
6
6
  Marginalia::Comment.components ||= [:application, :controller, :action]
7
7
 
8
8
  def self.update!(controller = nil)
9
- @controller = controller
9
+ self.marginalia_controller = controller
10
+ end
11
+
12
+ def self.update_job!(job)
13
+ self.marginalia_job = job
10
14
  end
11
15
 
12
16
  def self.construct_comment
@@ -22,10 +26,30 @@ module Marginalia
22
26
  end
23
27
 
24
28
  def self.clear!
25
- @controller = nil
29
+ self.marginalia_controller = nil
30
+ end
31
+
32
+ def self.clear_job!
33
+ self.marginalia_job = nil
26
34
  end
27
35
 
28
36
  private
37
+ def self.marginalia_controller=(controller)
38
+ Thread.current[:marginalia_controller] = controller
39
+ end
40
+
41
+ def self.marginalia_controller
42
+ Thread.current[:marginalia_controller]
43
+ end
44
+
45
+ def self.marginalia_job=(job)
46
+ Thread.current[:marginalia_job] = job
47
+ end
48
+
49
+ def self.marginalia_job
50
+ Thread.current[:marginalia_job]
51
+ end
52
+
29
53
  def self.application
30
54
  if defined?(Rails.application)
31
55
  Marginalia.application_name ||= Rails.application.class.name.split("::").first
@@ -36,16 +60,20 @@ module Marginalia
36
60
  Marginalia.application_name
37
61
  end
38
62
 
63
+ def self.job
64
+ marginalia_job.class.name if marginalia_job
65
+ end
66
+
39
67
  def self.controller
40
- @controller.controller_name if @controller.respond_to? :controller_name
68
+ marginalia_controller.controller_name if marginalia_controller.respond_to? :controller_name
41
69
  end
42
70
 
43
71
  def self.controller_with_namespace
44
- @controller.class.name if @controller
72
+ marginalia_controller.class.name if marginalia_controller
45
73
  end
46
74
 
47
75
  def self.action
48
- @controller.action_name if @controller.respond_to? :action_name
76
+ marginalia_controller.action_name if marginalia_controller.respond_to? :action_name
49
77
  end
50
78
 
51
79
  def self.line
@@ -77,8 +105,8 @@ module Marginalia
77
105
  end
78
106
 
79
107
  def self.request_id
80
- if @controller.respond_to?(:request) && @controller.request.respond_to?(:uuid)
81
- @controller.request.uuid
108
+ if marginalia_controller.respond_to?(:request) && marginalia_controller.request.respond_to?(:uuid)
109
+ marginalia_controller.request.uuid
82
110
  end
83
111
  end
84
112
  end
@@ -13,6 +13,10 @@ module Marginalia
13
13
  ActiveSupport.on_load :action_controller do
14
14
  Marginalia::Railtie.insert_into_action_controller
15
15
  end
16
+
17
+ ActiveSupport.on_load :active_job do
18
+ Marginalia::Railtie.insert_into_active_job
19
+ end
16
20
  end
17
21
  end
18
22
  end
@@ -21,21 +25,28 @@ module Marginalia
21
25
  def self.insert
22
26
  insert_into_active_record
23
27
  insert_into_action_controller
28
+ insert_into_active_job
24
29
  end
25
30
 
26
- def self.insert_into_action_controller
27
- controller_instrumentation = <<-EOS
28
- def record_query_comment
29
- Marginalia::Comment.update!(self)
30
- yield
31
- ensure
32
- Marginalia::Comment.clear!
31
+ def self.insert_into_active_job
32
+ if defined? ActiveJob::Base
33
+ ActiveJob::Base.class_eval do
34
+ around_perform do |job, block|
35
+ begin
36
+ Marginalia::Comment.update_job! job
37
+ block.call
38
+ ensure
39
+ Marginalia::Comment.clear_job!
40
+ end
41
+ end
33
42
  end
34
- around_filter :record_query_comment
35
- EOS
36
- ActionController::Base.class_eval(controller_instrumentation)
43
+ end
44
+ end
45
+
46
+ def self.insert_into_action_controller
47
+ ActionController::Base.send(:include, ActionControllerInstrumentation)
37
48
  if defined? ActionController::API
38
- ActionController::API.class_eval(controller_instrumentation)
49
+ ActionController::API.send(:include, ActionControllerInstrumentation)
39
50
  end
40
51
  end
41
52
 
@@ -52,7 +63,6 @@ module Marginalia
52
63
  end
53
64
  end
54
65
 
55
- # SQL queries made through PostgreSQLAdapter#exec_delete will not be annotated.
56
66
  if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
57
67
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.module_eval do
58
68
  include Marginalia::ActiveRecordInstrumentation
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.test_files = `git ls-files -- {test}/*`.split("\n")
9
9
  gem.name = "marginalia"
10
10
  gem.require_paths = ["lib"]
11
- gem.version = "1.2.0"
11
+ gem.version = "1.3.0"
12
12
  gem.license = "MIT"
13
13
 
14
14
  gem.add_runtime_dependency "actionpack", ">= 2.3"
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency "mysql2"
19
19
  gem.add_development_dependency "pg"
20
20
  gem.add_development_dependency "sqlite3"
21
+ gem.add_development_dependency "minitest"
21
22
  gem.add_development_dependency "mocha"
22
23
 
23
24
  gem.summary = gem.description = %q{Attach comments to your ActiveRecord queries.}
@@ -9,6 +9,10 @@ def request_id_available?
9
9
  Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('3.2')
10
10
  end
11
11
 
12
+ def active_job_available?
13
+ Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('4.2')
14
+ end
15
+
12
16
  require "minitest/autorun"
13
17
  require 'mocha/test_unit'
14
18
  require 'logger'
@@ -20,6 +24,10 @@ if request_id_available?
20
24
  require 'action_dispatch/middleware/request_id'
21
25
  end
22
26
 
27
+ if active_job_available?
28
+ require 'active_job'
29
+ end
30
+
23
31
  if using_rails_api?
24
32
  require 'rails-api/action_controller/api'
25
33
  end
@@ -63,6 +71,14 @@ module API
63
71
  end
64
72
  end
65
73
 
74
+ if active_job_available?
75
+ class PostsJob < ActiveJob::Base
76
+ def perform
77
+ Post.first
78
+ end
79
+ end
80
+ end
81
+
66
82
  if using_rails_api?
67
83
  class PostsApiController < ActionController::API
68
84
  def driver_only
@@ -110,6 +126,22 @@ class MarginaliaTest < MiniTest::Test
110
126
  end
111
127
  end
112
128
 
129
+ if ENV["DRIVER"] =~ /^postgres/
130
+ def test_query_commenting_on_postgres_update
131
+ ActiveRecord::Base.connection.expects(:annotate_sql).returns("update posts set id = 1").once
132
+ ActiveRecord::Base.connection.send(:exec_update, "update posts set id = 1")
133
+ ensure
134
+ ActiveRecord::Base.connection.unstub(:annotate_sql)
135
+ end
136
+
137
+ def test_query_commenting_on_postgres_delete
138
+ ActiveRecord::Base.connection.expects(:annotate_sql).returns("delete from posts where id = 1").once
139
+ ActiveRecord::Base.connection.send(:exec_delete, "delete from posts where id = 1")
140
+ ensure
141
+ ActiveRecord::Base.connection.unstub(:annotate_sql)
142
+ end
143
+ end
144
+
113
145
  def test_query_commenting_on_mysql_driver_with_action
114
146
  PostsController.action(:driver_only).call(@env)
115
147
  assert_match %r{select id from posts /\*application:rails,controller:posts,action:driver_only\*/$}, @queries.first
@@ -195,6 +227,17 @@ class MarginaliaTest < MiniTest::Test
195
227
  end
196
228
  end
197
229
 
230
+ if active_job_available?
231
+ def test_active_job
232
+ Marginalia::Comment.components = [:job]
233
+ PostsJob.perform_later
234
+ assert_match %{job:PostsJob}, @queries.first
235
+
236
+ Post.first
237
+ refute_match %{job:PostsJob}, @queries.last
238
+ end
239
+ end
240
+
198
241
  def teardown
199
242
  Marginalia.application_name = nil
200
243
  Marginalia::Comment.lines_to_ignore = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marginalia
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Lorang
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-20 00:00:00.000000000 Z
13
+ date: 2015-02-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -110,6 +110,20 @@ dependencies:
110
110
  - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: minitest
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
113
127
  - !ruby/object:Gem::Dependency
114
128
  name: mocha
115
129
  requirement: !ruby/object:Gem::Requirement
@@ -164,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
178
  version: '0'
165
179
  requirements: []
166
180
  rubyforge_project:
167
- rubygems_version: 2.2.0
181
+ rubygems_version: 2.4.5
168
182
  signing_key:
169
183
  specification_version: 4
170
184
  summary: Attach comments to your ActiveRecord queries.