Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/active_uuid/all.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require_relative "../active_uuid"
require_relative "connection_patches"
require_relative "register_types"
18 changes: 18 additions & 0 deletions lib/active_uuid/register_types.rb
Original file line number Diff line number Diff line change
@@ -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,
)
19 changes: 19 additions & 0 deletions spec/integration/registered_type_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/support/fabricators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the return of the conditional for variable assignment and comparison.

self.table_name = "native_uuid_articles"
else
self.table_name = "binary_uuid_articles"
end

attribute :id, :uuid
attribute :another_uuid, :uuid
end