aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-02-17 14:17:59 -0700
committerSean Griffin <sean@thoughtbot.com>2015-02-17 14:17:59 -0700
commit82d12eb9045cba57172ec7cc0786d0f72a8b711f (patch)
treeb43fb5d830bd8e2d15e638907ae06b95f46aca32 /activerecord
parentad127d8836165bba70290a9429eee5b16033e20c (diff)
downloadrails-82d12eb9045cba57172ec7cc0786d0f72a8b711f.tar.gz
rails-82d12eb9045cba57172ec7cc0786d0f72a8b711f.tar.bz2
rails-82d12eb9045cba57172ec7cc0786d0f72a8b711f.zip
Add docs for the type registry
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attributes.rb14
-rw-r--r--activerecord/lib/active_record/type.rb15
2 files changed, 24 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 45a764bd5a..c8979a60d7 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -119,15 +119,21 @@ module ActiveRecord
# end
# end
#
+ # # config/initializers/types.rb
+ # ActiveRecord::Type.register(:money, MoneyType)
+ #
+ # # /app/models/store_listing.rb
# class StoreListing < ActiveRecord::Base
- # attribute :price_in_cents, MoneyType.new
+ # attribute :price_in_cents, :money
# end
#
# store_listing = StoreListing.new(price_in_cents: '$10.00')
# store_listing.price_in_cents # => 1000
#
# For more details on creating custom types, see the documentation for
- # ActiveRecord::Type::Value.
+ # ActiveRecord::Type::Value. For more details on registering your types
+ # to be referenced by a symbol, see ActiveRecord::Type.register. You can
+ # also pass a type object directly, in place of a symbol.
#
# ==== Querying
#
@@ -152,9 +158,11 @@ module ActiveRecord
# end
# end
#
+ # ActiveRecord::Type.register(:money, MoneyType)
+ #
# class Product < ActiveRecord::Base
# currency_converter = ConversionRatesFromTheInternet.new
- # attribute :price_in_bitcoins, MoneyType.new(currency_converter)
+ # attribute :price_in_bitcoins, :money, currency_converter
# end
#
# Product.where(price_in_bitcoins: Money.new(5, "USD"))
diff --git a/activerecord/lib/active_record/type.rb b/activerecord/lib/active_record/type.rb
index cddd56a20d..2c0cda69d0 100644
--- a/activerecord/lib/active_record/type.rb
+++ b/activerecord/lib/active_record/type.rb
@@ -26,10 +26,21 @@ module ActiveRecord
class << self
attr_accessor :registry # :nodoc:
+ delegate :add_modifier, to: :registry
- delegate :register, :add_modifier, to: :registry
+ # Add a new type to the registry, allowing it to be referenced as a
+ # symbol by ActiveRecord::Attributes::ClassMethods#attribute. If your
+ # type is only meant to be used with a specific database adapter, you can
+ # do so by passing +adapter: :postgresql+. If your type has the same
+ # name as a native type for the current adapter, an exception will be
+ # raised unless you specify an +:override+ option. +override: true+ will
+ # cause your type to be used instead of the native type. +override:
+ # false+ will cause the native type to be used over yours if one exists.
+ def register(type_name, klass = nil, **options, &block)
+ registry.register(type_name, klass, **options, &block)
+ end
- def lookup(*args, adapter: current_adapter_name, **kwargs)
+ def lookup(*args, adapter: current_adapter_name, **kwargs) # :nodoc:
registry.lookup(*args, adapter: adapter, **kwargs)
end