aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-03-27 11:07:07 +0100
committerJosé Valim <jose.valim@gmail.com>2010-03-27 11:07:07 +0100
commit418e5fc279b19c0d35d3f2b6b5ce216a566c8fc2 (patch)
tree33d51b954b8f6cac5e64e64d2152d6b0ca8cc40c
parent0cb3311d06c02649fb7444c34b6fdf2214ab85f5 (diff)
parenta24a888afe5652e391ce2ea682596686af5ec58b (diff)
downloadrails-418e5fc279b19c0d35d3f2b6b5ce216a566c8fc2.tar.gz
rails-418e5fc279b19c0d35d3f2b6b5ce216a566c8fc2.tar.bz2
rails-418e5fc279b19c0d35d3f2b6b5ce216a566c8fc2.zip
Merge branch 'master' of gitproxy:rails/rails
-rw-r--r--actionpack/lib/action_view/helpers/active_model_helper.rb4
-rw-r--r--actionpack/test/template/active_model_helper_test.rb14
-rw-r--r--activesupport/lib/active_support/core_ext/array/extract_options.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb13
-rw-r--r--activesupport/lib/active_support/core_ext/module/attribute_accessors.rb14
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb4
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb34
-rw-r--r--activesupport/test/core_ext/class/attribute_accessor_test.rb8
-rw-r--r--activesupport/test/core_ext/module/attribute_accessor_test.rb6
9 files changed, 102 insertions, 12 deletions
diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb
index e3db2923f7..bbe8609e91 100644
--- a/actionpack/lib/action_view/helpers/active_model_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -295,6 +295,10 @@ module ActionView
end
end
+ def error_message
+ object.errors[@method_name]
+ end
+
def column_type
object.send(:column_for_attribute, @method_name).type
end
diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb
index 371aa53c68..71c7d5b621 100644
--- a/actionpack/test/template/active_model_helper_test.rb
+++ b/actionpack/test/template/active_model_helper_test.rb
@@ -147,6 +147,20 @@ class ActiveModelHelperTest < ActionView::TestCase
)
end
+ def test_field_error_proc
+ old_proc = ActionView::Base.field_error_proc
+ ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
+ %(<div class=\"fieldWithErrors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
+ end
+
+ assert_dom_equal(
+ %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>),
+ text_field("post", "author_name")
+ )
+ ensure
+ ActionView::Base.field_error_proc = old_proc if old_proc
+ end
+
def test_form_with_string
assert_dom_equal(
%(<form action="create" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
diff --git a/activesupport/lib/active_support/core_ext/array/extract_options.rb b/activesupport/lib/active_support/core_ext/array/extract_options.rb
index 9ca32dc7aa..40ceb3eb9e 100644
--- a/activesupport/lib/active_support/core_ext/array/extract_options.rb
+++ b/activesupport/lib/active_support/core_ext/array/extract_options.rb
@@ -1,3 +1,14 @@
+class Hash
+ # By default, only instances of Hash itself are extractable.
+ # Subclasses of Hash may implement this method and return
+ # true to declare themselves as extractable. If a Hash
+ # is extractable, Array#extract_options! pops it from
+ # the Array when it is the last element of the Array.
+ def extractable_options?
+ instance_of?(Hash)
+ end
+end
+
class Array
# Extracts options from a set of arguments. Removes and returns the last
# element in the array if it's a hash, otherwise returns a blank hash.
@@ -9,6 +20,10 @@ class Array
# options(1, 2) # => {}
# options(1, 2, :a => :b) # => {:a=>:b}
def extract_options!
- last.is_a?(::Hash) ? pop : {}
+ if last.is_a?(Hash) && last.extractable_options?
+ pop
+ else
+ {}
+ end
end
end
diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
index 1602a609eb..83d154be5c 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -10,8 +10,8 @@ require 'active_support/core_ext/array/extract_options'
# Person.hair_colors = [:brown, :black, :blonde, :red]
class Class
def cattr_reader(*syms)
+ options = syms.extract_options!
syms.flatten.each do |sym|
- next if sym.is_a?(Hash)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@hair_colors
@@#{sym} = nil # @@hair_colors = nil
@@ -20,11 +20,14 @@ class Class
def self.#{sym} # def self.hair_colors
@@#{sym} # @@hair_colors
end # end
- #
- def #{sym} # def hair_colors
- @@#{sym} # @@hair_colors
- end # end
EOS
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{sym} # def hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
+ EOS
+ end
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
index 131b512944..c315fd5812 100644
--- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -2,7 +2,7 @@ require 'active_support/core_ext/array/extract_options'
class Module
def mattr_reader(*syms)
- syms.extract_options!
+ options = syms.extract_options!
syms.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@pagination_options
@@ -12,11 +12,15 @@ class Module
def self.#{sym} # def self.pagination_options
@@#{sym} # @@pagination_options
end # end
-
- def #{sym} # def pagination_options
- @@#{sym} # @@pagination_options
- end # end
EOS
+
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__)
+ def #{sym} # def hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
+ EOS
+ end
end
end
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 543dab4a75..8241b69c8b 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -4,6 +4,10 @@
module ActiveSupport
class HashWithIndifferentAccess < Hash
+ def extractable_options?
+ true
+ end
+
def initialize(constructor = {})
if constructor.is_a?(Hash)
super()
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index b374eca370..aecc644549 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -4,6 +4,7 @@ require 'active_support/core_ext/big_decimal'
require 'active_support/core_ext/object/conversions'
require 'active_support/core_ext' # FIXME: pulling in all to_xml extensions
+require 'active_support/hash_with_indifferent_access'
class ArrayExtAccessTests < Test::Unit::TestCase
def test_from
@@ -294,12 +295,45 @@ class ArrayToXmlTests < Test::Unit::TestCase
end
class ArrayExtractOptionsTests < Test::Unit::TestCase
+ class HashSubclass < Hash
+ end
+
+ class ExtractableHashSubclass < Hash
+ def extractable_options?
+ true
+ end
+ end
+
def test_extract_options
assert_equal({}, [].extract_options!)
assert_equal({}, [1].extract_options!)
assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
end
+
+ def test_extract_options_doesnt_extract_hash_subclasses
+ hash = HashSubclass.new
+ hash[:foo] = 1
+ array = [hash]
+ options = array.extract_options!
+ assert_equal({}, options)
+ assert_equal [hash], array
+ end
+
+ def test_extract_options_extracts_extractable_subclass
+ hash = ExtractableHashSubclass.new
+ hash[:foo] = 1
+ array = [hash]
+ options = array.extract_options!
+ assert_equal({:foo => 1}, options)
+ assert_equal [], array
+ end
+
+ def test_extract_options_extracts_hwia
+ hash = [{:foo => 1}.with_indifferent_access]
+ options = hash.extract_options!
+ assert_equal 1, options[:foo]
+ end
end
class ArrayUniqByTests < Test::Unit::TestCase
diff --git a/activesupport/test/core_ext/class/attribute_accessor_test.rb b/activesupport/test/core_ext/class/attribute_accessor_test.rb
index 2214ba9894..0f579d12e5 100644
--- a/activesupport/test/core_ext/class/attribute_accessor_test.rb
+++ b/activesupport/test/core_ext/class/attribute_accessor_test.rb
@@ -5,7 +5,8 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase
def setup
@class = Class.new do
cattr_accessor :foo
- cattr_accessor :bar, :instance_writer => false
+ cattr_accessor :bar, :instance_writer => false
+ cattr_reader :shaq, :instance_reader => false
end
@object = @class.new
end
@@ -29,4 +30,9 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase
assert @object.respond_to?(:bar)
assert !@object.respond_to?(:bar=)
end
+
+ def test_should_not_create_instance_reader
+ assert @class.respond_to?(:shaq)
+ assert !@object.respond_to?(:shaq)
+ end
end
diff --git a/activesupport/test/core_ext/module/attribute_accessor_test.rb b/activesupport/test/core_ext/module/attribute_accessor_test.rb
index bd9461e62c..263e78feaa 100644
--- a/activesupport/test/core_ext/module/attribute_accessor_test.rb
+++ b/activesupport/test/core_ext/module/attribute_accessor_test.rb
@@ -6,6 +6,7 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase
m = @module = Module.new do
mattr_accessor :foo
mattr_accessor :bar, :instance_writer => false
+ mattr_reader :shaq, :instance_reader => false
end
@class = Class.new
@class.instance_eval { include m }
@@ -31,4 +32,9 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase
assert @object.respond_to?(:bar)
assert !@object.respond_to?(:bar=)
end
+
+ def test_should_not_create_instance_reader
+ assert @module.respond_to?(:shaq)
+ assert !@object.respond_to?(:shaq)
+ end
end