From 19c763f7831e08606e6b4fa516f5ad3b00c6428f Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Mon, 28 Feb 2011 15:47:03 -0800 Subject: reduce two method calls per request, 1 to method_missing and one to send --- railties/lib/rails/commands/server.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index c3927b6613..e447209242 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -42,6 +42,10 @@ module Rails set_environment end + def app + @app ||= super.instance + end + def opt_parser Options.new end -- cgit v1.2.3 From 54fdd33f33e6d42cf034a19cf64c6c52426de872 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Mon, 28 Feb 2011 16:30:14 -0800 Subject: use a subclass of AS::TZ for testing html output --- actionpack/test/template/date_helper_test.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index b4eb3f76e1..aca2fef170 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1882,10 +1882,17 @@ class DateHelperTest < ActionView::TestCase end def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set - time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) - time_zone = mock() - time_zone.expects(:now).returns time - Time.zone = time_zone + # The love zone is UTC+0 + mytz = Class.new(ActiveSupport::TimeZone) { + attr_accessor :now + }.create('tenderlove', 0) + + now = Time.mktime(2004, 6, 15, 16, 35, 0) + mytz.now = now + Time.zone = mytz + + assert_equal mytz, Time.zone + @post = Post.new expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n} -- cgit v1.2.3 From 2dbb73bdda3b81947fd112486ac4285fb1a6e3a9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Mon, 28 Feb 2011 17:54:00 -0800 Subject: compute ext in initialize, and use an attr_reader --- actionpack/lib/action_dispatch/middleware/static.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 913b899e20..40c85d420a 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -2,11 +2,16 @@ require 'rack/utils' module ActionDispatch class FileHandler + attr_reader :ext + def initialize(at, root) @at, @root = at.chomp('/'), root.chomp('/') @compiled_at = (Regexp.compile(/^#{Regexp.escape(at)}/) unless @at.blank?) @compiled_root = Regexp.compile(/^#{Regexp.escape(root)}/) @file_server = ::Rack::File.new(@root) + + ext = ::ActionController::Base.page_cache_extension + @ext = "{,#{ext},/index#{ext}}" end def match?(path) @@ -27,13 +32,6 @@ module ActionDispatch def call(env) @file_server.call(env) end - - def ext - @ext ||= begin - ext = ::ActionController::Base.page_cache_extension - "{,#{ext},/index#{ext}}" - end - end end class Static -- cgit v1.2.3 From f279422e3346314ebec6b2156534c0fa5ebd5a98 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Mon, 28 Feb 2011 18:03:06 -0800 Subject: no need to pass a regex to Regexp.compile --- actionpack/lib/action_dispatch/middleware/static.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 40c85d420a..c5598f3e56 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -5,10 +5,10 @@ module ActionDispatch attr_reader :ext def initialize(at, root) - @at, @root = at.chomp('/'), root.chomp('/') - @compiled_at = (Regexp.compile(/^#{Regexp.escape(at)}/) unless @at.blank?) - @compiled_root = Regexp.compile(/^#{Regexp.escape(root)}/) - @file_server = ::Rack::File.new(@root) + @at, @root = at.chomp('/'), root.chomp('/') + @compiled_at = /^#{Regexp.escape(at)}/ unless @at.blank? + @compiled_root = /^#{Regexp.escape(root)}/ + @file_server = ::Rack::File.new(@root) ext = ::ActionController::Base.page_cache_extension @ext = "{,#{ext},/index#{ext}}" -- cgit v1.2.3 From 46bb4242874fe716268e3bb7b1ce3bb3c2d883e4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Mon, 28 Feb 2011 19:47:09 -0800 Subject: Revert "compute ext in initialize, and use an attr_reader" This reverts commit 2dbb73bdda3b81947fd112486ac4285fb1a6e3a9. Conflicts: actionpack/lib/action_dispatch/middleware/static.rb --- actionpack/lib/action_dispatch/middleware/static.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index c5598f3e56..5076567bd4 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -2,16 +2,11 @@ require 'rack/utils' module ActionDispatch class FileHandler - attr_reader :ext - def initialize(at, root) @at, @root = at.chomp('/'), root.chomp('/') @compiled_at = /^#{Regexp.escape(at)}/ unless @at.blank? @compiled_root = /^#{Regexp.escape(root)}/ @file_server = ::Rack::File.new(@root) - - ext = ::ActionController::Base.page_cache_extension - @ext = "{,#{ext},/index#{ext}}" end def match?(path) @@ -32,6 +27,13 @@ module ActionDispatch def call(env) @file_server.call(env) end + + def ext + @ext ||= begin + ext = ::ActionController::Base.page_cache_extension + "{,#{ext},/index#{ext}}" + end + end end class Static -- cgit v1.2.3 From 9cf3b1f6719e1fe917f5abe56be6fee157bc3c13 Mon Sep 17 00:00:00 2001 From: Sam Elliott <sam@lenary.co.uk> Date: Mon, 28 Feb 2011 22:17:38 +0000 Subject: Remove warnings about redefined test methods [#6490 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com> --- activesupport/test/inflector_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index f55116dfab..1670d9ee7d 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -51,21 +51,21 @@ class InflectorTest < Test::Unit::TestCase end SingularToPlural.each do |singular, plural| - define_method "test_pluralize_#{singular}" do + define_method "test_pluralize_singular_#{singular}" do assert_equal(plural, ActiveSupport::Inflector.pluralize(singular)) assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize)) end end SingularToPlural.each do |singular, plural| - define_method "test_singularize_#{plural}" do + define_method "test_singularize_plural_#{plural}" do assert_equal(singular, ActiveSupport::Inflector.singularize(plural)) assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize)) end end - + SingularToPlural.each do |singular, plural| - define_method "test_pluralize_#{plural}" do + define_method "test_pluralize_plural_#{plural}" do assert_equal(plural, ActiveSupport::Inflector.pluralize(plural)) assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize)) end -- cgit v1.2.3 From e477fc1147b4bfe83a26d4ce818167a1b561b8fc Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 10:11:32 -0800 Subject: fixing test case test on 1.9.3dev --- activesupport/test/test_case_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 7e65c63062..756d21b3e4 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -12,6 +12,10 @@ module ActiveSupport def puke(klass, name, e) @puked << [klass, name, e] end + + def options + nil + end end if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions -- cgit v1.2.3 From 648fd60ecf72e77c6326aac63244d9305611ed0c Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 10:14:01 -0800 Subject: prefer composition over inheritance with AD::MS --- actionpack/CHANGELOG | 4 ++ actionpack/lib/action_controller/metal.rb | 2 +- actionpack/lib/action_dispatch/middleware/stack.rb | 45 ++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index ee9d30e1fb..fc3410ba6e 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,9 @@ *Rails 3.1.0 (unreleased)* +* ActionDispatch::MiddlewareStack now uses composition over inheritance. It is +no longer an array which means there may be methods missing that were not +tested. + * Add an :authenticity_token option to form_tag for custom handling or to omit the token (pass :authenticity_token => false). [Jakub Kuźma, Igor Wiedler] * HTML5 button_tag helper. [Rizwan Reza] diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index b2c8053584..e5db31061b 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -36,7 +36,7 @@ module ActionController action = action.to_s raise "MiddlewareStack#build requires an app" unless app - reverse.inject(app) do |a, middleware| + middlewares.reverse.inject(app) do |a, middleware| middleware.valid?(action) ? middleware.build(a) : a end diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index e3cd779756..852f697d91 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -2,7 +2,7 @@ require "active_support/inflector/methods" require "active_support/dependencies" module ActionDispatch - class MiddlewareStack < Array + class MiddlewareStack class Middleware attr_reader :args, :block @@ -41,18 +41,43 @@ module ActionDispatch end end - # Use this instead of super to work around a warning. - alias :array_initialize :initialize + include Enumerable + + attr_accessor :middlewares def initialize(*args) - array_initialize(*args) + @middlewares = [] yield(self) if block_given? end + def each + @middlewares.each { |x| yield x } + end + + def size + middlewares.size + end + + def last + middlewares.last + end + + def [](i) + middlewares[i] + end + + def include?(item) + middlewares.include? item + end + + def initialize_copy(other) + self.middlewares = other.middlewares.dup + end + def insert(index, *args, &block) index = assert_index(index, :before) middleware = self.class::Middleware.new(*args, &block) - super(index, middleware) + middlewares.insert(index, middleware) end alias_method :insert_before, :insert @@ -67,21 +92,25 @@ module ActionDispatch delete(target) end + def delete(target) + middlewares.delete target + end + def use(*args, &block) middleware = self.class::Middleware.new(*args, &block) - push(middleware) + middlewares.push(middleware) end def build(app = nil, &block) app ||= block raise "MiddlewareStack#build requires an app" unless app - reverse.inject(app) { |a, e| e.build(a) } + middlewares.reverse.inject(app) { |a, e| e.build(a) } end protected def assert_index(index, where) - i = index.is_a?(Integer) ? index : self.index(index) + i = index.is_a?(Integer) ? index : middlewares.index(index) raise "No such middleware to insert #{where}: #{index.inspect}" unless i i end -- cgit v1.2.3 From fd26afc93be89df62584f6b28bd26ec9eea7ff00 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 10:21:44 -0800 Subject: Enumerable gives us include?, so remove include? --- actionpack/lib/action_dispatch/middleware/stack.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index 852f697d91..a5f651297a 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -66,10 +66,6 @@ module ActionDispatch middlewares[i] end - def include?(item) - middlewares.include? item - end - def initialize_copy(other) self.middlewares = other.middlewares.dup end -- cgit v1.2.3 From 24faddd60c5fd148c8264898aeca2d5f36b25a4b Mon Sep 17 00:00:00 2001 From: Alexander Uvarov <alexander.uvarov@gmail.com> Date: Sun, 13 Feb 2011 22:27:33 +0500 Subject: Move ActiveModel::AttributeMethods#attribute_methods_generated? to ActiveRecord, so it's flexible now MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#6428 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com> --- activemodel/CHANGELOG | 2 + activemodel/lib/active_model/attribute_methods.rb | 44 ++++++++++------------ activemodel/test/cases/attribute_methods_test.rb | 10 +++-- .../lib/active_record/attribute_methods.rb | 11 ++++++ 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index 9dd5e03685..3082c7186a 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -2,6 +2,8 @@ * Added ActiveModel::SecurePassword to encapsulate dead-simple password usage with BCrypt encryption and salting [DHH] +* ActiveModel::AttributeMethods allows attributes to be defined on demand [Alexander Uvarov] + *Rails 3.0.2 (unreleased)* diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 8f3782eb48..2a99450a3d 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -260,30 +260,30 @@ module ActiveModel # end # end def define_attribute_methods(attr_names) - return if attribute_methods_generated? - attr_names.each do |attr_name| - attribute_method_matchers.each do |matcher| - unless instance_method_already_implemented?(matcher.method_name(attr_name)) - generate_method = "define_method_#{matcher.prefix}attribute#{matcher.suffix}" + attr_names.each { |attr_name| define_attribute_method(attr_name) } + end + + def define_attribute_method(attr_name) + attribute_method_matchers.each do |matcher| + unless instance_method_already_implemented?(matcher.method_name(attr_name)) + generate_method = "define_method_#{matcher.prefix}attribute#{matcher.suffix}" - if respond_to?(generate_method) - send(generate_method, attr_name) - else - method_name = matcher.method_name(attr_name) + if respond_to?(generate_method) + send(generate_method, attr_name) + else + method_name = matcher.method_name(attr_name) - generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 - if method_defined?('#{method_name}') - undef :'#{method_name}' - end - define_method('#{method_name}') do |*args| - send('#{matcher.method_missing_target}', '#{attr_name}', *args) - end - STR - end + generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 + if method_defined?('#{method_name}') + undef :'#{method_name}' + end + define_method('#{method_name}') do |*args| + send('#{matcher.method_missing_target}', '#{attr_name}', *args) + end + STR end end end - @attribute_methods_generated = true end # Removes all the previously dynamically defined methods from the class @@ -291,7 +291,6 @@ module ActiveModel generated_attribute_methods.module_eval do instance_methods.each { |m| undef_method(m) } end - @attribute_methods_generated = nil end # Returns true if the attribute methods defined have been generated. @@ -303,11 +302,6 @@ module ActiveModel end end - # Returns true if the attribute methods defined have been generated. - def attribute_methods_generated? - @attribute_methods_generated ||= nil - end - protected def instance_method_already_implemented?(method_name) method_defined?(method_name) diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index 422aa25668..b001adb35a 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -42,10 +42,16 @@ class AttributeMethodsTest < ActiveModel::TestCase ModelWithAttributes2.send(:attribute_method_matchers) end + test '#define_attribute_method generates attribute method' do + ModelWithAttributes.define_attribute_method(:foo) + + assert_respond_to ModelWithAttributes.new, :foo + assert_equal "value of foo", ModelWithAttributes.new.foo + end + test '#define_attribute_methods generates attribute methods' do ModelWithAttributes.define_attribute_methods([:foo]) - assert ModelWithAttributes.attribute_methods_generated? assert_respond_to ModelWithAttributes.new, :foo assert_equal "value of foo", ModelWithAttributes.new.foo end @@ -53,7 +59,6 @@ class AttributeMethodsTest < ActiveModel::TestCase test '#define_attribute_methods generates attribute methods with spaces in their names' do ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar']) - assert ModelWithAttributesWithSpaces.attribute_methods_generated? assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar' assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar') end @@ -69,7 +74,6 @@ class AttributeMethodsTest < ActiveModel::TestCase ModelWithAttributes.define_attribute_methods([:foo]) ModelWithAttributes.undefine_attribute_methods - assert !ModelWithAttributes.attribute_methods_generated? assert !ModelWithAttributes.new.respond_to?(:foo) assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 284ae2bebc..5833c65893 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -10,7 +10,18 @@ module ActiveRecord # Generates all the attribute related methods for columns in the database # accessors, mutators and query methods. def define_attribute_methods + return if attribute_methods_generated? super(column_names) + @attribute_methods_generated = true + end + + def attribute_methods_generated? + @attribute_methods_generated ||= false + end + + def undefine_attribute_methods(*args) + super + @attribute_methods_generated = false end # Checks whether the method is defined in the model or any of its subclasses -- cgit v1.2.3 From 50ed1a25a4e496a7a67861b1a04d667bd3445bb6 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 13:28:59 -0800 Subject: initialize ivars --- actionpack/lib/action_dispatch/middleware/static.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 5076567bd4..c57f694c4d 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -4,7 +4,7 @@ module ActionDispatch class FileHandler def initialize(at, root) @at, @root = at.chomp('/'), root.chomp('/') - @compiled_at = /^#{Regexp.escape(at)}/ unless @at.blank? + @compiled_at = @at.blank? ? nil : /^#{Regexp.escape(at)}/ @compiled_root = /^#{Regexp.escape(root)}/ @file_server = ::Rack::File.new(@root) end -- cgit v1.2.3 From 7b6bfe84f332a3c99656f73cf0251bce0a16ba88 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 17:20:10 -0800 Subject: refactor Reference to a ClassCache object, fix lazy lookup in Middleware so that anonymous classes are supported --- actionpack/lib/action_dispatch/middleware/stack.rb | 13 +++- .../lib/action_dispatch/routing/route_set.rb | 5 +- .../dispatch/middleware_stack/middleware_test.rb | 50 +++++++++++++ activesupport/CHANGELOG | 6 ++ activesupport/lib/active_support/dependencies.rb | 43 +++++++++--- activesupport/test/class_cache_test.rb | 81 ++++++++++++++++++++++ activesupport/test/dependencies_test.rb | 6 +- 7 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 actionpack/test/dispatch/middleware_stack/middleware_test.rb create mode 100644 activesupport/test/class_cache_test.rb diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index a5f651297a..fe87abb4d0 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -7,12 +7,19 @@ module ActionDispatch attr_reader :args, :block def initialize(klass_or_name, *args, &block) - @ref = ActiveSupport::Dependencies::Reference.new(klass_or_name) + @klass = nil + @name = klass_or_name + + if klass_or_name.respond_to?(:name) + @klass = klass_or_name + @name = @klass.name + end + @args, @block = args, block end def klass - @ref.get + @klass ||= ActiveSupport::Inflector.constantize(@name) end def ==(middleware) @@ -22,7 +29,7 @@ module ActionDispatch when Class klass == middleware else - normalize(@ref.name) == normalize(middleware) + normalize(@name) == normalize(middleware) end end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 4b4e9da173..2f764f88dc 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -50,12 +50,13 @@ module ActionDispatch private def controller_reference(controller_param) + controller_name = "#{controller_param.camelize}Controller" + unless controller = @controllers[controller_param] - controller_name = "#{controller_param.camelize}Controller" controller = @controllers[controller_param] = ActiveSupport::Dependencies.ref(controller_name) end - controller.get + controller.get(controller_name) end def dispatch(controller, action, env) diff --git a/actionpack/test/dispatch/middleware_stack/middleware_test.rb b/actionpack/test/dispatch/middleware_stack/middleware_test.rb new file mode 100644 index 0000000000..aedd5feb38 --- /dev/null +++ b/actionpack/test/dispatch/middleware_stack/middleware_test.rb @@ -0,0 +1,50 @@ +require 'abstract_unit' +require 'action_dispatch/middleware/stack' + +module ActionDispatch + class MiddlewareStack + class MiddlewareTest < ActiveSupport::TestCase + class Omg; end + + { + 'concrete' => Omg, + 'anonymous' => Class.new + }.each do |name, klass| + + define_method("test_#{name}_klass") do + mw = Middleware.new klass + assert_equal klass, mw.klass + end + + define_method("test_#{name}_==") do + mw1 = Middleware.new klass + mw2 = Middleware.new klass + assert_equal mw1, mw2 + end + + end + + def test_string_class + mw = Middleware.new Omg.name + assert_equal Omg, mw.klass + end + + def test_double_equal_works_with_classes + k = Class.new + mw = Middleware.new k + assert_operator mw, :==, k + assert_operator mw, :!=, Class.new + end + + def test_double_equal_works_with_strings + mw = Middleware.new Omg + assert_operator mw, :==, Omg.name + end + + def test_double_equal_normalizes_strings + mw = Middleware.new Omg + assert_operator mw, :==, "::#{Omg.name}" + end + end + end +end diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 1b8bcf649c..99cea2586b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,11 @@ *Rails 3.1.0 (unreleased)* +* ActiveSupport::Dependencies::ClassCache class has been introduced for +holding references to reloadable classes. + +* ActiveSupport::Dependencies::Reference has been refactored to take direct +advantage of the new ClassCache. + * Backports Range#cover? as an alias for Range#include? in Ruby 1.8 [Diego Carrion, fxn] * Added weeks_ago and prev_week to Date/DateTime/Time. [Rob Zolkos, fxn] diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index dab6fdbac6..94a8608aeb 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -524,31 +524,52 @@ module ActiveSupport #:nodoc: explicitly_unloadable_constants.each { |const| remove_constant const } end - class Reference - @@constants = Hash.new { |h, k| h[k] = Inflector.constantize(k) } + class ClassCache + def initialize + @store = Hash.new { |h, k| h[k] = Inflector.constantize(k) } + end + + def empty? + @store.empty? + end + + def key?(key) + @store.key?(key) + end - attr_reader :name + def []=(key, value) + return unless key.respond_to?(:name) - def initialize(name) - @name = name.to_s - @@constants[@name] = name if name.respond_to?(:name) + raise(ArgumentError, 'anonymous classes cannot be cached') unless key.name + + @store[key.name] = value + end + + def [](key) + key = key.name if key.respond_to?(:name) + + @store[key] end + alias :get :[] - def get - @@constants[@name] + def new(name) + self[name] = name + self end - def self.clear! - @@constants.clear + def clear! + @store.clear end end + Reference = ClassCache.new + def ref(name) references[name] ||= Reference.new(name) end def constantize(name) - ref(name).get + ref(name).get(name) end # Determine if the given constant has been automatically loaded. diff --git a/activesupport/test/class_cache_test.rb b/activesupport/test/class_cache_test.rb new file mode 100644 index 0000000000..3d3ae559e5 --- /dev/null +++ b/activesupport/test/class_cache_test.rb @@ -0,0 +1,81 @@ +require 'abstract_unit' +require 'active_support/dependencies' + +module ActiveSupport + module Dependencies + class ClassCacheTest < ActiveSupport::TestCase + def setup + @cache = ClassCache.new + end + + def test_empty? + assert @cache.empty? + @cache[ClassCacheTest] = ClassCacheTest + assert !@cache.empty? + end + + def test_clear! + assert @cache.empty? + @cache[ClassCacheTest] = ClassCacheTest + assert !@cache.empty? + @cache.clear! + assert @cache.empty? + end + + def test_set_key + @cache[ClassCacheTest] = ClassCacheTest + assert @cache.key?(ClassCacheTest.name) + end + + def test_set_rejects_strings + @cache[ClassCacheTest.name] = ClassCacheTest + assert @cache.empty? + end + + def test_get_with_class + @cache[ClassCacheTest] = ClassCacheTest + assert_equal ClassCacheTest, @cache[ClassCacheTest] + end + + def test_get_with_name + @cache[ClassCacheTest] = ClassCacheTest + assert_equal ClassCacheTest, @cache[ClassCacheTest.name] + end + + def test_get_constantizes + assert @cache.empty? + assert_equal ClassCacheTest, @cache[ClassCacheTest.name] + end + + def test_get_is_an_alias + assert_equal @cache[ClassCacheTest], @cache.get(ClassCacheTest.name) + end + + def test_new + @cache.new ClassCacheTest + assert @cache.key?(ClassCacheTest.name) + end + + def test_new_rejects_strings + @cache.new ClassCacheTest.name + assert !@cache.key?(ClassCacheTest.name) + end + + def test_new_returns_self + v = @cache.new ClassCacheTest.name + assert_equal @cache, v + end + + def test_anonymous_class_fail + assert_raises(ArgumentError) do + @cache.new Class.new + end + + assert_raises(ArgumentError) do + x = Class.new + @cache[x] = x + end + end + end + end +end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index bc7f597f1d..b3ada53497 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -479,13 +479,13 @@ class DependenciesTest < Test::Unit::TestCase with_loading 'dependencies' do c = ActiveSupport::Dependencies.ref("ServiceOne") service_one_first = ServiceOne - assert_equal service_one_first, c.get + assert_equal service_one_first, c.get("ServiceOne") ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) service_one_second = ServiceOne - assert_not_equal service_one_first, c.get - assert_equal service_one_second, c.get + assert_not_equal service_one_first, c.get("ServiceOne") + assert_equal service_one_second, c.get("ServiceOne") end end -- cgit v1.2.3 From f345e2380cac2560f3bbd80defe1ef485e0d564e Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Tue, 1 Mar 2011 17:43:45 -0800 Subject: yo dawg, directly use the class cache rather than the cache of the cache --- actionpack/lib/action_dispatch/middleware/stack.rb | 3 ++- activesupport/lib/active_support/dependencies.rb | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index fe87abb4d0..d9bfd3d573 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -8,11 +8,12 @@ module ActionDispatch def initialize(klass_or_name, *args, &block) @klass = nil - @name = klass_or_name if klass_or_name.respond_to?(:name) @klass = klass_or_name @name = @klass.name + else + @name = klass_or_name.to_s end @args, @block = args, block diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 94a8608aeb..d5c51ca417 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -47,9 +47,6 @@ module ActiveSupport #:nodoc: mattr_accessor :autoloaded_constants self.autoloaded_constants = [] - mattr_accessor :references - self.references = {} - # An array of constant names that need to be unloaded on every request. Used # to allow arbitrary constants to be marked for unloading. mattr_accessor :explicitly_unloadable_constants @@ -565,11 +562,11 @@ module ActiveSupport #:nodoc: Reference = ClassCache.new def ref(name) - references[name] ||= Reference.new(name) + Reference.new(name) end def constantize(name) - ref(name).get(name) + Reference.get(name) end # Determine if the given constant has been automatically loaded. -- cgit v1.2.3 From e4cf28f3a0491620787d8e06d3a47918c5accc9b Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic <dalibor.nasevic@gmail.com> Date: Wed, 2 Mar 2011 01:18:32 +0100 Subject: Fixed typos in asset_host_test [#6501 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com> --- actionmailer/test/asset_host_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionmailer/test/asset_host_test.rb b/actionmailer/test/asset_host_test.rb index 069860ff06..b24eca5fbb 100644 --- a/actionmailer/test/asset_host_test.rb +++ b/actionmailer/test/asset_host_test.rb @@ -29,7 +29,7 @@ class AssetHostTest < Test::Unit::TestCase assert_equal %Q{<img alt="Somelogo" src="http://www.example.com/images/somelogo.png" />}, mail.body.to_s.strip end - def test_asset_host_as_one_arguement_proc + def test_asset_host_as_one_argument_proc AssetHostMailer.config.asset_host = Proc.new { |source| if source.starts_with?('/images') "http://images.example.com" @@ -41,7 +41,7 @@ class AssetHostTest < Test::Unit::TestCase assert_equal %Q{<img alt="Somelogo" src="http://images.example.com/images/somelogo.png" />}, mail.body.to_s.strip end - def test_asset_host_as_two_arguement_proc + def test_asset_host_as_two_argument_proc ActionController::Base.config.asset_host = Proc.new {|source,request| if request && request.ssl? "https://www.example.com" -- cgit v1.2.3 From 2ee55557440a644453482a678e4ff08a4b5ebd3e Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow <chuyeow@gmail.com> Date: Sun, 27 Feb 2011 22:57:46 +0800 Subject: Fix Action caching bug where an action that has a non-cacheable response always renders a nil response body. It now correctly renders the response body. Note that only GET and HTTP 200 responses can be cached. [#6480 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com> --- actionpack/lib/action_controller/caching/actions.rb | 8 +++++--- actionpack/test/controller/caching_test.rb | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index a4bac3caed..a1c582560c 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -103,12 +103,14 @@ module ActionController #:nodoc: end def _save_fragment(name, options) - return unless caching_allowed? - content = response_body content = content.join if content.is_a?(Array) - write_fragment(name, content, options) + if caching_allowed? + write_fragment(name, content, options) + else + content + end end protected diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index cc393d3ef4..01f3e8f2b6 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -559,6 +559,11 @@ class ActionCacheTest < ActionController::TestCase assert_response 404 end + def test_four_oh_four_renders_content + get :four_oh_four + assert_equal "404'd!", @response.body + end + def test_simple_runtime_error_returns_500_for_multiple_requests get :simple_runtime_error assert_response 500 -- cgit v1.2.3 From 66245441d4471f29043433f6cf789498cf35b96c Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 09:18:05 -0800 Subject: adding backwards compat for class cache references. <3<3 --- activesupport/lib/active_support/dependencies.rb | 13 ++++++++++++- activesupport/test/class_cache_test.rb | 14 ++++++++++---- activesupport/test/dependencies_test.rb | 6 +++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index d5c51ca417..97e20bdaf4 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -549,10 +549,21 @@ module ActiveSupport #:nodoc: end alias :get :[] + class Getter # :nodoc: + def initialize(name) + @name = name + end + + def get + Reference.get @name + end + end + def new(name) self[name] = name - self + Getter.new(name) end + deprecate :new def clear! @store.clear diff --git a/activesupport/test/class_cache_test.rb b/activesupport/test/class_cache_test.rb index 3d3ae559e5..4d19e9841a 100644 --- a/activesupport/test/class_cache_test.rb +++ b/activesupport/test/class_cache_test.rb @@ -52,7 +52,9 @@ module ActiveSupport end def test_new - @cache.new ClassCacheTest + assert_deprecated do + @cache.new ClassCacheTest + end assert @cache.key?(ClassCacheTest.name) end @@ -61,9 +63,13 @@ module ActiveSupport assert !@cache.key?(ClassCacheTest.name) end - def test_new_returns_self - v = @cache.new ClassCacheTest.name - assert_equal @cache, v + def test_new_returns_proxy + v = nil + assert_deprecated do + v = @cache.new ClassCacheTest.name + end + + assert_equal ClassCacheTest, v.get end def test_anonymous_class_fail diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index b3ada53497..bc7f597f1d 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -479,13 +479,13 @@ class DependenciesTest < Test::Unit::TestCase with_loading 'dependencies' do c = ActiveSupport::Dependencies.ref("ServiceOne") service_one_first = ServiceOne - assert_equal service_one_first, c.get("ServiceOne") + assert_equal service_one_first, c.get ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) service_one_second = ServiceOne - assert_not_equal service_one_first, c.get("ServiceOne") - assert_equal service_one_second, c.get("ServiceOne") + assert_not_equal service_one_first, c.get + assert_equal service_one_second, c.get end end -- cgit v1.2.3 From 69e348013bc3ce4289c5fefc29c1aacc47048c0d Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 09:31:40 -0800 Subject: adding deprecation noticies to deprecated class cache methods --- activesupport/lib/active_support/dependencies.rb | 13 ++++++++++++ activesupport/test/class_cache_test.rb | 27 +++++++++++++++++++++--- activesupport/test/dependencies_test.rb | 8 +++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 97e20bdaf4..4abeb74c45 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -557,6 +557,7 @@ module ActiveSupport #:nodoc: def get Reference.get @name end + deprecate :get end def new(name) @@ -565,6 +566,11 @@ module ActiveSupport #:nodoc: end deprecate :new + def store(name) + self[name] = name + self + end + def clear! @store.clear end @@ -575,7 +581,14 @@ module ActiveSupport #:nodoc: def ref(name) Reference.new(name) end + deprecate :ref + + # Store a reference to a class +klass+. + def reference(klass) + Reference.store klass + end + # Get the reference for class named +name+. def constantize(name) Reference.get(name) end diff --git a/activesupport/test/class_cache_test.rb b/activesupport/test/class_cache_test.rb index 4d19e9841a..8445af8d25 100644 --- a/activesupport/test/class_cache_test.rb +++ b/activesupport/test/class_cache_test.rb @@ -59,28 +59,49 @@ module ActiveSupport end def test_new_rejects_strings - @cache.new ClassCacheTest.name + assert_deprecated do + @cache.new ClassCacheTest.name + end assert !@cache.key?(ClassCacheTest.name) end + def test_new_rejects_strings + @cache.store ClassCacheTest.name + assert !@cache.key?(ClassCacheTest.name) + end + + def test_store_returns_self + x = @cache.store ClassCacheTest + assert_equal @cache, x + end + def test_new_returns_proxy v = nil assert_deprecated do v = @cache.new ClassCacheTest.name end - assert_equal ClassCacheTest, v.get + assert_deprecated do + assert_equal ClassCacheTest, v.get + end end def test_anonymous_class_fail assert_raises(ArgumentError) do - @cache.new Class.new + assert_deprecated do + @cache.new Class.new + end end assert_raises(ArgumentError) do x = Class.new @cache[x] = x end + + assert_raises(ArgumentError) do + x = Class.new + @cache.store x + end end end end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index bc7f597f1d..ef017d7436 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -477,15 +477,15 @@ class DependenciesTest < Test::Unit::TestCase def test_references_should_work with_loading 'dependencies' do - c = ActiveSupport::Dependencies.ref("ServiceOne") + c = ActiveSupport::Dependencies.reference("ServiceOne") service_one_first = ServiceOne - assert_equal service_one_first, c.get + assert_equal service_one_first, c.get("ServiceOne") ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) service_one_second = ServiceOne - assert_not_equal service_one_first, c.get - assert_equal service_one_second, c.get + assert_not_equal service_one_first, c.get("ServiceOne") + assert_equal service_one_second, c.get("ServiceOne") end end -- cgit v1.2.3 From dd41387f343e813c40dc997135207f4ac81c58ab Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 09:35:10 -0800 Subject: use newer class cache api --- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 2f764f88dc..fc86d52a3a 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -54,7 +54,7 @@ module ActionDispatch unless controller = @controllers[controller_param] controller = @controllers[controller_param] = - ActiveSupport::Dependencies.ref(controller_name) + ActiveSupport::Dependencies.reference(controller_name) end controller.get(controller_name) end -- cgit v1.2.3 From 91983724211b0e654c9e79e73bacf10a65adf172 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 09:43:27 -0800 Subject: Ruby 1.8: Y U NO FUN? --- actionpack/test/dispatch/middleware_stack/middleware_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/actionpack/test/dispatch/middleware_stack/middleware_test.rb b/actionpack/test/dispatch/middleware_stack/middleware_test.rb index aedd5feb38..4e16fdbc77 100644 --- a/actionpack/test/dispatch/middleware_stack/middleware_test.rb +++ b/actionpack/test/dispatch/middleware_stack/middleware_test.rb @@ -33,7 +33,9 @@ module ActionDispatch k = Class.new mw = Middleware.new k assert_operator mw, :==, k - assert_operator mw, :!=, Class.new + + result = mw != Class.new + assert result, 'middleware should not equal other anon class' end def test_double_equal_works_with_strings -- cgit v1.2.3 From 1f2e7214aaed01f50be12017f0ffbf9fcdbdbb5e Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 10:11:28 -0800 Subject: make sure string keys are always looked up from the class cache --- actionpack/lib/action_dispatch/middleware/stack.rb | 5 +++-- .../dispatch/middleware_stack/middleware_test.rb | 25 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index d9bfd3d573..a4308f528c 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -4,7 +4,7 @@ require "active_support/dependencies" module ActionDispatch class MiddlewareStack class Middleware - attr_reader :args, :block + attr_reader :args, :block, :name, :classcache def initialize(klass_or_name, *args, &block) @klass = nil @@ -16,11 +16,12 @@ module ActionDispatch @name = klass_or_name.to_s end + @classcache = ActiveSupport::Dependencies::Reference @args, @block = args, block end def klass - @klass ||= ActiveSupport::Inflector.constantize(@name) + @klass || classcache[@name] end def ==(middleware) diff --git a/actionpack/test/dispatch/middleware_stack/middleware_test.rb b/actionpack/test/dispatch/middleware_stack/middleware_test.rb index 4e16fdbc77..9607f026db 100644 --- a/actionpack/test/dispatch/middleware_stack/middleware_test.rb +++ b/actionpack/test/dispatch/middleware_stack/middleware_test.rb @@ -47,6 +47,31 @@ module ActionDispatch mw = Middleware.new Omg assert_operator mw, :==, "::#{Omg.name}" end + + def test_middleware_loads_classnames_from_cache + mw = Class.new(Middleware) { + attr_accessor :classcache + }.new(Omg.name) + + fake_cache = { mw.name => Omg } + mw.classcache = fake_cache + + assert_equal Omg, mw.klass + + fake_cache[mw.name] = Middleware + assert_equal Middleware, mw.klass + end + + def test_middleware_always_returns_class + mw = Class.new(Middleware) { + attr_accessor :classcache + }.new(Omg) + + fake_cache = { mw.name => Middleware } + mw.classcache = fake_cache + + assert_equal Omg, mw.klass + end end end end -- cgit v1.2.3 From 72405efe64c7f2d4fc759744b8facbd781f56165 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 10:23:04 -0800 Subject: anonymous classes have blank names on ruby 1.8 --- activesupport/lib/active_support/dependencies.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 4abeb74c45..9776eb6a79 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -537,7 +537,7 @@ module ActiveSupport #:nodoc: def []=(key, value) return unless key.respond_to?(:name) - raise(ArgumentError, 'anonymous classes cannot be cached') unless key.name + raise(ArgumentError, 'anonymous classes cannot be cached') if key.name.blank? @store[key.name] = value end -- cgit v1.2.3 From 272ede9b9abeba6c987b66f047e24b472e99921d Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 10:42:43 -0800 Subject: require deprecation so that we can deprecate methods! --- activesupport/lib/active_support/dependencies.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 9776eb6a79..47596a389d 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -5,6 +5,7 @@ require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/deprecation' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/load_error' require 'active_support/core_ext/name_error' -- cgit v1.2.3 From b7cf1f4ccfaac3b50c09df484062fdc92b340876 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic <dalibor.nasevic@gmail.com> Date: Wed, 2 Mar 2011 01:55:22 +0100 Subject: No need of instance variable [#6502 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com> --- actionmailer/test/base_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 7ed9d4a5c0..1b793d255e 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -32,21 +32,21 @@ class BaseTest < ActiveSupport::TestCase end test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do - @time = Time.now.beginning_of_day.to_datetime + time = Time.now.beginning_of_day.to_datetime email = BaseMailer.welcome(:bcc => 'bcc@test.lindsaar.net', :cc => 'cc@test.lindsaar.net', :content_type => 'multipart/mixed', :charset => 'iso-8559-1', :mime_version => '2.0', :reply_to => 'reply-to@test.lindsaar.net', - :date => @time) + :date => time) assert_equal(['bcc@test.lindsaar.net'], email.bcc) assert_equal(['cc@test.lindsaar.net'], email.cc) assert_equal('multipart/mixed; charset=iso-8559-1', email.content_type) assert_equal('iso-8559-1', email.charset) assert_equal('2.0', email.mime_version) assert_equal(['reply-to@test.lindsaar.net'], email.reply_to) - assert_equal(@time, email.date) + assert_equal(time, email.date) end test "mail() renders the template using the method being processed" do -- cgit v1.2.3 From b247c8d71a54d684a1c307986a03d1759bbc20e0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 14:04:41 -0800 Subject: * LocalCache strategy is now a real middleware class, not an anonymous class posing for pictures. --- activesupport/CHANGELOG | 3 ++ .../active_support/cache/strategy/local_cache.rb | 52 ++++++++++++---------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 99cea2586b..373236ce9a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,8 @@ *Rails 3.1.0 (unreleased)* +* LocalCache strategy is now a real middleware class, not an anonymous class +posing for pictures. + * ActiveSupport::Dependencies::ClassCache class has been introduced for holding references to reloadable classes. diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 4dce35f1c9..99b26b19fe 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -50,34 +50,40 @@ module ActiveSupport end end - # Middleware class can be inserted as a Rack handler to be local cache for the - # duration of request. - def middleware - @middleware ||= begin - klass = Class.new - klass.class_eval(<<-EOS, __FILE__, __LINE__ + 1) - class << self - def name - "ActiveSupport::Cache::Strategy::LocalCache" - end - alias :to_s :name - end + #-- + # This class wraps up local storage for middlewares. Only the middleware method should + # construct them. + class Middleware # :nodoc: + attr_reader :name, :thread_local_key + alias :to_s :name - def initialize(app) - @app = app - end + def initialize(name, thread_local_key) + @name = name + @thread_local_key = thread_local_key + @app = nil + end - def call(env) - Thread.current[:#{thread_local_key}] = LocalStore.new - @app.call(env) - ensure - Thread.current[:#{thread_local_key}] = nil - end - EOS - klass + def new(app) + @app = app + self + end + + def call(env) + Thread.current[thread_local_key] = LocalStore.new + @app.call(env) + ensure + Thread.current[thread_local_key] = nil end end + # Middleware class can be inserted as a Rack handler to be local cache for the + # duration of request. + def middleware + @middleware ||= Middleware.new( + "ActiveSupport::Cache::Strategy::LocalCache", + thread_local_key) + end + def clear(options = nil) # :nodoc: local_cache.clear(options) if local_cache super -- cgit v1.2.3 From dc89e29f4937a68e91cd7572f45f06c523430c23 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 15:17:13 -0800 Subject: remove to_s implementation so that inspect is helpful --- activesupport/lib/active_support/cache/strategy/local_cache.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 99b26b19fe..0649a058aa 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -55,7 +55,6 @@ module ActiveSupport # construct them. class Middleware # :nodoc: attr_reader :name, :thread_local_key - alias :to_s :name def initialize(name, thread_local_key) @name = name -- cgit v1.2.3 From 1db4969dc9cabed9db162e7194b9353d43c967d7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed, 2 Mar 2011 15:17:29 -0800 Subject: only compute path.to_s once --- railties/lib/rails/engine.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 6e5e842370..7c26234750 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -382,7 +382,10 @@ module Rails # Finds engine with given path def find(path) - Rails::Engine::Railties.engines.find { |r| File.expand_path(r.root.to_s) == File.expand_path(path.to_s) } + path = path.to_s + Rails::Engine::Railties.engines.find { |r| + File.expand_path(r.root.to_s) == File.expand_path(path) + } end end -- cgit v1.2.3