Permalink
Browse files

Merge branch 'configurable-comments'

  • Loading branch information...
2 parents 2cb6804 + ac478ea commit 730e619ae7e555119b098f84b20b6b4109d732eb @noahhl noahhl committed Apr 29, 2012
Showing with 83 additions and 7 deletions.
  1. +29 −0 README.md
  2. +4 −5 lib/marginalia.rb
  3. +40 −0 lib/marginalia/comment.rb
  4. +2 −2 lib/marginalia/railtie.rb
  5. +8 −0 test/query_comments_test.rb
View
@@ -60,6 +60,35 @@ Optionally, you can set the application name shown in the log like so in an init
For Rails 3 applications, the name will default to your Rails application name.
For Rails 2 applications, "rails" is used as the default application name.
+You can also configure the components of the comment that will be appended,
+by setting `Marginalia::Comment.components`. By default, this is set to:
+
+ Marginalia::Comment.components = [:application, :controller, :action]
+
+Which results in a comment of
+`application:#{application_name},controller:#{controller.name},action:#{action_name}`.
+
+You can re-order or remove these components. You can also add additional
+comment components of your desire by defining new module methods for
+`Marginalia::Comment` which return a string. For example:
+
+ module Marginalia
+ module Comment
+ def self.mycommentcomponent
+ "TEST"
+ end
+ end
+ end
+
+ Marginalia::Comment.components = [:application, :mycommentcomponent]
+
+Which will result in a comment like
+`application:#{application_name},mycommentcomponent:TEST`
+The calling controller is available to these methods via `@controller`.
+
+Marginalia ships with `:application`, `:controller`, and `:action` enabled by
+default.
+
## Contributing
Start by bundling and creating the test database:
View
@@ -1,13 +1,12 @@
require 'marginalia/railtie'
+require 'marginalia/comment'
module Marginalia
- mattr_accessor :comment, :application_name
+ mattr_accessor :application_name
module ActiveRecordInstrumentation
def self.included(instrumented_class)
- if defined? Rails.application
- Marginalia.application_name = Rails.application.class.name.split("::").first
- end
+ Marginalia::Comment.components = [:application, :controller, :action]
instrumented_class.class_eval do
if defined? :execute
alias_method :execute_without_marginalia, :execute
@@ -17,7 +16,7 @@ def self.included(instrumented_class)
end
def execute_with_marginalia(sql, name = nil)
- execute_without_marginalia("#{sql} /*#{Marginalia.comment}*/", name)
+ execute_without_marginalia("#{sql} /*#{Marginalia::Comment.to_s}*/", name)
end
end
View
@@ -0,0 +1,40 @@
+module Marginalia
+ module Comment
+ mattr_accessor :components, :comment
+
+ def self.update!(controller = nil)
+ @controller = controller
+ self.comment = self.components.collect{|c| "#{c}:#{self.send(c) }" }.join(",")
+ end
+
+ def self.to_s
+ self.comment
+ end
+
+ def self.clear!
+ self.comment = nil
+ end
+
+ private
+ def self.application
+ if defined? Rails.application
+ Marginalia.application_name ||= Rails.application.class.name.split("::").first
+ else
+ Marginalia.application_name ||= "rails"
+ end
+
+ Marginalia.application_name
+ end
+
+ def self.controller
+ @controller.controller_name if @controller.respond_to? :controller_name
+ end
+
+ def self.action
+ @controller.action_name if @controller.respond_to? :action_name
+ end
+
+
+ end
+
+end
@@ -26,10 +26,10 @@ def self.insert
def self.insert_into_action_controller
ActionController::Base.class_eval do
def record_query_comment
- Marginalia.comment = "application:#{Marginalia.application_name || "rails"},controller:#{controller_name},action:#{action_name}"
+ Marginalia::Comment.update!(self)
yield
ensure
- Marginalia.comment = nil
+ Marginalia::Comment.clear!
end
around_filter :record_query_comment
end
@@ -57,8 +57,16 @@ def test_configuring_application
assert_match %r{/\*application:customapp,controller:posts,action:driver_only\*/$}, @queries.first
end
+ def test_configuring_query_components
+ Marginalia::Comment.components = [:controller]
+ PostsController.action(:driver_only).call(@env)
+
+ assert_match %r{/\*controller:posts\*/$}, @queries.first
+ end
+
def teardown
Marginalia.application_name = nil
+ Marginalia::Comment.components = [:application, :controller, :action]
ActiveSupport::Notifications.unsubscribe "sql.active_record"
end
end

0 comments on commit 730e619

Please sign in to comment.