From 515fc49727bbd8b7988c177df29f935fd6237d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 22 Oct 2015 19:10:07 +0200 Subject: [PATCH] FIX: replace polls with a link in emails --- lib/email/message_builder.rb | 2 +- lib/email/renderer.rb | 4 ++-- lib/email/styles.rb | 5 +++-- plugins/poll/config/locales/server.en.yml | 3 +++ plugins/poll/plugin.rb | 14 +++++++++++--- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index bacc5f1cf..f80532879 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -68,7 +68,7 @@ module Email html_override.gsub!("%{unsubscribe_link}", unsubscribe_link) end - styled = Email::Styles.new(html_override) + styled = Email::Styles.new(html_override, @opts) styled.format_basic if style = @opts[:style] diff --git a/lib/email/renderer.rb b/lib/email/renderer.rb index 4f21cb8bf..9b209781f 100644 --- a/lib/email/renderer.rb +++ b/lib/email/renderer.rb @@ -16,11 +16,11 @@ module Email def html if @message.html_part - style = Email::Styles.new(@message.html_part.body.to_s) + style = Email::Styles.new(@message.html_part.body.to_s, @opts) style.format_basic style.format_html else - style = Email::Styles.new(PrettyText.cook(text)) + style = Email::Styles.new(PrettyText.cook(text), @opts) style.format_basic end diff --git a/lib/email/styles.rb b/lib/email/styles.rb index 22f9ffe1e..c8001e8da 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -6,8 +6,9 @@ module Email class Styles @@plugin_callbacks = [] - def initialize(html) + def initialize(html, opts=nil) @html = html + @opts = opts || {} @fragment = Nokogiri::HTML.fragment(@html) end @@ -146,7 +147,7 @@ module Email # this method is reserved for styles specific to plugin def plugin_styles - @@plugin_callbacks.each { |block| block.call(@fragment) } + @@plugin_callbacks.each { |block| block.call(@fragment, @opts) } end def to_html diff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.yml index 9417c9ba0..1ef088e0e 100644 --- a/plugins/poll/config/locales/server.en.yml +++ b/plugins/poll/config/locales/server.en.yml @@ -55,3 +55,6 @@ en: topic_must_be_open_to_toggle_status: "The topic must be open to toggle status." only_staff_or_op_can_toggle_status: "Only a staff member or the original poster can toggle a poll status." + + email: + link_to_poll: "Click to view the poll." diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index 3f789922d..3d7947591 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -22,9 +22,17 @@ DEFAULT_POLL_NAME ||= "poll".freeze after_initialize do - # remove "Vote Now!" & "Show Results" links in emails - Email::Styles.register_plugin_style do |fragment| - fragment.css(".poll a.cast-votes, .poll a.toggle-results").each(&:remove) + # turn polls into a link in emails + Email::Styles.register_plugin_style do |fragment, opts| + post = Post.find_by(id: opts[:post_id]) rescue nil + if post.nil? || post.trashed? + fragment.css(".poll").each(&:remove) + else + post_url = "#{Discourse.base_url}#{post.url}" + fragment.css(".poll").each do |poll| + poll.replace "

#{I18n.t("poll.email.link_to_poll")}

" + end + end end module ::DiscoursePoll