Skip to content
Merged
7 changes: 5 additions & 2 deletions lib/hoardable/database_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion lib/hoardable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/hoardable/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Hoardable
VERSION = "0.19.5"
VERSION = "0.19.6"
end
9 changes: 5 additions & 4 deletions lib/hoardable/version_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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