Class: Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Post
- Defined in:
- app/models/post.rb
Class Method Summary collapse
-
.create_from_message(message) ⇒ Object
This creates a new post from the information given in the ‘message`.
-
.mail_encoding(mail) ⇒ Object
In order to do the encoding conversion properly, we have to find out the former encoding from the mail header.
Instance Method Summary collapse
- #author ⇒ Object
-
#author=(author) ⇒ Object
This allows to set the author either as email or as email string.
-
#mailing_list_footer ⇒ Object
This method returns a mail footer, which may be added to the messages delivered via email.
-
#message_for_email_delivery_to_user(user) ⇒ Object
This method returns the modified message, ready for delivery via email to the specified user.
-
#messages_to_deliver_to_mailing_list_members ⇒ Object
Each post may be delivered to all group members via email.
-
#modified_subject ⇒ Object
This method returns the modified subject, which is used by the Group Mail Feature.
-
#set_group_by_email_address(email_address) ⇒ Object
This sets the group this message belongs to, identifying the group by its email token.
-
#text_without_html_tags ⇒ Object
This returns the text attribute, i.e.
Methods inherited from ActiveRecord::Base
Class Method Details
.create_from_message(message) ⇒ Object
This creates a new post from the information given in the ‘message`. `message` is a `Mail::Message` object (from `ActionMailer::Base`. See spec/factories/mail_message.rb.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/post.rb', line 10 def self.() # http://stackoverflow.com/questions/4868205 body = (.html_part || .text_part || ).body_in_utf8 # extract the content of the body tag if necessary if body.include?('<body') doc = Nokogiri::HTML( body ) body = doc.xpath( '//body' ).first.inner_html end new_post = self.create(subject: .subject, text: body, sent_at: .date) new_post. = .from.first new_post.set_group_by_email_address(.to.first) new_post. = new_post.save return new_post end |
.mail_encoding(mail) ⇒ Object
In order to do the encoding conversion properly, we have to find out the former encoding from the mail header.
parameter: Mail object #<Mail::Part:-570274288, Multipart: false, Headers: <Content-Type: text/html; charset=windows-1252>,
<Content-Transfer-Encoding: quoted-printable>>
66 67 68 |
# File 'app/models/post.rb', line 66 def self.mail_encoding(mail) mail.inspect.to_s.scan(/.charset=(.*)>./)[0][0].split(">").first if mail end |
Instance Method Details
#author ⇒ Object
46 47 48 |
# File 'app/models/post.rb', line 46 def super || end |
#author=(author) ⇒ Object
This allows to set the author either as email or as email string.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/post.rb', line 33 def () if .kind_of? User super() elsif .kind_of? String users_by_email = User.find_all_by_email() user_by_email = users_by_email.first if users_by_email.count == 1 if user_by_email super(user_by_email) else self. = end end end |
#mailing_list_footer ⇒ Object
This method returns a mail footer, which may be added to the messages delivered via email. The footer contains, e.g. a link to the group’s site.
116 117 118 119 120 121 |
# File 'app/models/post.rb', line 116 def "\n\n\n" + "_____________________________________\n" + I18n.t(:this_message_has_been_deliverd_through_mailing_list, group_name: self.group.name ) + "\n" + self.group.url + "\n" end |
#message_for_email_delivery_to_user(user) ⇒ Object
This method returns the modified message, ready for delivery via email to the specified user.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/post.rb', line 126 def ( user ) # use the stored message as template = self. # modify the subject according to the group's name .subject = self.modified_subject # modify the envelope_to field, but keep the to field as it is. # Thereby, the group mail address is shown in the mail programs # as recipient. .smtp_envelope_to = user.email # add the footer for each part .add_to_body self. return end |
#messages_to_deliver_to_mailing_list_members ⇒ Object
Each post may be delivered to all group members via email. (“Group Mail Feature”). This method returns the message to deliver to the group members. This is done separately (i.e. one user at a time) in order to (a) not reveal the email addresses, and (b) avoid being caught by a spam filter.
Calling this method will produce, *not deliver* the mail messages.
91 92 93 94 95 |
# File 'app/models/post.rb', line 91 def self.group.descendant_users.collect do |user| (user) end end |
#modified_subject ⇒ Object
This method returns the modified subject, which is used by the Group Mail Feature. Give a post subject ‘My Fancy Subject“ and the post’s group’s name being ”Test Group“, this mehtod returns ”[Test Group] My Fancy Subject“.
If the subject already contains the prefix, like in “Re: [Test Group] My Fancy Subject”, of cause, the prefix isn’t added, twice.
104 105 106 107 108 109 110 111 |
# File 'app/models/post.rb', line 104 def modified_subject prefix = "[#{self.group.name}] " if self.subject.include? prefix return subject else return prefix + subject end end |
#set_group_by_email_address(email_address) ⇒ Object
This sets the group this message belongs to, identifying the group by its email token.
52 53 54 55 56 57 |
# File 'app/models/post.rb', line 52 def set_group_by_email_address(email_address) token = email_address.split("@").first groups = Group.select { |group| group.name && group.name.parameterize == token } # TODO Make this efficient, e.g. by email_token attribute self.group = groups.first if groups.count == 1 end |
#text_without_html_tags ⇒ Object
This returns the text attribute, i.e. the message body, without html tags, which could be used in block quotes, where only an excerpt of the message is shown. (Use this to avoid opened but not closed html tags.)
75 76 77 78 |
# File 'app/models/post.rb', line 75 def # http://stackoverflow.com/questions/7414267/strip-html-from-string-ruby-on-rails HTML::FullSanitizer.new.sanitize(self.text) end |