From 615ebd2a67487b001ec5b3388cf27ce663bddcb3 Mon Sep 17 00:00:00 2001 From: Sergey Gnuskov Date: Thu, 13 Jun 2024 15:55:36 +0300 Subject: [PATCH] Better transition period handling (#18) --- Gemfile.lock | 2 +- lib/uuidable.rb | 1 + lib/uuidable/active_record.rb | 5 +++-- lib/uuidable/v1_model_migration.rb | 19 +++++++++++++++++++ lib/uuidable/version.rb | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 lib/uuidable/v1_model_migration.rb diff --git a/Gemfile.lock b/Gemfile.lock index 89e4378..7f157be 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - uuidable (1.0.2) + uuidable (1.0.3) activerecord (>= 4.2, < 7.2) mysql-binuuid-rails (>= 1.3, < 2) uuidtools (>= 2.1, < 3) diff --git a/lib/uuidable.rb b/lib/uuidable.rb index 046561a..e2fd914 100644 --- a/lib/uuidable.rb +++ b/lib/uuidable.rb @@ -17,4 +17,5 @@ def generate_uuid require 'uuidable/migration' require 'uuidable/v1_migration_helpers' +require 'uuidable/v1_model_migration' require 'uuidable/active_record' diff --git a/lib/uuidable/active_record.rb b/lib/uuidable/active_record.rb index 20d0267..702030f 100644 --- a/lib/uuidable/active_record.rb +++ b/lib/uuidable/active_record.rb @@ -30,16 +30,17 @@ def uuidable(as_param: true) # rubocop:disable Metrics/AbcSize, Metrics/Cyclomat columns.select { |c| c.type == :binary && c.limit == 16 && c.name.include?('uuid') }.each do |column| attribute column.name.to_sym, MySQLBinUUID::Type.new end + + include V1ModelMigration if columns.any? { |c| c.name.include?(V1MigrationHelpers::OLD_POSTFIX) } rescue ::ActiveRecord::ConnectionNotEstablished, Mysql2::Error::ConnectionError, ::ActiveRecord::NoDatabaseError # rubocop:disable Lint/SuppressedException end end after_initialize do self.uuid = Uuidable.generate_uuid if attributes.keys.include?('uuid') && uuid.blank? - self.uuid__old = uuid if respond_to?(:uuid__old) end - validates :uuid, presence: true, uniqueness: true, if: :uuid_changed? + validates :uuid, presence: true, uniqueness: true, if: -> { try :uuid_changed? } if as_param define_method :to_param do diff --git a/lib/uuidable/v1_model_migration.rb b/lib/uuidable/v1_model_migration.rb new file mode 100644 index 0000000..ad0152e --- /dev/null +++ b/lib/uuidable/v1_model_migration.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Uuidable + # Temporary helpers for transition period when migrating to v1 + module V1ModelMigration + extend ActiveSupport::Concern + + included do + before_validation do + attributes.each_key do |attr_name| + next unless attr_name.include?(V1MigrationHelpers::OLD_POSTFIX) + + new_attr_name = attr_name.gsub(V1MigrationHelpers::OLD_POSTFIX, '') + public_send("#{attr_name}=", attributes[new_attr_name].to_s) + end + end + end + end +end diff --git a/lib/uuidable/version.rb b/lib/uuidable/version.rb index 6370a2e..029d638 100644 --- a/lib/uuidable/version.rb +++ b/lib/uuidable/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Uuidable - VERSION = '1.0.2' + VERSION = '1.0.3' end