Skip to content

Commit

Permalink
add notice by mail to cancel event
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-fuentes committed Dec 5, 2017
1 parent 37e95ea commit 4dfb528
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end
13 changes: 13 additions & 0 deletions app/mailers/event_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class EventMailer < ApplicationMailer

def cancel(event)
user = event.user
@reasons = event.reasons
@canceled_at = l event.canceled_at, format: :short
@event_title = event.title
@name = user.full_name
subject = t('mailers.cancel_event.subject', title: @event_title)
mail(to: user.email, subject: subject)
end

end
10 changes: 5 additions & 5 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Event < ActiveRecord::Base
accepts_nested_attributes_for :attachments, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :participants, reject_if: :all_blank, allow_destroy: true

enum status: { requested: 0, accepted: 1 , canceled: 4 }
enum status: { requested: 0, accepted: 1, canceled: 4 }

scope :by_title, lambda {|title| where("title ILIKE ?", "%#{title}%") }
scope :by_holders, lambda {|holder_ids|
Expand All @@ -50,10 +50,10 @@ class Event < ActiveRecord::Base
scope :published, -> { where("published_at >= ?", Time.zone.today) }

def cancel_event
if cancel == 'true' and canceled_at.nil?
self.canceled_at = Time.zone.today
self.status = 'canceled'
end
return unless cancel == 'true' && canceled_at.nil?
self.canceled_at = Time.zone.today
self.status = 'canceled'
EventMailer.cancel(self).deliver_now
end

def self.managed_by(user)
Expand Down
25 changes: 25 additions & 0 deletions app/views/event_mailer/cancel.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<td style="padding-bottom: 20px; padding-left: 10px;">
<h1 style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;">
<%= t('mailers.cancel_event.welcome', name: @name) %>
</h1>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.cancel_event.text1', title: @event_title) %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.cancel_event.text2') %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= raw @reasons %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.cancel_event.canceled_at', canceled_at: @canceled_at) %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.cancel_event.thanks') %>
</p>
</td>
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
9 changes: 9 additions & 0 deletions config/locales/mailers.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
en:
mailers:
cancel_event:
subject: "Cancelled event: %{title}"
welcome: "Hello %{name}"
text1: "The event: %{title}"
text2: "Has been cancelled for the following reason:"
canceled_at: "On %{canceled_at}"
thanks: "Regards"
9 changes: 9 additions & 0 deletions config/locales/mailers.es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
es:
mailers:
cancel_event:
subject: "Cancelado evento: %{title}"
welcome: "Hola %{name}"
text1: "El evento: %{title}"
text2: "Ha sido cancelado por la siguiente razón:"
canceled_at: "A día %{canceled_at}"
thanks: "Un saludo"
5 changes: 5 additions & 0 deletions spec/mailers/event_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe EventMailer, type: :mailer do
pending "add some examples to (or delete) #{__FILE__}"
end
9 changes: 9 additions & 0 deletions spec/mailers/previews/event_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Preview all emails at http://localhost:3000/rails/mailers/event_mailer
class EventMailerPreview < ActionMailer::Preview

def cancel
event = Event.second
EventMailer.cancel(event)
end

end

0 comments on commit 4dfb528

Please sign in to comment.