Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
77 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
require 'active_record_unit' | |||
require 'active_record/rails/controller_runtime' | |||
require 'fixtures/project' | |||
|
|||
ActionController::Base.send :include, ActiveRecord::Rails::ControllerRuntime | |||
|
|||
class ARLoggingController < ActionController::Base | |||
def show | |||
render :inline => "<%= Project.all %>" | |||
end | |||
end | |||
|
|||
class ARLoggingTest < ActionController::TestCase | |||
tests ARLoggingController | |||
|
|||
def setup | |||
super | |||
set_logger | |||
end | |||
|
|||
def wait | |||
ActiveSupport::Notifications.notifier.wait | |||
end | |||
|
|||
def test_log_with_active_record | |||
get :show | |||
wait | |||
assert_match /ActiveRecord runtime/, logs[3] | |||
end | |||
|
|||
private | |||
def set_logger | |||
@controller.logger = MockLogger.new | |||
end | |||
|
|||
def logs | |||
@logs ||= @controller.logger.logged.compact.map {|l| l.to_s.strip} | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
activerecord/lib/active_record/rails/controller_runtime.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
module ActiveRecord | |||
module Rails | |||
module ControllerRuntime | |||
extend ActiveSupport::Concern | |||
|
|||
attr_internal :db_runtime | |||
|
|||
def cleanup_view_runtime | |||
if ActiveRecord::Base.connected? | |||
db_rt_before_render = ActiveRecord::Base.connection.reset_runtime | |||
runtime = super | |||
db_rt_after_render = ActiveRecord::Base.connection.reset_runtime | |||
self.db_runtime = db_rt_before_render + db_rt_after_render | |||
runtime - db_rt_after_render | |||
else | |||
super | |||
end | |||
end | |||
|
|||
module ClassMethods | |||
def log_process_action(controller) | |||
super | |||
db_runtime = controller.send :db_runtime | |||
logger.info(" ActiveRecord runtime: %.1fms" % db_runtime.to_f) if db_runtime | |||
end | |||
end | |||
end | |||
end | |||
end |