marginalia 1.0.3 → 1.1.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.

data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- marginalia (1.0.2)
4
+ marginalia (1.1.0)
5
5
  actionpack (>= 2.3, < 3.3)
6
6
  activerecord (>= 2.3, < 3.3)
7
7
 
data/README.md CHANGED
@@ -27,9 +27,12 @@ Patches are welcome for other database adapters.
27
27
 
28
28
  ### For Rails 3.x:
29
29
 
30
+ # Gemfile
30
31
  gem 'marginalia'
32
+
33
+ #config/application.rb
34
+ require 'marginalia/railtie'
31
35
 
32
- Then `bundle`, and that's it!
33
36
 
34
37
  ### For Rails 2.x:
35
38
 
@@ -57,6 +60,40 @@ Optionally, you can set the application name shown in the log like so in an init
57
60
  For Rails 3 applications, the name will default to your Rails application name.
58
61
  For Rails 2 applications, "rails" is used as the default application name.
59
62
 
63
+ You can also configure the components of the comment that will be appended,
64
+ by setting `Marginalia::Comment.components`. By default, this is set to:
65
+
66
+ Marginalia::Comment.components = [:application, :controller, :action]
67
+
68
+ Which results in a comment of
69
+ `application:#{application_name},controller:#{controller.name},action:#{action_name}`.
70
+
71
+ You can re-order or remove these components. You can also add additional
72
+ comment components of your desire by defining new module methods for
73
+ `Marginalia::Comment` which return a string. For example:
74
+
75
+ module Marginalia
76
+ module Comment
77
+ def self.mycommentcomponent
78
+ "TEST"
79
+ end
80
+ end
81
+ end
82
+
83
+ Marginalia::Comment.components = [:application, :mycommentcomponent]
84
+
85
+ Which will result in a comment like
86
+ `application:#{application_name},mycommentcomponent:TEST`
87
+ The calling controller is available to these methods via `@controller`.
88
+
89
+ Marginalia ships with `:application`, `:controller`, and `:action` enabled by
90
+ default. In addition, implementation is provided for:
91
+ * `:line` (for file and line number calling query). :line supports
92
+ a configuration by setting a regexp in `Marignalia::Comment.lines_to_ignore`
93
+ to exclude parts of the stacktrace from inclusion in the line comment.
94
+
95
+ Pull requests for other included comment components are welcome.
96
+
60
97
  ## Contributing
61
98
 
62
99
  Start by bundling and creating the test database:
data/lib/marginalia.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  require 'marginalia/railtie'
2
+ require 'marginalia/comment'
2
3
 
3
4
  module Marginalia
4
- mattr_accessor :comment, :application_name
5
+ mattr_accessor :application_name
5
6
 
6
7
  module ActiveRecordInstrumentation
7
8
  def self.included(instrumented_class)
8
- if defined? Rails.application
9
- Marginalia.application_name = Rails.application.class.name.split("::").first
10
- end
9
+ Marginalia::Comment.components = [:application, :controller, :action]
11
10
  instrumented_class.class_eval do
12
11
  if defined? :execute
13
12
  alias_method :execute_without_marginalia, :execute
@@ -17,7 +16,7 @@ module Marginalia
17
16
  end
18
17
 
19
18
  def execute_with_marginalia(sql, name = nil)
20
- execute_without_marginalia("#{sql} /*#{Marginalia.comment}*/", name)
19
+ execute_without_marginalia("#{sql} /*#{Marginalia::Comment.to_s}*/", name)
21
20
  end
22
21
  end
23
22
 
