diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-02-15 13:49:21 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-02-15 14:22:08 -0700 |
commit | 8c837e5fcc2a3ac639b3b93b0024bd2c20d3b6ed (patch) | |
tree | 48c22796cb5e039bc4f21cc75458f4467ecf76eb /activerecord/test/cases/type_test.rb | |
parent | 1747c4e2cea811cbf04fccc9f57256c80112d9ce (diff) | |
download | rails-8c837e5fcc2a3ac639b3b93b0024bd2c20d3b6ed.tar.gz rails-8c837e5fcc2a3ac639b3b93b0024bd2c20d3b6ed.tar.bz2 rails-8c837e5fcc2a3ac639b3b93b0024bd2c20d3b6ed.zip |
Add a global type registry, used to lookup and register types
As per previous discussions, we want to give users the ability to
reference their own types with symbols, instead of having to pass the
object manually. This adds the class that will be used to do so.
ActiveRecord::Type.register(:money, MyMoneyType)
Diffstat (limited to 'activerecord/test/cases/type_test.rb')
-rw-r--r-- | activerecord/test/cases/type_test.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/test/cases/type_test.rb b/activerecord/test/cases/type_test.rb new file mode 100644 index 0000000000..d45a9b3141 --- /dev/null +++ b/activerecord/test/cases/type_test.rb @@ -0,0 +1,39 @@ +require "cases/helper" + +class TypeTest < ActiveRecord::TestCase + setup do + @old_registry = ActiveRecord::Type.registry + ActiveRecord::Type.registry = ActiveRecord::Type::AdapterSpecificRegistry.new + end + + teardown do + ActiveRecord::Type.registry = @old_registry + end + + test "registering a new type" do + type = Struct.new(:args) + ActiveRecord::Type.register(:foo, type) + + assert_equal type.new(:arg), ActiveRecord::Type.lookup(:foo, :arg) + end + + test "looking up a type for a specific adapter" do + type = Struct.new(:args) + pgtype = Struct.new(:args) + ActiveRecord::Type.register(:foo, type, override: false) + ActiveRecord::Type.register(:foo, pgtype, adapter: :postgresql) + + assert_equal type.new, ActiveRecord::Type.lookup(:foo, adapter: :sqlite) + assert_equal pgtype.new, ActiveRecord::Type.lookup(:foo, adapter: :postgresql) + end + + test "lookup defaults to the current adapter" do + current_adapter = ActiveRecord::Base.connection.adapter_name.downcase.to_sym + type = Struct.new(:args) + adapter_type = Struct.new(:args) + ActiveRecord::Type.register(:foo, type, override: false) + ActiveRecord::Type.register(:foo, adapter_type, adapter: current_adapter) + + assert_equal adapter_type.new, ActiveRecord::Type.lookup(:foo) + end +end |