From 1321eb67cf212b4e2240ee5811dd19d5c0fbfa8d Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Tue, 2 Sep 2014 18:11:35 +0300 Subject: [PATCH] Send a notification to administrators on new event --- app/mailers/event_mailer.rb | 8 +++++++ app/models/event.rb | 8 +++++++ .../new_event_notification.text.erb | 24 +++++++++++++++++++ .../20140902144335_add_admin_flag_to_users.rb | 5 ++++ 4 files changed, 45 insertions(+) create mode 100644 app/mailers/event_mailer.rb create mode 100644 app/views/event_mailer/new_event_notification.text.erb create mode 100644 db/migrate/20140902144335_add_admin_flag_to_users.rb diff --git a/app/mailers/event_mailer.rb b/app/mailers/event_mailer.rb new file mode 100644 index 0000000..c34f589 --- /dev/null +++ b/app/mailers/event_mailer.rb @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +class EventMailer < ActionMailer::Base + def new_event_notification(event) + @users = User.where(admin: true) + @event = event + mail(to: @users.map(&:email), subject: "Ново предложение за #{@event.class.model_name.human.mb_chars.downcase.to_s}: #{@event.title}") + end +end diff --git a/app/models/event.rb b/app/models/event.rb index 50312ab..3ca6fd0 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -8,4 +8,12 @@ class Event < ActiveRecord::Base belongs_to :track has_one :conference, through: :track belongs_to :user + + after_create :send_new_event_notification + + private + + def send_new_event_notification + EventMailer.new_event_notification(self).deliver + end end diff --git a/app/views/event_mailer/new_event_notification.text.erb b/app/views/event_mailer/new_event_notification.text.erb new file mode 100644 index 0000000..4b114fc --- /dev/null +++ b/app/views/event_mailer/new_event_notification.text.erb @@ -0,0 +1,24 @@ +Здравейте, + +Беше получено следното предложение за <%= @event.class.model_name.human.mb_chars.downcase.to_s %>: + +Потребител: <%= @event.user.email %> + +Заглавие: <%= @event.title %> + +Подзаглавие: <%= @event.title %> + +Поток: <%= @event.track.name %> + +Продължителност: <%= @event.length %> + +Език: <%= @event.language %> + +Резюме: +<%= @event.abstract %> + +Описание: +<%= @event.description %> + +Забележки: +<%= @event.notes %> diff --git a/db/migrate/20140902144335_add_admin_flag_to_users.rb b/db/migrate/20140902144335_add_admin_flag_to_users.rb new file mode 100644 index 0000000..557aceb --- /dev/null +++ b/db/migrate/20140902144335_add_admin_flag_to_users.rb @@ -0,0 +1,5 @@ +class AddAdminFlagToUsers < ActiveRecord::Migration + def change + add_column :users, :admin, :boolean, null: false, default: false + end +end