aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2012-09-11 23:05:19 -0400
committerMarc-Andre Lafortune <github@marc-andre.ca>2012-09-11 23:05:19 -0400
commitad5f18e6e936e783e0e97a9315ed4a72f333a2cd (patch)
tree754b96ceb1ceea9b7a6dfa0118dc8700cf416bad /activesupport
parent34b23e7110a3a13cf157608cefc9b5701017bf39 (diff)
downloadrails-ad5f18e6e936e783e0e97a9315ed4a72f333a2cd.tar.gz
rails-ad5f18e6e936e783e0e97a9315ed4a72f333a2cd.tar.bz2
rails-ad5f18e6e936e783e0e97a9315ed4a72f333a2cd.zip
Nice and easy delegation to the class
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb3
-rw-r--r--activesupport/test/core_ext/module_test.rb11
2 files changed, 14 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 39a1240c61..8703587243 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -123,6 +123,9 @@ class Module
file, line = caller.first.split(':', 2)
line = line.to_i
+ to = to.to_s
+ to = 'self.class' if to == 'class'
+
methods.each do |method|
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index bd41311739..82249ddd1b 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -34,6 +34,12 @@ class Someone < Struct.new(:name, :place)
delegate :street, :city, :to_f, :to => :place
delegate :name=, :to => :place, :prefix => true
delegate :upcase, :to => "place.city"
+ delegate :table_name, :to => :class
+ delegate :table_name, :to => :class, :prefix => true
+
+ def self.table_name
+ 'some_table'
+ end
FAILED_DELEGATE_LINE = __LINE__ + 1
delegate :foo, :to => :place
@@ -111,6 +117,11 @@ class ModuleTest < ActiveSupport::TestCase
assert_equal "DAVID HANSSON", david.upcase
end
+ def test_delegation_to_class_method
+ assert_equal 'some_table', @david.table_name
+ assert_equal 'some_table', @david.class_table_name
+ end
+
def test_missing_delegation_target
assert_raise(ArgumentError) do
Name.send :delegate, :nowhere