FIX: replace polls with a link in emails

This commit is contained in:
Régis Hanol 2015-10-22 19:10:07 +02:00
parent 57fc1e5e0c
commit 515fc49727
5 changed files with 20 additions and 8 deletions

View file

@ -68,7 +68,7 @@ module Email
html_override.gsub!("%{unsubscribe_link}", unsubscribe_link) html_override.gsub!("%{unsubscribe_link}", unsubscribe_link)
end end
styled = Email::Styles.new(html_override) styled = Email::Styles.new(html_override, @opts)
styled.format_basic styled.format_basic
if style = @opts[:style] if style = @opts[:style]

View file

@ -16,11 +16,11 @@ module Email
def html def html
if @message.html_part 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_basic
style.format_html style.format_html
else else
style = Email::Styles.new(PrettyText.cook(text)) style = Email::Styles.new(PrettyText.cook(text), @opts)
style.format_basic style.format_basic
end end

View file

@ -6,8 +6,9 @@ module Email
class Styles class Styles
@@plugin_callbacks = [] @@plugin_callbacks = []
def initialize(html) def initialize(html, opts=nil)
@html = html @html = html
@opts = opts || {}
@fragment = Nokogiri::HTML.fragment(@html) @fragment = Nokogiri::HTML.fragment(@html)
end end
@ -146,7 +147,7 @@ module Email
# this method is reserved for styles specific to plugin # this method is reserved for styles specific to plugin
def plugin_styles def plugin_styles
@@plugin_callbacks.each { |block| block.call(@fragment) } @@plugin_callbacks.each { |block| block.call(@fragment, @opts) }
end end
def to_html def to_html

View file

@ -55,3 +55,6 @@ en:
topic_must_be_open_to_toggle_status: "The topic must be open to toggle status." 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." 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."

View file

@ -22,9 +22,17 @@ DEFAULT_POLL_NAME ||= "poll".freeze
after_initialize do after_initialize do
# remove "Vote Now!" & "Show Results" links in emails # turn polls into a link in emails
Email::Styles.register_plugin_style do |fragment| Email::Styles.register_plugin_style do |fragment, opts|
fragment.css(".poll a.cast-votes, .poll a.toggle-results").each(&:remove) 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 "<p><a href='#{post_url}'>#{I18n.t("poll.email.link_to_poll")}</a></p>"
end
end
end end
module ::DiscoursePoll module ::DiscoursePoll