@@ -0,0 +1,57 @@
1
+ module Marginalia
2
+ module Comment
3
+ mattr_accessor :components, :comment, :lines_to_ignore
4
+
5
+ def self.update!(controller = nil)
6
+ @controller = controller
7
+ self.comment = self.components.collect{|c| "#{c}:#{self.send(c) }" }.join(",")
8
+ end
9
+
10
+ def self.to_s
11
+ self.comment
12
+ end
13
+
14
+ def self.clear!
15
+ self.comment = nil
16
+ end
17
+
18
+ private
19
+ def self.application
20
+ if defined? Rails.application
21
+ Marginalia.application_name ||= Rails.application.class.name.split("::").first
22
+ else
23
+ Marginalia.application_name ||= "rails"
24
+ end
25
+
26
+ Marginalia.application_name
27
+ end
28
+
29
+ def self.controller
30
+ @controller.controller_name if @controller.respond_to? :controller_name
31
+ end
32
+
33
+ def self.action
34
+ @controller.action_name if @controller.respond_to? :action_name
35
+ end
36
+
37
+ def self.line
38
+ Marginalia::Comment.lines_to_ignore ||= /\.rvm|gem|vendor|marginalia|rbenv/
39
+ last_line = caller.detect { |line| line !~ Marginalia::Comment.lines_to_ignore }
40
+ if last_line
41
+ root = if defined?(Rails) && Rails.respond_to?(:root)
42
+ Rails.root.to_s
43
+ elsif defined?(RAILS_ROOT)
44
+ RAILS_ROOT
45
+ else
46
+ ""
47
+ end
48
+ if last_line.starts_with? root
49
+ last_line = last_line[root.length..-1]
50
+ end
51
+ last_line
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -26,10 +26,10 @@ module Marginalia
26
26
  def self.insert_into_action_controller
27
27
  ActionController::Base.class_eval do
28
28
  def record_query_comment
29
- Marginalia.comment = "application:#{Marginalia.application_name || "rails"},controller:#{controller_name},action:#{action_name}"
29
+ Marginalia::Comment.update!(self)
30
30
  yield
31
31
  ensure
32
- Marginalia.comment = nil
32
+ Marginalia::Comment.clear!
33
33
  end
34
34
  around_filter :record_query_comment
35
35
  end
@@ -37,29 +37,36 @@ module Marginalia
37
37
 
38
38
  def self.insert_into_active_record
39
39
  if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
40
- ActiveRecord::ConnectionAdapters::Mysql2Adapter.module_eval do
41
- include Marginalia::ActiveRecordInstrumentation
40
+ if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
41
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter.module_eval do
42
+ include Marginalia::ActiveRecordInstrumentation
43
+ end
42
44
  end
43
45
  end
44
46
 
45
47
  if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
46
- ActiveRecord::ConnectionAdapters::MysqlAdapter.module_eval do
47
- include Marginalia::ActiveRecordInstrumentation
48
+ if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
49
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.module_eval do
50
+ include Marginalia::ActiveRecordInstrumentation
51
+ end
48
52
  end
49
53
  end
50
54
 
51
55
  if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
52
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.module_eval do
53
- include Marginalia::ActiveRecordInstrumentation
56
+ if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
57
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.module_eval do
58
+ include Marginalia::ActiveRecordInstrumentation
59
+ end
54
60
  end
55
61
  end
56
62
 
57
63
  if defined? ActiveRecord::ConnectionAdapters::SQLiteAdapter
58
- ActiveRecord::ConnectionAdapters::SQLiteAdapter.module_eval do
59
- include Marginalia::ActiveRecordInstrumentation
64
+ if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::SQLiteAdapter)
65
+ ActiveRecord::ConnectionAdapters::SQLiteAdapter.module_eval do
66
+ include Marginalia::ActiveRecordInstrumentation
67
+ end
60
68
  end
61
69
  end
62
-
63
70
  end
64
71
  end
65
72
  end
data/marginalia.gemspec CHANGED
@@ -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.0.3"
11
+ gem.version = "1.1.0"
12
12
 
13
13
  gem.add_runtime_dependency "actionpack", ">= 2.3", "< 3.3"
14
14
  gem.add_runtime_dependency "activerecord", ">= 2.3", "< 3.3"
@@ -4,6 +4,7 @@ require 'pp'
4
4
  require 'active_record'
5
5
  require 'action_controller'
6
6
  require 'marginalia'
7
+ RAILS_ROOT = File.expand_path(File.dirname(__FILE__))
7
8
 
