aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/attribute_methods_test.rb
diff options
context:
space:
mode:
authorPaul Gillard <paulmgillard@gmail.com>2009-08-04 16:19:19 -0500
committerJoshua Peek <josh@joshpeek.com>2009-08-04 16:19:19 -0500
commitc30a0ce3c8f88baebd369180a6e221706e2b5cbf (patch)
tree7ec8f230d8c0f87f889b1b83f0668b12dad6d6e9 /activerecord/test/cases/attribute_methods_test.rb
parentaad5a30bf25d8a3167afd685fc91c99f4f09cc57 (diff)
downloadrails-c30a0ce3c8f88baebd369180a6e221706e2b5cbf.tar.gz
rails-c30a0ce3c8f88baebd369180a6e221706e2b5cbf.tar.bz2
rails-c30a0ce3c8f88baebd369180a6e221706e2b5cbf.zip
Modified ActiveRecord::AttributeMethods to allow classes to specify attribute method prefixes and/or suffixes. Previously only suffixes were allowed.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activerecord/test/cases/attribute_methods_test.rb')
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb94
1 files changed, 79 insertions, 15 deletions
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 168b617fbc..a5f4a67200 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -4,40 +4,90 @@ require 'models/minimalistic'
class AttributeMethodsTest < ActiveRecord::TestCase
fixtures :topics
+
def setup
- @old_suffixes = ActiveRecord::Base.send(:attribute_method_suffixes).dup
+ @old_matchers = ActiveRecord::Base.send(:attribute_method_matchers).dup
@target = Class.new(ActiveRecord::Base)
@target.table_name = 'topics'
end
def teardown
- ActiveRecord::Base.send(:attribute_method_suffixes).clear
- ActiveRecord::Base.attribute_method_suffix *@old_suffixes
+ ActiveRecord::Base.send(:attribute_method_matchers).clear
+ ActiveRecord::Base.send(:attribute_method_matchers).concat(@old_matchers)
end
- def test_match_attribute_method_query_returns_match_data
- assert_not_nil md = @target.match_attribute_method?('title=')
- assert_equal 'title', md.pre_match
- assert_equal ['='], md.captures
-
- %w(_hello_world ist! _maybe?).each do |suffix|
+ def test_match_attribute_method_query_returns_default_match_data
+ topic = @target.new(:title => 'Budget')
+ assert_not_nil match = topic.match_attribute_method?('title=')
+ assert_equal '', match.prefix
+ assert_equal 'title', match.base
+ assert_equal '=', match.suffix
+ end
+
+ def test_match_attribute_method_query_returns_match_data_for_prefixes
+ topic = @target.new(:title => 'Budget')
+ %w(default_ title_).each do |prefix|
+ @target.class_eval "def #{prefix}attribute(*args) args end"
+ @target.attribute_method_prefix prefix
+
+ assert_not_nil match = topic.match_attribute_method?("#{prefix}title")
+ assert_equal prefix, match.prefix
+ assert_equal 'title', match.base
+ assert_equal '', match.suffix
+ end
+ end
+
+ def test_match_attribute_method_query_returns_match_data_for_suffixes
+ topic = @target.new(:title => 'Budget')
+ %w(_default _title_default it! _candidate= _maybe?).each do |suffix|
@target.class_eval "def attribute#{suffix}(*args) args end"
@target.attribute_method_suffix suffix
- assert_not_nil md = @target.match_attribute_method?("title#{suffix}")
- assert_equal 'title', md.pre_match
- assert_equal [suffix], md.captures
+ assert_not_nil match = topic.match_attribute_method?("title#{suffix}")
+ assert_equal '', match.prefix
+ assert_equal 'title', match.base
+ assert_equal suffix, match.suffix
end
end
-
- def test_declared_attribute_method_affects_respond_to_and_method_missing
+
+ def test_match_attribute_method_query_returns_match_data_for_affixes
+ topic = @target.new(:title => 'Budget')
+ [['mark_', '_for_update'], ['reset_', '!'], ['default_', '_value?']].each do |prefix, suffix|
+ @target.class_eval "def #{prefix}attribute#{suffix}(*args) args end"
+ @target.attribute_method_affix({ :prefix => prefix, :suffix => suffix })
+
+ assert_not_nil match = topic.match_attribute_method?("#{prefix}title#{suffix}")
+ assert_equal prefix, match.prefix
+ assert_equal 'title', match.base
+ assert_equal suffix, match.suffix
+ end
+ end
+
+ def test_undeclared_attribute_method_does_not_affect_respond_to_and_method_missing
topic = @target.new(:title => 'Budget')
assert topic.respond_to?('title')
assert_equal 'Budget', topic.title
assert !topic.respond_to?('title_hello_world')
assert_raise(NoMethodError) { topic.title_hello_world }
+ end
- %w(_hello_world _it! _candidate= able?).each do |suffix|
+ def test_declared_prefixed_attribute_method_affects_respond_to_and_method_missing
+ topic = @target.new(:title => 'Budget')
+ %w(default_ title_).each do |prefix|
+ @target.class_eval "def #{prefix}attribute(*args) args end"
+ @target.attribute_method_prefix prefix
+
+ meth = "#{prefix}title"
+ assert topic.respond_to?(meth)
+ assert_equal ['title'], topic.send(meth)
+ assert_equal ['title', 'a'], topic.send(meth, 'a')
+ assert_equal ['title', 1, 2, 3], topic.send(meth, 1, 2, 3)
+ end
+ end
+
+ def test_declared_suffixed_attribute_method_affects_respond_to_and_method_missing
+ topic = @target.new(:title => 'Budget')
+ %w(_default _title_default _it! _candidate= able?).each do |suffix|
@target.class_eval "def attribute#{suffix}(*args) args end"
@target.attribute_method_suffix suffix
@@ -49,6 +99,20 @@ class AttributeMethodsTest < ActiveRecord::TestCase
end
end
+ def test_declared_affixed_attribute_method_affects_respond_to_and_method_missing
+ topic = @target.new(:title => 'Budget')
+ [['mark_', '_for_update'], ['reset_', '!'], ['default_', '_value?']].each do |prefix, suffix|
+ @target.class_eval "def #{prefix}attribute#{suffix}(*args) args end"
+ @target.attribute_method_affix({ :prefix => prefix, :suffix => suffix })
+
+ meth = "#{prefix}title#{suffix}"
+ assert topic.respond_to?(meth)
+ assert_equal ['title'], topic.send(meth)
+ assert_equal ['title', 'a'], topic.send(meth, 'a')
+ assert_equal ['title', 1, 2, 3], topic.send(meth, 1, 2, 3)
+ end
+ end
+
def test_should_unserialize_attributes_for_frozen_records
myobj = {:value1 => :value2}
topic = Topic.create("content" => myobj)