Skip to content

Commit

Permalink
Sending email when event is created or updated
Browse files Browse the repository at this point in the history
  • Loading branch information
stiancor committed Oct 3, 2013
1 parent fded202 commit 2fb12e9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class EventsController < ApplicationController
include EventsHelper

before_filter :signed_in_user
before_filter :user_is_invited, only: [:edit, :update, :destroy, :show]
Expand All @@ -24,6 +25,9 @@ def create
@event.user = current_user
if @event.save
@event.event_invites.create(user_id: current_user.id, attend_status: 'yes')
if @event.send_mail
send_email_to_all_invites(@event)
end
flash[:success] = 'Event created!'
redirect_to events_path
else
Expand All @@ -41,6 +45,9 @@ def edit
def update
if @event.update_attributes(params[:event])
@event.event_invites.create(user_id: current_user.id, attend_status: 'yes')
if @event.send_mail
send_email_to_all_invites(@event)
end
flash[:success] = 'Event was updated'
redirect_to events_path
else
Expand Down
14 changes: 14 additions & 0 deletions app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
module EventsHelper
include MailHelper

def send_email_to_all_invites(event)
all_emails = all_invites(event)
all_emails << event.user.email
Rails.logger.info("Trying to send event invite to #{all_emails.join(',')}")
send_event_invite(event, all_emails, "#{event.user.name} invited you to #{event.title}")
end

def all_invites(event)
event.users.collect { |user|
user.email
}
end
end
15 changes: 14 additions & 1 deletion app/helpers/mail_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module MailHelper

def send_simple_message (sender, recipients, message)
def send_mentioned_message(sender, recipients, message)
RestClient.post "https://api:#{ENV['MAILGUN_KEY']}@api.mailgun.net/v2/#{ENV['MAILGUN_SERVER']}/messages",
:from => "TwittoSocial <no-reply@twittosocial.com>",
:to => recipients.join(','),
Expand All @@ -20,6 +20,15 @@ def send_forgot_password_message(recipients, token)
:html => forgotten_password_message(token)
end

def send_event_invite(event, recipients, subject)
RestClient.post "https://api:#{ENV['MAILGUN_KEY']}@api.mailgun.net/v2/#{ENV['MAILGUN_SERVER']}/messages",
:from => "TwittoSocial <no-reply@twittosocial.com>",
:to => recipients.join(','),
:sender => "TwittoSocial",
:subject => subject,
:html => event_invite_message(event)
end

private

def build_mentioned_message(sender, message)
Expand All @@ -30,4 +39,8 @@ def forgotten_password_message(token)
"<html><body>Please follow <a href='#{request.protocol}#{request.host_with_port}/reset_password/#{token}' target='_blank'>this</a> link to set a new password at TwittoSocial<body></html>"
end

def event_invite_message(event)
"<html><body>#{event.user.name} invited you to #{event.title}. Check out the invite at <a href='#{request.protocol}#{request.host_with_port}/events/#{event.id}' target='_blank'>TwittoSocial</a><body></html>"
end

end
2 changes: 1 addition & 1 deletion app/helpers/micropost_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def send_email_if_registered_usernames(sender, message)
emails = get_emails(user_names)
unless emails.empty?
Rails.logger.info("Trying to send mail to #{emails.join(',')}")
send_simple_message(sender, emails, message)
send_mentioned_message(sender, emails, message)
end
end
end
Expand Down

0 comments on commit 2fb12e9

Please sign in to comment.