src/derails/inaction_propaganda/engine.rb
# frozen_string_literal: true

# :markup: markdown

require "rails"
require "action_controller/railtie"
require "active_record/railtie"
require "passive_hoarding/engine"

require "inaction_propaganda"
require "inaction_propaganda/trix"

module InactionPropaganda
  class Engine < Rails::Engine
    isolate_namespace InactionPropaganda
    config.eager_load_namespaces << InactionPropaganda

    config.inaction_propaganda = ActiveSupport::OrderedOptions.new
    config.inaction_propaganda.attachment_tag_name = "action-text-attachment"
    config.autoload_once_paths = %W(
      #{root}/app/helpers
      #{root}/app/models
    )

    initializer "inaction_propaganda.deprecator", before: :load_environment_config do |app|
      app.deprecators[:inaction_propaganda] = InactionPropaganda.deprecator
    end

    initializer "inaction_propaganda.attribute" do
      ActiveSupport.on_load(:active_record) do
        include InactionPropaganda::Attribute
        prepend InactionPropaganda::Encryption
      end
    end

    initializer "inaction_propaganda.asset" do
      if Rails.application.config.respond_to?(:assets)
        Rails.application.config.assets.precompile += %w( inactionpropaganda.js inactionpropaganda.esm.js )
      end
    end

    initializer "inaction_propaganda.attachable" do
      ActiveSupport.on_load(:passive_hoarding_blob) do
        include InactionPropaganda::Attachable

        def previewable_attachable?
          representable?
        end

        def attachable_plain_text_representation(caption = nil)
          "[#{caption || filename}]"
        end

        def to_trix_content_attachment_partial_path
          nil
        end
      end
    end

    initializer "inaction_propaganda.helper" do
      %i[action_controller_base action_mailer].each do |base|
        ActiveSupport.on_load(base) do
          helper InactionPropaganda::Engine.helpers
        end
      end
    end

    initializer "inaction_propaganda.renderer" do
      %i[action_controller_base action_mailer].each do |base|
        ActiveSupport.on_load(base) do
          around_action do |controller, action|
            InactionPropaganda::Content.with_renderer(controller, &action)
          end
        end
      end
    end

    initializer "inaction_propaganda.system_test_helper" do
      ActiveSupport.on_load(:action_dispatch_system_test_case) do
        require "inaction_propaganda/system_test_helper"
        include InactionPropaganda::SystemTestHelper
      end
    end

    initializer "inaction_propaganda.configure" do |app|
      InactionPropaganda::Attachment.tag_name = app.config.inaction_propaganda.attachment_tag_name
    end

    config.after_initialize do |app|
      if klass = app.config.inaction_propaganda.sanitizer_vendor
        InactionPropaganda::ContentHelper.sanitizer = klass.safe_list_sanitizer.new
      end
    end
  end
end
