提交 08a4c997 编写于 作者: M Michael Koziarski

Remove options from the attributes method, tidy up the implementation. Closes...

Remove options from the attributes method, tidy up the implementation.  Closes #11093 [juanjo.bazan, Koz]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8863 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 46356100
......@@ -2214,35 +2214,18 @@ def attributes=(new_attributes, guard_protected_attributes = true)
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
def attributes(options = nil)
attrs = {}
self.attribute_names.each do |name|
attrs[name]=read_attribute(name)
end
if options.nil?
self.attribute_names.inject({}) do |attrs, name|
attrs[name] = read_attribute(name)
attrs
else
if except = options[:except]
except = Array(except).collect { |attribute| attribute.to_s }
except.each { |attribute_name| attrs.delete(attribute_name) }
attrs
elsif only = options[:only]
only = Array(only).collect { |attribute| attribute.to_s }
attrs.delete_if { |key, value| !only.include?(key) }
attrs
else
raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
end
end
end
# Returns a hash of attributes before typecasting and deserialization.
def attributes_before_type_cast
attrs = {}
self.attribute_names.each do |name|
attrs[name]=read_attribute_before_type_cast(name)
self.attribute_names.inject({}) do |attrs, name|
attrs[name] = read_attribute_before_typecast(name)
attrs
end
attrs
end
# Format attributes nicely for inspect.
......
......@@ -1778,23 +1778,6 @@ def test_to_xml_with_block
assert xml.include?(%(<arbitrary-element>#{value}</arbitrary-element>))
end
def test_except_attributes
assert_equal(
%w( author_name type id approved replies_count bonus_time written_on content author_email_address parent_id last_read).sort,
topics(:first).attributes(:except => :title).keys.sort
)
assert_equal(
%w( replies_count bonus_time written_on content author_email_address parent_id last_read).sort,
topics(:first).attributes(:except => [ :title, :id, :type, :approved, :author_name ]).keys.sort
)
end
def test_include_attributes
assert_equal(%w( title ), topics(:first).attributes(:only => :title).keys)
assert_equal(%w( title author_name type id approved ).sort, topics(:first).attributes(:only => [ :title, :id, :type, :approved, :author_name ]).keys.sort)
end
def test_type_name_with_module_should_handle_beginning
assert_equal 'ActiveRecord::Person', ActiveRecord::Base.send(:type_name_with_module, 'Person')
assert_equal '::Person', ActiveRecord::Base.send(:type_name_with_module, '::Person')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册