8
9
  ActiveRecord::Base.establish_connection({
9
10
  :adapter => ENV["DRIVER"] || "mysql",
@@ -57,8 +58,29 @@ class MarginaliaTest < Test::Unit::TestCase
57
58
  assert_match %r{/\*application:customapp,controller:posts,action:driver_only\*/$}, @queries.first
58
59
  end
59
60
 
61
+ def test_configuring_query_components
62
+ Marginalia::Comment.components = [:controller]
63
+ PostsController.action(:driver_only).call(@env)
64
+
65
+ assert_match %r{/\*controller:posts\*/$}, @queries.first
66
+ end
67
+
68
+ def test_last_line_component
69
+ Marginalia::Comment.components = [:line]
70
+ PostsController.action(:driver_only).call(@env)
71
+ assert_match %r{/\*line:test/query_comments_test.rb:[0-9]*:in `call'\*/$}, @queries.first
72
+ end
73
+
74
+ def test_last_line_component_with_lines_to_ignore
75
+ Marginalia::Comment.lines_to_ignore = /foo bar/
76
+ Marginalia::Comment.components = [:line]
77
+ PostsController.action(:driver_only).call(@env)
78
+ assert_match %r{/\*line:.*lib/marginalia/comment.rb:7:in .*?\*/$}, @queries.first
79
+ end
80
+
60
81
  def teardown
61
82
  Marginalia.application_name = nil
83
+ Marginalia::Comment.components = [:application, :controller, :action]
62
84
  ActiveSupport::Notifications.unsubscribe "sql.active_record"
63
85
  end
64
86
  end
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.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-04-29 00:00:00.000000000 Z
14
+ date: 2012-04-30 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: actionpack
18
- requirement: &14527580 !ruby/object:Gem::Requirement
18
+ requirement: &9071720 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -26,10 +26,10 @@ dependencies:
26
26
  version: '3.3'
27
27
  type: :runtime
28
28
  prerelease: false
29
- version_requirements: *14527580
29
+ version_requirements: *9071720
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activerecord
32
- requirement: &14526420 !ruby/object:Gem::Requirement
32
+ requirement: &9070320 !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
@@ -40,10 +40,10 @@ dependencies:
40
40
  version: '3.3'
41
41
  type: :runtime
42
42
  prerelease: false
43
- version_requirements: *14526420
43
+ version_requirements: *9070320
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: rake
46
- requirement: &14525140 !ruby/object:Gem::Requirement
46
+ requirement: &9069220 !ruby/object:Gem::Requirement
47
47
  none: false
48
48
  requirements:
49
49
  - - ! '>='
@@ -51,10 +51,10 @@ dependencies:
51
51
  version: '0'
52
52
  type: :development
53
53
  prerelease: false
54
- version_requirements: *14525140
54
+ version_requirements: *9069220
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mysql
57
- requirement: &14514160 !ruby/object:Gem::Requirement
57
+ requirement: &9058300 !ruby/object:Gem::Requirement
58
58
  none: false
59
59
  requirements:
60
60
  - - ! '>='
@@ -62,10 +62,10 @@ dependencies:
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
- version_requirements: *14514160
65
+ version_requirements: *9058300
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: mysql2
68
- requirement: &14513460 !ruby/object:Gem::Requirement
68
+ requirement: &9057600 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
71
  - - ! '>='
@@ -73,10 +73,10 @@ dependencies:
73
73
  version: '0'
74
74
  type: :development
75
75
  prerelease: false
76
- version_requirements: *14513460
76
+ version_requirements: *9057600
77
77
  - !ruby/object:Gem::Dependency
78
78
  name: pg
79
- requirement: &14512660 !ruby/object:Gem::Requirement
79
+ requirement: &9057080 !ruby/object:Gem::Requirement
80
80
  none: false
81
81
  requirements:
82
82
  - - ! '>='
@@ -84,10 +84,10 @@ dependencies:
84
84
  version: '0'
85
85
  type: :development
86
86
  prerelease: false
87
- version_requirements: *14512660
87
+ version_requirements: *9057080
88
88
  - !ruby/object:Gem::Dependency
89
89
  name: sqlite3
90
- requirement: &14511580 !ruby/object:Gem::Requirement
90
+ requirement: &9055100 !ruby/object:Gem::Requirement
91
91
  none: false
92
92
  requirements:
93
93
  - - ! '>='
@@ -95,7 +95,7 @@ dependencies:
95
95
  version: '0'
96
96
  type: :development
97
97
  prerelease: false
98
- version_requirements: *14511580
98
+ version_requirements: *9055100
99
99
  description:
100
100
  email:
101
101
  - noah@37signals.com
@@ -113,6 +113,7 @@ files:
113
113
  - Rakefile
114
114
  - init.rb
115
115
  - lib/marginalia.rb
116
+ - lib/marginalia/comment.rb
116
117
  - lib/marginalia/railtie.rb
117
118
  - marginalia.gemspec
118
119
  - test/query_comments_test.rb