digest_custom_html을 HTML로 처리하도록 수정 (기존: digest.html.erb 오버라이딩)

오, 정말… 할 수 있다니!!! 템플릿의 위쪽(최소 내 머리속에서는) 렌더링되는 부분에 있는 것이 아니라, 바로 거기에 넣을 수 있다는 생각은 전혀 하지 못했거든요.

이제 digest_custom이 .html_safe를 호출한다는 것을 보고 이해하게 되었어요. 원하시는 대로, 제가 살펴보고 이해하려고 했던 코드에서요. 다만, 그 특이한 괄호 없는 문법을 사용한다는 점이 (제게는) 혼란스럽습니다. (아니면, 그냥 제가 그걸 발견하지 못했을 수도 있겠네요).

글쎄요, 로케일(로컬라이제이션)에서 값들은 볼 수 있지만, 전에 하던 것처럼 템플릿을 오버라이드하는 것과 비교하면, 이 방법은 훨씬 위험성이 적은 해결책입니다.

제 길을 제대로 안내해 주셔서 정말 감사합니다!

또 다른 한 가지, 제가 좀 어리석게 느끼는 일이 있습니다. 제 digest_custom_html은 렌더링되는 사용자를 필요로 하므로, digest를 오버라이드하여 @user를 설정하고 있습니다. 이렇게 하면 제 digets_custom_html이 다이제스트의 대상 사용자에게 접근할 수 있습니다. 이 과정을 피할 수 있는 아주 쉬운 방법이 있을까요?

after_initialize do
  # Code which should run after Rails has finished booting

  # require_relative "lib/discourse_add_jobs_to_digest/user_notifications_helper_override"
  require_relative "lib/discourse_add_jobs_to_digest/engine"
  require_relative "lib/discourse_add_jobs_to_digest/job_api"

  require_dependency "user_notifications"
  module ::UserNotificationsHelperOverride
    def digest_custom_html(position_key)
      if position_key == "above_footer"
        DiscourseAddJobsToDigest::JobApi.get_jobs_html(@user).html_safe
      else
        super
      end
    end
  end

  UserNotificationsHelper.prepend(::UserNotificationsHelperOverride)

  module ::UserNotificationsOverride
    def digest(user, opts = {})
      @user = user
      super
    end
  end
  UserNotifications.prepend(::UserNotificationsOverride)
end