module Thincloud
  module Postmark
    # Public: Thincloud Postmark Engine
    class Engine < ::Rails::Engine

      # convenience method for engine options / configuration
      def configuration
        Thincloud::Postmark.configuration
      end

      # initialize the configuration so it is available during rails init
      ActiveSupport.on_load :before_configuration do
        unless config.respond_to? :thincloud
          config.thincloud = ActiveSupport::OrderedOptions.new
        end

        config.thincloud.postmark ||= Thincloud::Postmark.configure
      end

      # Require the config initializer in advance so it is available for
      # the "thincloud.postmark.action_mailer" initializer
      initializer "thincloud.postmark.configuration", before: "thincloud.postmark.action_mailer" do
        config_initializer = File.expand_path("config/initializers/thincloud_postmark.rb")
        require config_initializer if File.exists?(config_initializer)
      end

      # Apply the postmark settings just before ActionMailer applies them
      initializer "thincloud.postmark.action_mailer", before: "action_mailer.set_configs" do |app|
        if configuration.api_key
          app.config.action_mailer.delivery_method = :postmark
          app.config.action_mailer.postmark_settings = { api_key: configuration.api_key }
        end
      end

    end
  end
end
module Thincloud
  module Postmark
    # Public: Thincloud Postmark Engine
    class Engine < ::Rails::Engine

      # convenience method for engine options / configuration
      def configuration
        Thincloud::Postmark.configuration
      end

      # initialize the configuration so it is available during rails init
      ActiveSupport.on_load :before_configuration do
        unless config.respond_to? :thincloud
          config.thincloud = ActiveSupport::OrderedOptions.new
        end

        config.thincloud.postmark ||= Thincloud::Postmark.configure
      end

      # Apply the postmark settings just before ActionMailer applies them
      initializer "thincloud.postmark.action_mailer", after: "finisher_hook" do |app|
        if configuration.api_key
          app.config.action_mailer.delivery_method = :postmark
          app.config.action_mailer.postmark_settings = { api_key: configuration.api_key }
        end
      end

    end
  end
end
Use different hook for action_mailer config
Use different hook for action_mailer config

By using the `after: "finisher_hook"` we are 
alleviated from the specific load order of the
initializers. ActionMailer allows the addition
of delivery methods at any point during app
execution as long as it's there before you send
mail.

Now we can lose the specific initializer path
tests, the generated initializer is unneeded
in a standard application, and the app can use
any method to set the configuration during 
initialization and it's picked up at the end.