aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/properties.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-26 17:06:05 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-30 08:51:58 -0700
commit5aa1f5d39987e176631fa9119d12e0aaabf98787 (patch)
tree8e8e2da8fd7ccadb3448c9cfe956610af41f57ae /activerecord/lib/active_record/properties.rb
parent8c77b0a086bb47ef7cd4b827460a51613f94094e (diff)
downloadrails-5aa1f5d39987e176631fa9119d12e0aaabf98787.tar.gz
rails-5aa1f5d39987e176631fa9119d12e0aaabf98787.tar.bz2
rails-5aa1f5d39987e176631fa9119d12e0aaabf98787.zip
Allow specifying a default value in overloaded properties
Diffstat (limited to 'activerecord/lib/active_record/properties.rb')
-rw-r--r--activerecord/lib/active_record/properties.rb15
1 files changed, 13 insertions, 2 deletions
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.