我的天哪。显然……我可以!!!!我从来没想过我能把它放在那里,而不是放在模板的顶部(至少在我脑子里是这样渲染的)。
我现在看到并理解了 digest_custom 以及它在代码中调用 .html_safe,它很漂亮,就在我查看并试图理解的代码中,但它使用那种奇怪的无括号语法让我感到困惑(对我来说)。(或者我可能永远也看不到它)。
嗯,我确实在 locales 中看到了这些值,但与我之前覆盖模板相比,这是一个风险小得多的解决方案。
非常感谢你指明了正确的方向!
这是我还在做的另一件我觉得很傻的事情。我的 digest_custom_html 需要它被渲染的用户,所以我正在覆盖 digest 来设置 @user,这样我的 digets_custom_html 就可以访问 digest 所属的用户。有没有什么超级简单的方法可以避免这样做?
after_initialize do
# Code which should run after Rails has finished booting
# 在 Rails 完成启动后应运行的代码
# 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