From e2f9b7dd6f52f7a36e2f5e3c2063f727a84e1911 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Wed, 9 Oct 2019 17:45:01 +1100 Subject: [PATCH] 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 https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions 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 --- .../certificate_generator.rb | 549 +----------------- .../templates/advanced_user.svg.erb | 56 ++ .../templates/new_user.svg.erb | 473 +++++++++++++++ 3 files changed, 543 insertions(+), 535 deletions(-) create mode 100644 plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/advanced_user.svg.erb create mode 100644 plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/new_user.svg.erb 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 75ce1cefb3d..ad805ae8d9b 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #{I18n.t('discourse_narrative_bot.new_user_narrative.title')} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #{I18n.t('discourse_narrative_bot.new_user_narrative.cert_title')} - - - #{@discobot_user.username} - - - #{@date} - - - - - - - - - #{name} - - #{logo_group(40, width, 280)} - - - - - - - - - - - - - - SVG + ApplicationController.render(inline: File.read(File.expand_path('../templates/new_user.svg.erb', __FILE__)), + assigns: { width: width, + discobot_user: @discobot_user, + date: @date, + avatar_url: base64_image_link(avatar_url), + logo_group: logo_group(55, width, 350), + name: name }) end def advanced_user_track width = 722.8 # Default width for the SVG - - <<~SVG - - - - - - - - - - - - - - - - - - - #{I18n.t('discourse_narrative_bot.advanced_user_narrative.cert_title')} - - - - - - - - - - - - - - #{@date} - - - - - - - - - #{@discobot_user.username} - - - #{name} - - #{logo_group(55, width, 350)} - - - - - - - - SVG + ApplicationController.render(inline: File.read(File.expand_path('../templates/advanced_user.svg.erb', __FILE__)), + assigns: { width: width, + discobot_user: @discobot_user, + date: @date, + avatar_url: base64_image_link(avatar_url), + logo_group: logo_group(40, width, 280), + name: name }) end private diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/advanced_user.svg.erb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/advanced_user.svg.erb new file mode 100644 index 00000000000..e5bccc08237 --- /dev/null +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/advanced_user.svg.erb @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + <%= I18n.t('discourse_narrative_bot.advanced_user_narrative.cert_title') %> + + + + + + + + + + + + + + <%= @date %> + + + + + + /> + + + <%= @discobot_user.username %> + + + <%= @name %> + + <%= @logo_group %> + + + + + + + diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/new_user.svg.erb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/new_user.svg.erb new file mode 100644 index 00000000000..298f6119c7a --- /dev/null +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/templates/new_user.svg.erb @@ -0,0 +1,473 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <%= I18n.t('discourse_narrative_bot.new_user_narrative.title') %> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <%= I18n.t('discourse_narrative_bot.new_user_narrative.cert_title') %> + + + <%= @discobot_user.username %> + + + <%= @date %> + + + + + + /> + + + <%= @name %> + + <%= @logo_group %> + + + + + + + + + + + + +