From fe20ed5f010762ee4d0ec343638690e01df64407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Ska=C5=82acki?= Date: Thu, 3 Jan 2019 20:36:19 +0100 Subject: [PATCH 1/2] Test regustered :uuid type Test if attribute type can be referenced with :uuid symbol. --- spec/integration/registered_type_spec.rb | 19 +++++++++++++++++++ spec/support/fabricators.rb | 2 +- spec/support/models.rb | 13 +++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/integration/registered_type_spec.rb diff --git a/spec/integration/registered_type_spec.rb b/spec/integration/registered_type_spec.rb new file mode 100644 index 0000000..727f66a --- /dev/null +++ b/spec/integration/registered_type_spec.rb @@ -0,0 +1,19 @@ +require "spec_helper" + +Dir["#{__dir__}/examples_*.rb"].each { |f| require f } + +RSpec.describe "using registered :uuid type" do + before do + if ENV.fetch("NO_PATCHES", false) + skip "Attribute types are not registered when monkey patching is disabled" + end + end + + include_examples "model with UUIDs" do + let(:model) { RegisteredUuidTypeArticle } + + unless ENV["DB"] == "postgresql" + let(:quoted_article_id) { ActiveUUID.quote_as_binary(article.id) } + end + end +end diff --git a/spec/support/fabricators.rb b/spec/support/fabricators.rb index a84667f..82d183d 100644 --- a/spec/support/fabricators.rb +++ b/spec/support/fabricators.rb @@ -5,7 +5,7 @@ [p, "uuid_article", s].compact.join("_").to_sym end -fab_names.push(:article) +fab_names.push(:article, :registered_uuid_type_article) fab_names.each do |fab_name| Fabricator(fab_name) do diff --git a/spec/support/models.rb b/spec/support/models.rb index 5374077..c833169 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -39,3 +39,16 @@ class Article < ActiveRecord::Base key_with_namescape_class_name = "#{regular_class_name}WithNamespace" Object.const_set key_with_namescape_class_name, key_with_namescape_class end + +class RegisteredUuidTypeArticle < ActiveRecord::Base + include ActiveUUID::Model + + if ENV["DB"] == "postgresql" + self.table_name = "native_uuid_articles" + else + self.table_name = "binary_uuid_articles" + end + + attribute :id, :uuid + attribute :another_uuid, :uuid +end From e5a24495eb2417bb82dd3da652adfc3935656850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Ska=C5=82acki?= Date: Thu, 3 Jan 2019 20:38:09 +0100 Subject: [PATCH 2/2] Add UUID attribute types to registry --- lib/active_uuid/all.rb | 1 + lib/active_uuid/register_types.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 lib/active_uuid/register_types.rb diff --git a/lib/active_uuid/all.rb b/lib/active_uuid/all.rb index ad64ff9..9f052e5 100644 --- a/lib/active_uuid/all.rb +++ b/lib/active_uuid/all.rb @@ -1,2 +1,3 @@ require_relative "../active_uuid" require_relative "connection_patches" +require_relative "register_types" diff --git a/lib/active_uuid/register_types.rb b/lib/active_uuid/register_types.rb new file mode 100644 index 0000000..55bc4e3 --- /dev/null +++ b/lib/active_uuid/register_types.rb @@ -0,0 +1,18 @@ +ActiveRecord::Type.register( + :uuid, + ::ActiveUUID::Type::BinaryUUID, + adapter: :mysql2, +) + +ActiveRecord::Type.register( + :uuid, + ::ActiveUUID::Type::StringUUID, + adapter: :postgresql, + override: true, +) + +ActiveRecord::Type.register( + :uuid, + ::ActiveUUID::Type::BinaryUUID, + adapter: :sqlite, +)