email_article_plugin_myprofile_controller.rb
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class EmailArticlePluginMyprofileController < MyProfileController
needs_profile
def send_email
if user.is_admin?(profile)
article = Article.find(params[:id])
EmailArticlePluginMyprofileController::Sender.content(article).deliver
render :text => "Email sent to queue"
else
render :status => :forbidden, :text => "Forbidden user"
end
end
class Sender < ActionMailer::Base
def content(article)
members = article.profile.members
emails = []
members.each{ |m|
emails.push(m.user.email)
}
mail(
content_type: 'text/html',
to: emails,
from: "#{article.author.user.name} <#{article.author.user.email}>",
reply_to: article.author.user.email,
subject: "[Artigo] " + article.title,
body: set_absolute_path(article.body, article.environment)
)
end
def set_absolute_path(body, environment)
parsed = Hpricot(body.to_s)
parsed.search('img[@src]').map { |i| change_element_path(i, 'src', environment) }
parsed.search('a[@href]').map { |i| change_element_path(i, 'href', environment) }
parsed.to_s
end
def change_element_path(el, attribute, environment)
fullpath = /^http/.match(el[attribute])
if not fullpath
relative_path = /\/?(.*)/.match(el[attribute])
el[attribute] = "http://#{environment.default_hostname}/#{relative_path[1]}"
end
end
end
end