דו\"ח: ניסיונות להחליף את התבנית `show.rss.erb` ובעיות שנצפו

מטרה

  • החלף את תבנית ה-Discourse המקורית ב-discourse/app/views/topics/show.rss.erb.
  • מקם תבנית מותאמת אישית בספריית התוסף: discourse-XXX/app/views/topics/show.rss.erb.
  • מטרה: לגרום לדפי RSS לטעון את תבנית התוסף במקום את התבנית המקורית.

גישות שניסיתי ותוצאותיהן

גישה 1: מיקום ישיר של התבנית המותאמת אישית בספריית התוסף

  • פעולה: הכנסת show.rss.erb המותאמת אישית ל-discourse-XXX/app/views/topics/.
  • תוצאה: התבנית המקורית עדיין בשימוש; דף ה-RSS אינו טוען את תבנית התוסף.

גישה 2: after_initialize עם prepend_view_path

after_initialize do
  class ::TopicsController
    prepend_view_path File.expand_path("../app/views", __dir__)
  end
end
  • תוצאה: תבנית התוסף מתעלמת; דף ה-RSS ממשיך להשתמש בתבנית המקורית.

גישה 3: after_initialize עם prepend_view_path המצביע על ספריית התוסף

after_initialize do
  class ::TopicsController
    prepend_view_path Rails.root.join("plugins/discourse-XXX-plugin-name/app/views")
  end
end
  • תוצאה: תבנית התוסף עדיין אינה בשימוש; דף ה-RSS ממשיך לרנדר את התבנית המקורית.

גישה 4: דריסת המתודה feed ורינדור מפורש של תבנית התוסף

class ::TopicsController
  prepend_view_path Rails.root.join("plugins/discourse-XXX/app/views")

  alias_method :original_feed, :feed
  def feed
    raise Discourse::NotFound if !Post.exists?(topic_id: params[:topic_id])

    begin
      @topic_view = TopicView.new(params[:topic_id])
    rescue Discourse::NotLoggedIn
      raise Discourse::NotFound
    rescue Discourse::InvalidAccess => ex
      deleted =
        guardian.can_see_topic?(ex.obj, false) ||
        (!guardian.can_see_topic?(ex.obj) && ex.obj&.access_topic_via_group && ex.obj.deleted_at)

      raise Discourse::NotFound.new(
        nil,
        check_permalinks: deleted,
        original_path: ex.obj.relative_url,
      )
    end

    @first_post = @topic_view.posts.first

    discourse_expires_in 1.minute
    response.headers["X-Robots-Tag"] = "noindex, nofollow"

    render file: Rails.root.join("plugins/discourse-XXX/app/views/topics/show.rss.erb"), formats: [:rss]
  end
end
  • תוצאה: נראה שתבנית התוסף נקראת, אך ביקור בדף .rss של נושא גורם לשגיאת oops.

אני מקווה שהדוח הזה מסכם בבירור את הניסיונות ואת הבעיות שנתקלתי בהן. אשמח מאוד לקבל תובנות, הצעות או פתרונות מהקהילה כדי להחליף בהצלחה את תבנית show.rss.erb. תודה מראש על עזרתכם!

  • I think you should use __FILE__ instead of __dir__
  • If you’re calling it outside of a class body it should be self.prepend_view_path

So I believe this will work

after_initialize do
  class ::TopicsController
    self.prepend_view_path File.expand_path("../app/views", __FILE__)
  end
end