From 5aa1f5d39987e176631fa9119d12e0aaabf98787 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 26 May 2014 17:06:05 -0700 Subject: Allow specifying a default value in overloaded properties --- activerecord/lib/active_record/properties.rb | 15 +++++++++++++-- activerecord/test/cases/custom_properties_test.rb | 9 +++++---- activerecord/test/schema/schema.rb | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/properties.rb b/activerecord/lib/active_record/properties.rb index 7fe59ccce4..21ff906fec 100644 --- a/activerecord/lib/active_record/properties.rb +++ b/activerecord/lib/active_record/properties.rb @@ -14,6 +14,17 @@ module ActiveRecord # Active Record's type casting behavior, as well as adding support for user defined # types. # + # +name+ The name of the methods to define attribute methods for, and the column which + # this will persist to. + # + # +cast_type+ A type object that contains information about how to type cast the value. + # See the examples section for more information. + # + # ==== Options + # The options hash accepts the following options: + # + # +default+ is the default value that the column should use on a new record. + # # ==== Examples # # The type detected by Active Record can be overriden. @@ -62,11 +73,11 @@ module ActiveRecord # # store_listing = StoreListing.new(price_in_cents: '$10.00') # store_listing.price_in_cents # => 1000 - def property(name, cast_type) + def property(name, cast_type, options = {}) name = name.to_s clear_properties_cache # Assign a new hash to ensure that subclasses do not share a hash - self.user_provided_columns = user_provided_columns.merge(name => connection.new_column(name, nil, cast_type)) + self.user_provided_columns = user_provided_columns.merge(name => connection.new_column(name, options[:default], cast_type)) end # Returns an array of column objects for the table associated with this class. diff --git a/activerecord/test/cases/custom_properties_test.rb b/activerecord/test/cases/custom_properties_test.rb index 047c1b9b74..a406704114 100644 --- a/activerecord/test/cases/custom_properties_test.rb +++ b/activerecord/test/cases/custom_properties_test.rb @@ -4,6 +4,7 @@ class OverloadedType < ActiveRecord::Base property :overloaded_float, Type::Integer.new property :overloaded_string_with_limit, Type::String.new(limit: 50) property :non_existent_decimal, Type::Decimal.new + property :string_with_default, Type::String.new, default: 'the overloaded default' end class ChildOfOverloadedType < OverloadedType @@ -62,12 +63,12 @@ module ActiveRecord end end - def test_overloaded_properties_have_no_default + def test_changing_defaults data = OverloadedType.new unoverloaded_data = UnoverloadedType.new - assert_nil data.overloaded_float - assert unoverloaded_data.overloaded_float + assert_equal 'the overloaded default', data.string_with_default + assert_equal 'the original default', unoverloaded_data.string_with_default end def test_children_inherit_custom_properties @@ -84,7 +85,7 @@ module ActiveRecord 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 + assert_equal %w(id overloaded_float unoverloaded_float overloaded_string_with_limit string_with_default non_existent_decimal), column_names end end end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 4cce58f4f4..5f459cf682 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -860,6 +860,7 @@ ActiveRecord::Schema.define do t.float :overloaded_float, default: 500 t.float :unoverloaded_float t.string :overloaded_string_with_limit, limit: 255 + t.string :string_with_default, default: 'the original default' end end -- cgit v1.2.3