Module: Goodmail::Configuration

Included in:
Goodmail
Defined in:
lib/goodmail/configuration.rb

Overview

Handles configuration settings for the Goodmail gem.

Constant Summary collapse

THREAD_CONFIG_KEY =
:goodmail_current_config
DEFAULT_CONFIG =

Default configuration values

OpenStruct.new(
  brand_color:  "#348eda",
  company_name: "Example Inc.",
  logo_url:     nil,
  # Optional: URL the header logo links to.
  company_url:  nil,
  # Optional: Global default unsubscribe URL.
  unsubscribe_url: nil,
  # Optional: Default preheader text (appears after subject in inbox preview).
  default_preheader: nil,
  # Optional footer text (e.g., "Why you received this email")
  footer_text:  nil,
  # Show a visible unsubscribe link in the footer?
  show_footer_unsubscribe_link: false,
  # Text for the footer unsubscribe link
  footer_unsubscribe_link_text: "Unsubscribe"
).freeze
REQUIRED_CONFIG_KEYS =

Define required keys that MUST be set by the user (cannot be nil/empty)

%i[company_name].freeze

Instance Method Summary collapse

Instance Method Details

#configObject Also known as: configuration

Returns the current configuration object. Initializes with a copy of the defaults if not already configured.



40
41
42
# File 'lib/goodmail/configuration.rb', line 40

def config
  Thread.current[THREAD_CONFIG_KEY] || global_config
end

#config_with(overrides) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/goodmail/configuration.rb', line 62

def config_with(overrides)
  override_config = config.dup
  if overrides.respond_to?(:to_h)
    overrides.to_h.each { |key, value| override_config[key] = value }
  else
    overrides.each_pair { |key, value| override_config[key] = value }
  end
  validate_config!(override_config)
  override_config
end

#configure {|global_config| ... } ⇒ Object

Provides the configuration block helper. Ensures validation runs after the block is executed.

Yields:



33
34
35
36
# File 'lib/goodmail/configuration.rb', line 33

def configure
  yield global_config # Ensures global config is initialized via accessor
  validate_config!(global_config)
end

#global_configObject

Returns the process-wide configuration object, ignoring any temporary per-render override installed by ‘with_config`.



75
76
77
78
# File 'lib/goodmail/configuration.rb', line 75

def global_config
  @config = DEFAULT_CONFIG.dup unless defined?(@config) && @config
  @config
end

#reset_config!Object

Resets the configuration back to the default values. Primarily useful for testing environments.



82
83
84
85
# File 'lib/goodmail/configuration.rb', line 82

def reset_config!
  @config = nil
  Thread.current[THREAD_CONFIG_KEY] = nil
end

#with_config(overrides) ⇒ Object

Runs a block with a per-render configuration override in the current thread. This keeps whitelabel / tenant-specific emails from mutating the process-wide config while preserving the existing ‘Goodmail.config` read path used by Builder, Layout, and Plaintext.

Source: Ruby thread-local variables: docs.ruby-lang.org/en/3.4/Thread.html#method-i-5B-5D



52
53
54
55
56
57
58
59
60
# File 'lib/goodmail/configuration.rb', line 52

def with_config(overrides)
  return yield if overrides.nil?

  previous_config = Thread.current[THREAD_CONFIG_KEY]
  Thread.current[THREAD_CONFIG_KEY] = config_with(overrides)
  yield
ensure
  Thread.current[THREAD_CONFIG_KEY] = previous_config if defined?(previous_config)
end