aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb14
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb6
2 files changed, 9 insertions, 11 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 149f6abf61..44e3e64a9e 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -317,7 +317,7 @@ module ActiveModel
private
class AttributeMethodMatcher
- attr_reader :prefix, :suffix
+ attr_reader :prefix, :suffix, :method_missing_target
AttributeMethodMatch = Struct.new(:target, :attr_name)
@@ -325,22 +325,20 @@ module ActiveModel
options.symbolize_keys!
@prefix, @suffix = options[:prefix] || '', options[:suffix] || ''
@regex = /^(#{Regexp.escape(@prefix)})(.+?)(#{Regexp.escape(@suffix)})$/
+ @method_missing_target = :"#{@prefix}attribute#{@suffix}"
+ @method_name = "#{prefix}%s#{suffix}"
end
def match(method_name)
- if matchdata = @regex.match(method_name)
- AttributeMethodMatch.new(method_missing_target, matchdata[2])
+ if @regex =~ method_name
+ AttributeMethodMatch.new(method_missing_target, $2)
else
nil
end
end
def method_name(attr_name)
- "#{prefix}#{attr_name}#{suffix}"
- end
-
- def method_missing_target
- :"#{prefix}attribute#{suffix}"
+ @method_name % attr_name
end
end
end
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 3c4fa4fd3f..1750bf004a 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -541,7 +541,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
topic = @target.new(:title => "The pros and cons of programming naked.")
assert !topic.respond_to?(:title)
exception = assert_raise(NoMethodError) { topic.title }
- assert_equal "Attempt to call private method", exception.message
+ assert_match %r(^Attempt to call private method), exception.message
assert_equal "I'm private", topic.send(:title)
end
@@ -551,7 +551,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
topic = @target.new
assert !topic.respond_to?(:title=)
exception = assert_raise(NoMethodError) { topic.title = "Pants"}
- assert_equal "Attempt to call private method", exception.message
+ assert_match %r(^Attempt to call private method), exception.message
topic.send(:title=, "Very large pants")
end
@@ -561,7 +561,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
topic = @target.new(:title => "Isaac Newton's pants")
assert !topic.respond_to?(:title?)
exception = assert_raise(NoMethodError) { topic.title? }
- assert_equal "Attempt to call private method", exception.message
+ assert_match %r(^Attempt to call private method), exception.message
assert topic.send(:title?)
end