Skip to content
Merged
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/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def ignored?(table_name)
def tables(stream)
super
dump_inherited_tables(stream)
empty_line(stream)
stream.puts

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

what's the tldr on this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ahh - the Ruby 3.3 build checks were failing because Rails 8 has removed the empty_line method. Under the hood it was just doing a puts anyway, so this will give us the same effect.

functions(stream)
triggers(stream)
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.3"
VERSION = "0.19.4"
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