diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-15 16:13:29 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-15 16:13:29 -0700 |
commit | d043d6539682f804a696d301b580accfc68f6cce (patch) | |
tree | d1303b0c4dc8a2ed14725c12eec300b7e1fa3bb5 /activerecord | |
parent | 6e581cce1c1116d71484a5a4a5f92f0ea6fd29e4 (diff) | |
parent | d77b306b63e20aabec5daf7159d31c8ee31492c9 (diff) | |
download | rails-d043d6539682f804a696d301b580accfc68f6cce.tar.gz rails-d043d6539682f804a696d301b580accfc68f6cce.tar.bz2 rails-d043d6539682f804a696d301b580accfc68f6cce.zip |
Merge pull request #570 from sikachu/decouple_actionpack
Make ParamsWrapper use a well-defined API and not rely on AR methods
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 13 |
3 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 32bcf02139..2e144745cd 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.1.0 (unreleased)* +* Add ActiveRecord::Base.attribute_names to return a list of attribute names. This will return an empty array if the model is abstract or table does not exists. [Prem Sichanugrist] + * CSV Fixtures are deprecated and support will be removed in Rails 3.2.0 * AR#new, AR#create, AR#create!, AR#update_attributes and AR#update_attributes! all accept a second hash as option that allows you diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e1bf2ccc8a..cfe6d8d2de 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -767,6 +767,14 @@ module ActiveRecord #:nodoc: super || (table_exists? && column_names.include?(attribute.to_s.sub(/=$/, ''))) end + def attribute_names + @attribute_names ||= if !abstract_class? && table_exists? + column_names + else + [] + end + end + # Set the lookup ancestors for ActiveModel. def lookup_ancestors #:nodoc: klass = self diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9bc04ed29c..bfb66f07da 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1790,4 +1790,17 @@ class BasicsTest < ActiveRecord::TestCase assert_equal expected.attributes, actual.attributes end + + def test_attribute_names + assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id"], + Company.attribute_names + end + + def test_attribute_names_on_table_not_exists + assert_equal [], NonExistentTable.attribute_names + end + + def test_attribtue_names_on_abstract_class + assert_equal [], AbstractCompany.attribute_names + end end |