diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-05-28 10:32:00 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-05-28 12:08:41 -0700 |
commit | 622021cf181419109fd284c5a237018e805f05b4 (patch) | |
tree | 5d9f068c46f54e750b854ae32f537eaea3af3907 /activerecord | |
parent | 092b92f1bdddc21d92c8e60d1eb112759beb790a (diff) | |
download | rails-622021cf181419109fd284c5a237018e805f05b4.tar.gz rails-622021cf181419109fd284c5a237018e805f05b4.tar.bz2 rails-622021cf181419109fd284c5a237018e805f05b4.zip |
Maintain column order when overriding existing columns
Working towards re-implementing serialized attributes to use the
properties API exposed the need for this, as serializing a column
shouldn't change the order of the columns.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/properties.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/custom_properties_test.rb | 5 |
2 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/properties.rb b/activerecord/lib/active_record/properties.rb index a51dd56bcf..c99594c823 100644 --- a/activerecord/lib/active_record/properties.rb +++ b/activerecord/lib/active_record/properties.rb @@ -88,9 +88,14 @@ module ActiveRecord private def add_user_provided_columns(schema_columns) - schema_columns.reject { |column| - user_provided_columns.key? column.name - } + user_provided_columns.values + existing_columns = schema_columns.map do |column| + user_provided_columns[column.name] || column + end + + existing_column_names = existing_columns.map(&:name) + new_columns = user_provided_columns.except(*existing_column_names).values + + existing_columns + new_columns end end end diff --git a/activerecord/test/cases/custom_properties_test.rb b/activerecord/test/cases/custom_properties_test.rb index 9910e4042c..397a8e0692 100644 --- a/activerecord/test/cases/custom_properties_test.rb +++ b/activerecord/test/cases/custom_properties_test.rb @@ -79,5 +79,10 @@ module ActiveRecord assert_equal 4.4, data.overloaded_float end + + def test_overloading_properties_does_not_change_column_order + column_names = OverloadedType.column_names + assert_equal %w(id overloaded_float unoverloaded_float overloaded_string_with_limit non_existent_decimal), column_names + end end end |