FIX: Narrative Bot certificates are ERB templates (#8174)

FIX: Narrative Bot certificates are ERB templates (#8174)

There are at least two ways of rendering templates outside of the controller. The first one is Rails way enabled with Rails 5 New feature in Rails 5: Render views outside of actions — Martian Chronicles, Evil Martians’ team blog The downside of this method is that all variables need to be passed as params (I could find a way to pass the whole context)

Another way is to use instance_eval described in Erubi documentation https://github.com/jeremyevans/erubi#usage - it works perfectly fine, however, I didn’t feel very confident about using eval unless necessary.

An additional benefit of using ApplicationController.render is that if Rails would change the ERB engine in the future, this code should still work.

If you want to test it on your local, you need to be signed in and then that two URLs are generating certificates: http://localhost:3000/discobot/certificate.svg?date=Oct+07+2019&type=standard&user_id=1 http://localhost:3000/discobot/certificate.svg?date=Oct+07+2019&type=advanced&user_id=1

Dev: https://dev.discourse.org/t/discourse-narrative-bot-should-not-be-storing-giant-strings/17130

diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb
index 75ce1ce..ad805ae 100644
--- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb
+++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb
@@ -22,545 +22,24 @@ module DiscourseNarrativeBot
 
     def new_user_track
       width = 538.583 # Default width for the SVG
-
-      svg = <<~SVG
-      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 #{width} 384.97">
-        <defs>
-          <style>

[... diff too long, it was truncated ...]

GitHub sha: e2f9b7dd

2 Likes