From 10085114ce7ec6bc967fa701c49ef218f666efb5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 25 May 2009 18:05:58 +0200 Subject: Make Filter#filter work with around filters --- activesupport/lib/active_support/new_callbacks.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index f8108780f1..039d0fa658 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -298,11 +298,23 @@ module ActiveSupport kind, name = @kind, @name @klass.send(:define_method, "#{method_name}_object") { filter } - @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def #{method_name}(&blk) - #{method_name}_object.send("#{kind}_#{name}", self, &blk) - end - RUBY_EVAL + if kind == :around + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + if :#{kind} == :around && #{method_name}_object.respond_to?(:filter) + #{method_name}_object.send("filter", self, &blk) + else + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end + end + RUBY_EVAL + else + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end + RUBY_EVAL + end method_name end end -- cgit v1.2.3 From 2f59066470193c6219dfd958fc5d8096a2ddee68 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 26 May 2009 13:09:33 +0200 Subject: Support Method callbacks --- activesupport/lib/active_support/new_callbacks.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index 039d0fa658..b6cbdbb6b0 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -287,6 +287,14 @@ module ActiveSupport when Proc @klass.send(:define_method, method_name, &filter) method_name << (filter.arity == 1 ? "(self)" : "") + when Method + @klass.send(:define_method, "#{method_name}_method") { filter } + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + #{method_name}_method.call(self, &blk) + end + RUBY_EVAL + method_name when String @klass.class_eval <<-RUBY_EVAL def #{method_name} -- cgit v1.2.3 From 6ef329d3250cfc3a21184cc2a8248aa18a1f6815 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 27 May 2009 14:54:58 -0500 Subject: Ensure Memcache local cache returns duplicated values [#2302 state:resolved] --- activesupport/lib/active_support/cache/strategy/local_cache.rb | 2 +- activesupport/test/caching_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 4bbcd8e4c4..3b5fccc737 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -45,7 +45,7 @@ module ActiveSupport elsif value.nil? value = super local_cache.write(key, value || NULL) if local_cache - value + value.duplicable? ? value.dup : value else # forcing the value to be immutable value.duplicable? ? value.dup : value diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index bd237a5c8e..51d04d9388 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -180,6 +180,15 @@ uses_memcached 'memcached backed store' do end end + def test_stored_objects_should_not_be_frozen + @cache.with_local_cache do + @cache.write('foo', 'bar') + end + @cache.with_local_cache do + assert !@cache.read('foo').frozen? + end + end + def test_write_should_return_true_on_success @cache.with_local_cache do result = @cache.write('foo', 'bar') -- cgit v1.2.3 From a5688fa9073dc8824d98071346e6cd9ae417eb72 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 27 May 2009 11:02:13 -0700 Subject: Add the ability to prepend filters to new callbacks --- activesupport/lib/active_support/new_callbacks.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index b6cbdbb6b0..e9a30b9c35 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -464,7 +464,9 @@ module ActiveSupport self._#{symbol}_callbacks.delete_if {|c| c.matches?(type, :#{symbol}, filter)} Callback.new(filter, type, options.dup, self, :#{symbol}) end - self._#{symbol}_callbacks.push(*filters) + options[:prepend] ? + self._#{symbol}_callbacks.unshift(*filters) : + self._#{symbol}_callbacks.push(*filters) _define_runner(:#{symbol}, self._#{symbol}_callbacks.compile(nil, :terminator => _#{symbol}_terminator), options) -- cgit v1.2.3 From 2b166d306c7829c8be531cb030515166367cbb20 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 27 May 2009 16:49:23 -0700 Subject: Added support to new callbacks for around filter object that respond to :before & :after --- activesupport/lib/active_support/new_callbacks.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index e9a30b9c35..5ca6b90b01 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -311,6 +311,11 @@ module ActiveSupport def #{method_name}(&blk) if :#{kind} == :around && #{method_name}_object.respond_to?(:filter) #{method_name}_object.send("filter", self, &blk) + # TODO: Deprecate this + elsif #{method_name}_object.respond_to?(:before) && #{method_name}_object.respond_to?(:after) + #{method_name}_object.before(self) + yield + #{method_name}_object.after(self) else #{method_name}_object.send("#{kind}_#{name}", self, &blk) end -- cgit v1.2.3 From 1e2628431a742e4f67af1d5dc1db8ad6393e8832 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 27 May 2009 16:50:40 -0700 Subject: Added support to new callbacks for around filter object that respond to :before & :after used in before|after callbacks --- activesupport/lib/active_support/new_callbacks.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index 5ca6b90b01..185fb6a6af 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -324,7 +324,11 @@ module ActiveSupport else @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{method_name}(&blk) - #{method_name}_object.send("#{kind}_#{name}", self, &blk) + if #{method_name}_object.respond_to?(:#{kind}) + #{method_name}_object.#{kind}(self, &blk) + else + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end end RUBY_EVAL end -- cgit v1.2.3 From f2f58f535520bfa77ab2d0ad1fb598f18349dd2e Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 27 May 2009 17:31:48 -0700 Subject: Fix failing ActionCacheFilter tests due to around filter changes. --- activesupport/lib/active_support/new_callbacks.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index 185fb6a6af..f1d2ea7bb1 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -313,8 +313,8 @@ module ActiveSupport #{method_name}_object.send("filter", self, &blk) # TODO: Deprecate this elsif #{method_name}_object.respond_to?(:before) && #{method_name}_object.respond_to?(:after) - #{method_name}_object.before(self) - yield + should_continue = #{method_name}_object.before(self) + yield if should_continue #{method_name}_object.after(self) else #{method_name}_object.send("#{kind}_#{name}", self, &blk) -- cgit v1.2.3 From 4e50a35fa243f6cf7ad567774a9f7c1cb87a1653 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 28 May 2009 11:35:36 -0500 Subject: Break up DependencyModule's dual function of providing a "depend_on" DSL and "included" block DSL into separate modules. But, unify both approaches under AS::Concern. --- activesupport/lib/active_support/autoload.rb | 3 +- activesupport/lib/active_support/concern.rb | 22 ++++++ .../lib/active_support/dependency_module.rb | 12 +-- activesupport/test/concern_test.rb | 88 ++++++++++++++++++++++ activesupport/test/dependency_module_test.rb | 88 ---------------------- 5 files changed, 113 insertions(+), 100 deletions(-) create mode 100644 activesupport/lib/active_support/concern.rb create mode 100644 activesupport/test/concern_test.rb delete mode 100644 activesupport/test/dependency_module_test.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/autoload.rb b/activesupport/lib/active_support/autoload.rb index ed229d1c5f..75706855d6 100644 --- a/activesupport/lib/active_support/autoload.rb +++ b/activesupport/lib/active_support/autoload.rb @@ -5,7 +5,7 @@ module ActiveSupport autoload :BufferedLogger, 'active_support/buffered_logger' autoload :Cache, 'active_support/cache' autoload :Callbacks, 'active_support/callbacks' - autoload :NewCallbacks, 'active_support/new_callbacks' + autoload :Concern, 'active_support/concern' autoload :ConcurrentHash, 'active_support/concurrent_hash' autoload :DependencyModule, 'active_support/dependency_module' autoload :Deprecation, 'active_support/deprecation' @@ -15,6 +15,7 @@ module ActiveSupport autoload :MessageEncryptor, 'active_support/message_encryptor' autoload :MessageVerifier, 'active_support/message_verifier' autoload :Multibyte, 'active_support/multibyte' + autoload :NewCallbacks, 'active_support/new_callbacks' autoload :OptionMerger, 'active_support/option_merger' autoload :OrderedHash, 'active_support/ordered_hash' autoload :OrderedOptions, 'active_support/ordered_options' diff --git a/activesupport/lib/active_support/concern.rb b/activesupport/lib/active_support/concern.rb new file mode 100644 index 0000000000..154f8807f7 --- /dev/null +++ b/activesupport/lib/active_support/concern.rb @@ -0,0 +1,22 @@ +require 'active_support/dependency_module' + +module ActiveSupport + module Concern + include DependencyModule + + def append_features(base) + if super + base.extend const_get("ClassMethods") if const_defined?("ClassMethods") + base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block") + end + end + + def included(base = nil, &block) + if base.nil? + @_included_block = block + else + super + end + end + end +end diff --git a/activesupport/lib/active_support/dependency_module.rb b/activesupport/lib/active_support/dependency_module.rb index 9872b9654b..6847c0f86a 100644 --- a/activesupport/lib/active_support/dependency_module.rb +++ b/activesupport/lib/active_support/dependency_module.rb @@ -1,19 +1,9 @@ module ActiveSupport module DependencyModule def append_features(base) - return if base < self + return false if base < self (@_dependencies ||= []).each { |dep| base.send(:include, dep) } super - base.extend const_get("ClassMethods") if const_defined?("ClassMethods") - base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block") - end - - def included(base = nil, &block) - if base.nil? - @_included_block = block - else - super - end end def depends_on(*mods) diff --git a/activesupport/test/concern_test.rb b/activesupport/test/concern_test.rb new file mode 100644 index 0000000000..22f7ec2064 --- /dev/null +++ b/activesupport/test/concern_test.rb @@ -0,0 +1,88 @@ +require 'abstract_unit' +require 'active_support/concern' + +class ConcernTest < Test::Unit::TestCase + module Baz + extend ActiveSupport::Concern + + module ClassMethods + def baz + "baz" + end + + def included_ran=(value) + @@included_ran = value + end + + def included_ran + @@included_ran + end + end + + included do + self.included_ran = true + end + + def baz + "baz" + end + end + + module Bar + extend ActiveSupport::Concern + + depends_on Baz + + def bar + "bar" + end + + def baz + "bar+" + super + end + end + + module Foo + extend ActiveSupport::Concern + + depends_on Bar, Baz + end + + def setup + @klass = Class.new + end + + def test_module_is_included_normally + @klass.send(:include, Baz) + assert_equal "baz", @klass.new.baz + assert_equal ConcernTest::Baz, @klass.included_modules[0] + + @klass.send(:include, Baz) + assert_equal "baz", @klass.new.baz + assert_equal ConcernTest::Baz, @klass.included_modules[0] + end + + def test_class_methods_are_extended + @klass.send(:include, Baz) + assert_equal "baz", @klass.baz + assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0] + end + + def test_included_block_is_ran + @klass.send(:include, Baz) + assert_equal true, @klass.included_ran + end + + def test_modules_dependencies_are_met + @klass.send(:include, Bar) + assert_equal "bar", @klass.new.bar + assert_equal "bar+baz", @klass.new.baz + assert_equal "baz", @klass.baz + assert_equal [ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..1] + end + + def test_depends_on_with_multiple_modules + @klass.send(:include, Foo) + assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2] + end +end diff --git a/activesupport/test/dependency_module_test.rb b/activesupport/test/dependency_module_test.rb deleted file mode 100644 index be7db0fa7b..0000000000 --- a/activesupport/test/dependency_module_test.rb +++ /dev/null @@ -1,88 +0,0 @@ -require 'abstract_unit' -require 'active_support/dependency_module' - -class DependencyModuleTest < Test::Unit::TestCase - module Baz - extend ActiveSupport::DependencyModule - - module ClassMethods - def baz - "baz" - end - - def included_ran=(value) - @@included_ran = value - end - - def included_ran - @@included_ran - end - end - - included do - self.included_ran = true - end - - def baz - "baz" - end - end - - module Bar - extend ActiveSupport::DependencyModule - - depends_on Baz - - def bar - "bar" - end - - def baz - "bar+" + super - end - end - - module Foo - extend ActiveSupport::DependencyModule - - depends_on Bar, Baz - end - - def setup - @klass = Class.new - end - - def test_module_is_included_normally - @klass.send(:include, Baz) - assert_equal "baz", @klass.new.baz - assert_equal DependencyModuleTest::Baz, @klass.included_modules[0] - - @klass.send(:include, Baz) - assert_equal "baz", @klass.new.baz - assert_equal DependencyModuleTest::Baz, @klass.included_modules[0] - end - - def test_class_methods_are_extended - @klass.send(:include, Baz) - assert_equal "baz", @klass.baz - assert_equal DependencyModuleTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0] - end - - def test_included_block_is_ran - @klass.send(:include, Baz) - assert_equal true, @klass.included_ran - end - - def test_modules_dependencies_are_met - @klass.send(:include, Bar) - assert_equal "bar", @klass.new.bar - assert_equal "bar+baz", @klass.new.baz - assert_equal "baz", @klass.baz - assert_equal [DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..1] - end - - def test_depends_on_with_multiple_modules - @klass.send(:include, Foo) - assert_equal [DependencyModuleTest::Foo, DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..2] - end -end -- cgit v1.2.3 From c7c35be8fe30b3e29a5d05edae767f7d6a286911 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 29 May 2009 16:28:54 -0500 Subject: AS::Concern includes InstanceMethods module if it exists --- activesupport/lib/active_support/concern.rb | 1 + activesupport/test/concern_test.rb | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/concern.rb b/activesupport/lib/active_support/concern.rb index 154f8807f7..a09a2420b3 100644 --- a/activesupport/lib/active_support/concern.rb +++ b/activesupport/lib/active_support/concern.rb @@ -7,6 +7,7 @@ module ActiveSupport def append_features(base) if super base.extend const_get("ClassMethods") if const_defined?("ClassMethods") + base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods") base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block") end end diff --git a/activesupport/test/concern_test.rb b/activesupport/test/concern_test.rb index 22f7ec2064..40fcb7c57f 100644 --- a/activesupport/test/concern_test.rb +++ b/activesupport/test/concern_test.rb @@ -19,6 +19,9 @@ class ConcernTest < Test::Unit::TestCase end end + module InstanceMethods + end + included do self.included_ran = true end @@ -55,11 +58,11 @@ class ConcernTest < Test::Unit::TestCase def test_module_is_included_normally @klass.send(:include, Baz) assert_equal "baz", @klass.new.baz - assert_equal ConcernTest::Baz, @klass.included_modules[0] + assert @klass.included_modules.include?(ConcernTest::Baz) @klass.send(:include, Baz) assert_equal "baz", @klass.new.baz - assert_equal ConcernTest::Baz, @klass.included_modules[0] + assert @klass.included_modules.include?(ConcernTest::Baz) end def test_class_methods_are_extended @@ -68,6 +71,12 @@ class ConcernTest < Test::Unit::TestCase assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0] end + def test_instance_methods_are_included + @klass.send(:include, Baz) + assert_equal "baz", @klass.new.baz + assert @klass.included_modules.include?(ConcernTest::Baz::InstanceMethods) + end + def test_included_block_is_ran @klass.send(:include, Baz) assert_equal true, @klass.included_ran @@ -78,11 +87,11 @@ class ConcernTest < Test::Unit::TestCase assert_equal "bar", @klass.new.bar assert_equal "bar+baz", @klass.new.baz assert_equal "baz", @klass.baz - assert_equal [ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..1] + assert @klass.included_modules.include?(ConcernTest::Bar) end def test_depends_on_with_multiple_modules @klass.send(:include, Foo) - assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2] + assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz::InstanceMethods, ConcernTest::Baz], @klass.included_modules[0..3] end end -- cgit v1.2.3 From 669fd84910586d4c791b6f5bf4320f68ac7845aa Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 29 May 2009 17:03:23 -0500 Subject: AS::Concern redefines "include" to lazy include modules as dependencies --- activesupport/lib/active_support/concern.rb | 2 ++ activesupport/test/concern_test.rb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/concern.rb b/activesupport/lib/active_support/concern.rb index a09a2420b3..dcf1e8152f 100644 --- a/activesupport/lib/active_support/concern.rb +++ b/activesupport/lib/active_support/concern.rb @@ -19,5 +19,7 @@ module ActiveSupport super end end + + alias_method :include, :depends_on end end diff --git a/activesupport/test/concern_test.rb b/activesupport/test/concern_test.rb index 40fcb7c57f..4cbe56a2d2 100644 --- a/activesupport/test/concern_test.rb +++ b/activesupport/test/concern_test.rb @@ -34,7 +34,7 @@ class ConcernTest < Test::Unit::TestCase module Bar extend ActiveSupport::Concern - depends_on Baz + include Baz def bar "bar" @@ -48,7 +48,7 @@ class ConcernTest < Test::Unit::TestCase module Foo extend ActiveSupport::Concern - depends_on Bar, Baz + include Bar, Baz end def setup @@ -90,7 +90,7 @@ class ConcernTest < Test::Unit::TestCase assert @klass.included_modules.include?(ConcernTest::Bar) end - def test_depends_on_with_multiple_modules + def test_dependencies_with_multiple_modules @klass.send(:include, Foo) assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz::InstanceMethods, ConcernTest::Baz], @klass.included_modules[0..3] end -- cgit v1.2.3 From 63992e8f3af48d34d70ce33828c93625af9e8562 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 30 May 2009 15:14:23 +0100 Subject: Support Object#filter method for before/after filters --- activesupport/lib/active_support/new_callbacks.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index f1d2ea7bb1..8512c659b0 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -326,6 +326,8 @@ module ActiveSupport def #{method_name}(&blk) if #{method_name}_object.respond_to?(:#{kind}) #{method_name}_object.#{kind}(self, &blk) + elsif #{method_name}_object.respond_to?(:filter) + #{method_name}_object.send("filter", self, &blk) else #{method_name}_object.send("#{kind}_#{name}", self, &blk) end -- cgit v1.2.3