aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_view/helpers.rb5
-rw-r--r--actionpack/lib/action_view/helpers/benchmark_helper.rb13
-rw-r--r--actionpack/test/template/benchmark_helper_test.rb24
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/test/cases/dup_test.rb8
-rw-r--r--activerecord/test/models/topic.rb5
6 files changed, 53 insertions, 4 deletions
diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb
index 262e0f1010..f2a3a494bc 100644
--- a/actionpack/lib/action_view/helpers.rb
+++ b/actionpack/lib/action_view/helpers.rb
@@ -1,5 +1,3 @@
-require 'active_support/benchmarkable'
-
module ActionView #:nodoc:
module Helpers #:nodoc:
extend ActiveSupport::Autoload
@@ -7,6 +5,7 @@ module ActionView #:nodoc:
autoload :ActiveModelHelper
autoload :AssetTagHelper
autoload :AtomFeedHelper
+ autoload :BenchmarkHelper
autoload :CacheHelper
autoload :CaptureHelper
autoload :ControllerHelper
@@ -33,10 +32,10 @@ module ActionView #:nodoc:
extend SanitizeHelper::ClassMethods
end
- include ActiveSupport::Benchmarkable
include ActiveModelHelper
include AssetTagHelper
include AtomFeedHelper
+ include BenchmarkHelper
include CacheHelper
include CaptureHelper
include ControllerHelper
diff --git a/actionpack/lib/action_view/helpers/benchmark_helper.rb b/actionpack/lib/action_view/helpers/benchmark_helper.rb
new file mode 100644
index 0000000000..dfdd5a786d
--- /dev/null
+++ b/actionpack/lib/action_view/helpers/benchmark_helper.rb
@@ -0,0 +1,13 @@
+require 'active_support/benchmarkable'
+
+module ActionView
+ module Helpers
+ module BenchmarkHelper
+ include ActiveSupport::Benchmarkable
+
+ def benchmark(*)
+ capture { super }
+ end
+ end
+ end
+end
diff --git a/actionpack/test/template/benchmark_helper_test.rb b/actionpack/test/template/benchmark_helper_test.rb
new file mode 100644
index 0000000000..1bdda22959
--- /dev/null
+++ b/actionpack/test/template/benchmark_helper_test.rb
@@ -0,0 +1,24 @@
+require 'abstract_unit'
+require 'stringio'
+
+class BenchmarkHelperTest < ActionView::TestCase
+ include RenderERBUtils
+ tests ActionView::Helpers::BenchmarkHelper
+
+ def test_output_in_erb
+ output = render_erb("Hello <%= benchmark do %>world<% end %>")
+ expected = 'Hello world'
+ assert_equal expected, output
+ end
+
+ def test_returns_value_from_block
+ assert_equal 'test', benchmark { 'test' }
+ end
+
+ def test_default_message
+ log = StringIO.new
+ self.stubs(:logger).returns(Logger.new(log))
+ benchmark {}
+ assert_match(log.rewind && log.read, /Benchmarking \(\d+.\d+ms\)/)
+ end
+end
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 0755379a74..89f6eccbd2 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -210,7 +210,7 @@ module ActiveRecord
@attributes = cloned_attributes
- _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
+ _run_initialize_callbacks if _initialize_callbacks.any?
@changed_attributes = {}
self.class.column_defaults.each do |attr, orig_value|
diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb
index 0236f9b0a1..9705a11387 100644
--- a/activerecord/test/cases/dup_test.rb
+++ b/activerecord/test/cases/dup_test.rb
@@ -99,5 +99,13 @@ module ActiveRecord
assert_not_nil new_topic.created_at
end
+ def test_dup_after_initialize_callbacks
+ topic = Topic.new
+ assert Topic.after_initialize_called
+ Topic.after_initialize_called = false
+ topic.dup
+ assert Topic.after_initialize_called
+ end
+
end
end
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index 1a1a18166a..8bcb9df8a8 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -80,6 +80,11 @@ class Topic < ActiveRecord::Base
after_initialize :set_email_address
+ class_attribute :after_initialize_called
+ after_initialize do
+ self.class.after_initialize_called = true
+ end
+
def approved=(val)
@custom_approved = val
write_attribute(:approved, val)