Added Base#attributes that returns a hash of all the attributes with their...

Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 [atyp.de]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@386 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 18e945d8
*SVN* *SVN*
* Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 [atyp.de]
* Fixed that foreign keys named the same as the association would cause stack overflow #437 [Eric Anderson] * Fixed that foreign keys named the same as the association would cause stack overflow #437 [Eric Anderson]
* Fixed default scope of acts_as_list from "1" to "1 = 1", so it'll work in PostgreSQL (among other places) #427 [Alexey] * Fixed default scope of acts_as_list from "1" to "1 = 1", so it'll work in PostgreSQL (among other places) #427 [Alexey]
......
...@@ -909,6 +909,18 @@ def attributes=(attributes) ...@@ -909,6 +909,18 @@ def attributes=(attributes)
assign_multiparameter_attributes(multi_parameter_attributes) assign_multiparameter_attributes(multi_parameter_attributes)
end end
# Returns a hash of all the attributes with their names as keys and clones of their objects as values.
def attributes
self.attribute_names.inject({}) do |attributes, name|
begin
attributes[name] = read_attribute(name).clone
rescue TypeError
attributes[name] = read_attribute(name)
end
attributes
end
end
# Returns true if the specified +attribute+ has been set by the user or by a database load and is neither # Returns true if the specified +attribute+ has been set by the user or by a database load and is neither
# nil nor empty? (the latter only applies to objects that responds to empty?, most notably Strings). # nil nor empty? (the latter only applies to objects that responds to empty?, most notably Strings).
def attribute_present?(attribute) def attribute_present?(attribute)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require 'fixtures/topic' require 'fixtures/topic'
require 'fixtures/reply' require 'fixtures/reply'
require 'fixtures/company' require 'fixtures/company'
require 'fixtures/project'
require 'fixtures/default' require 'fixtures/default'
require 'fixtures/auto_id' require 'fixtures/auto_id'
require 'fixtures/column_name' require 'fixtures/column_name'
...@@ -26,7 +27,7 @@ class TightDescendent < TightPerson ...@@ -26,7 +27,7 @@ class TightDescendent < TightPerson
class Booleantest < ActiveRecord::Base; end class Booleantest < ActiveRecord::Base; end
class BasicsTest < Test::Unit::TestCase class BasicsTest < Test::Unit::TestCase
fixtures :topics, :companies fixtures :topics, :companies, :projects
def test_set_attributes def test_set_attributes
topic = Topic.find(1) topic = Topic.find(1)
...@@ -100,6 +101,10 @@ def test_update_array_content ...@@ -100,6 +101,10 @@ def test_update_array_content
topic.content << "five" topic.content << "five"
assert_equal(%w( one two three four five ), topic.content) assert_equal(%w( one two three four five ), topic.content)
end end
def test_attributes_hash
assert_equal @projects["action_controller"].to_hash, @action_controller.attributes
end
def test_create def test_create
topic = Topic.new topic = Topic.new
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册