Skip to content

Commit

Permalink
Take AR affixes into account for Action Mailbox database models
Browse files Browse the repository at this point in the history
  • Loading branch information
chaadow committed Dec 8, 2023
1 parent 049c951 commit 174c72a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions actionmailbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
* Fix all Action Mailbox database related models to respect
`ActiveRecord::Base.table_name_prefix` configuration.

*Chedli Bourguiba*

Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/actionmailbox/CHANGELOG.md) for previous changes.
2 changes: 0 additions & 2 deletions actionmailbox/app/models/action_mailbox/inbound_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ module ActionMailbox
# inbound_email.mail.from # => 'david@loudthinking.com'
# inbound_email.source # Returns the full rfc822 source of the email as text
class InboundEmail < Record
self.table_name = "action_mailbox_inbound_emails"

include Incineratable, MessageId, Routable

has_one_attached :raw_email, service: ActionMailbox.storage_service
Expand Down
3 changes: 3 additions & 0 deletions actionmailbox/test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Application < Rails::Application
# For compatibility with applications that use this config
config.action_controller.include_all_helpers = false

config.active_record.table_name_prefix = 'prefix_'
config.active_record.table_name_suffix = '_suffix'

# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
Expand Down
2 changes: 1 addition & 1 deletion actionmailbox/test/migrations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def rerun_migration
end

def action_mailbox_tables
[:action_mailbox_inbound_emails]
@action_mailbox_tables ||= ActionMailbox::Record.descendants.map { |klass| klass.table_name.to_sym }
end

def primary_key(table)
Expand Down
38 changes: 38 additions & 0 deletions actionmailbox/test/models/record_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "test_helper"

class ActionMailbox::RecordTest < ActiveSupport::TestCase
setup do
@old_prefix = ActiveRecord::Base.table_name_prefix
@old_suffix = ActiveRecord::Base.table_name_suffix

ActiveRecord::Base.table_name_prefix = @prefix = "abc_"
ActiveRecord::Base.table_name_suffix = @suffix = "_xyz"

@descendants = ActionMailbox::Record.descendants
@descendants.map(&:reset_table_name)
end

test "prefix and suffix are added to the Action Mailbox tables' name" do
ActionMailbox::Record.descendants.each do |model|
assert_equal(
"#{@prefix}action_mailbox_#{table_name_without_affixes(model)}#{@suffix}",
model.table_name
)
end
ensure
ActiveRecord::Base.table_name_prefix = @old_prefix
ActiveRecord::Base.table_name_suffix = @old_suffix
@descendants.map(&:reset_table_name)
end

private
def table_name_without_affixes(model)
model.table_name
.dup
.tap { _1.delete_prefix!(model.table_name_prefix) }
.tap { _1.delete_prefix!("action_mailbox_") }
.tap { _1.delete_suffix!(model.table_name_suffix) }
end
end

0 comments on commit 174c72a

Please sign in to comment.