diff --git a/lib/hoardable/database_client.rb b/lib/hoardable/database_client.rb index cf8520a..e379518 100644 --- a/lib/hoardable/database_client.rb +++ b/lib/hoardable/database_client.rb @@ -28,8 +28,11 @@ def source_primary_key end def find_or_initialize_hoardable_event_uuid - Thread.current[:hoardable_event_uuid] ||= Thread.current[:contextual_event_uuid] || - SecureRandom.uuid + contextual_event_uuid = Thread.current[:contextual_event_uuid] + + return contextual_event_uuid unless contextual_event_uuid.nil? + + Thread.current[:hoardable_event_uuid] ||= SecureRandom.uuid end def initialize_version_attributes(operation) diff --git a/lib/hoardable/model.rb b/lib/hoardable/model.rb index 0cd9ee9..c4adbf7 100644 --- a/lib/hoardable/model.rb +++ b/lib/hoardable/model.rb @@ -9,6 +9,9 @@ module Model extend ActiveSupport::Concern class_methods do + # Allows setting a custom table name for the +version+ model. + attr_accessor :version_table_name + # If called with a hash, this will set the model-level +Hoardable+ configuration variables. If # called without an argument it will return the computed +Hoardable+ configuration considering # both model-level and global values. @@ -43,7 +46,7 @@ def with_hoardable_config(hash) end private def hoardable_current_config_key - "hoardable_#{name}_config".to_sym + :"hoardable_#{name}_config" end end diff --git a/lib/hoardable/version.rb b/lib/hoardable/version.rb index d87ab7a..bec0acd 100644 --- a/lib/hoardable/version.rb +++ b/lib/hoardable/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Hoardable - VERSION = "0.19.5" + VERSION = "0.19.6" end diff --git a/lib/hoardable/version_model.rb b/lib/hoardable/version_model.rb index 4e2e60e..6c90014 100644 --- a/lib/hoardable/version_model.rb +++ b/lib/hoardable/version_model.rb @@ -27,12 +27,13 @@ def version_class # disable STI on versions tables self.inheritance_column = :_ - self.table_name = "#{table_name.singularize}#{VERSION_TABLE_SUFFIX}" + self.table_name = + superclass.version_table_name || "#{table_name.singularize}#{VERSION_TABLE_SUFFIX}" alias_method :readonly?, :persisted? %i[operation event_uuid during].each do |symbol| - define_method("hoardable_#{symbol}") { public_send("_#{symbol}") } + define_method(:"hoardable_#{symbol}") { public_send(:"_#{symbol}") } end # @!scope class @@ -105,7 +106,7 @@ def untrash! transaction do insert_untrashed_source.tap do |untrashed| untrashed - .send("hoardable_client") + .send(:hoardable_client) .insert_hoardable_version("insert") do untrashed.instance_variable_set(:@hoardable_version, self) untrashed.run_callbacks(:untrashed) @@ -114,7 +115,7 @@ def untrash! end end - DATA_KEYS.each { |key| define_method("hoardable_#{key}") { _data&.dig(key.to_s) } } + DATA_KEYS.each { |key| define_method(:"hoardable_#{key}") { _data&.dig(key.to_s) } } # Returns the +ActiveRecord+ # {https://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-changes changes} that diff --git a/test/support/models.rb b/test/support/models.rb index 284bc5e..5e5e0a5 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -62,6 +62,13 @@ class PostWithUnhoardableRichText < ActiveRecord::Base has_rich_text :content end +class PostWithCustomVersionTable < ActiveRecord::Base + include Hoardable::Model + self.table_name = "posts" + self.version_table_name = "custom_post_versions" + belongs_to :user +end + class User < ActiveRecord::Base include Hoardable::Model has_many :posts, hoardable: true diff --git a/test/test_model.rb b/test/test_model.rb index 7746e74..6148295 100644 --- a/test/test_model.rb +++ b/test/test_model.rb @@ -799,4 +799,8 @@ def create_comments_and_destroy_post assert_equal("New name", tag.reload.name) assert_equal("Library", trashed_tag.name) end + + test "when using a custom version table name, it is respected" do + assert_equal(PostWithCustomVersionTable.version_class.table_name, "custom_post_versions") + end end