14 changed files with 111 additions and 34 deletions
@ -1,22 +1,46 @@
|
||||
class NotesController < InheritedResources::Base |
||||
belongs_to :moderation |
||||
class NotesController < ApplicationController |
||||
before_action :set_event, only: [:new, :create] |
||||
before_filter :set_mailer_host, only: [:create] |
||||
|
||||
# GET /moderations/id/new |
||||
def new |
||||
@note = @moderation.notes.new |
||||
end |
||||
|
||||
def create |
||||
create! do |format| |
||||
format.html { |
||||
@note.author = current_user |
||||
create!(notice: t(:added, scope: [:notes, :actions])) { moderations_url } |
||||
} |
||||
@note = @moderation.notes.new(note_params) |
||||
@note.author = current_user |
||||
|
||||
respond_to do |format| |
||||
if @note.save |
||||
if (params[:envoiParMail] == 'oui') |
||||
# Send an update mail to its author |
||||
NoteMailer.create(@note).deliver |
||||
end |
||||
|
||||
format.html { redirect_to moderations_url, notice: t('.ok') } |
||||
format.json { render action: 'show', status: :created, location: @event } |
||||
else |
||||
format.html { render action: 'new' } |
||||
format.json { render json: @note.errors, status: :unprocessable_entity } |
||||
end |
||||
end |
||||
end |
||||
|
||||
private |
||||
def begin_of_association_chain |
||||
@moderation = Event.unscoped.find params[:moderation_id] |
||||
@event = @moderation |
||||
# Use callbacks to share common setup or constraints between actions. |
||||
def set_event |
||||
@event = Event.find params[:moderation_id] |
||||
@moderation = @event |
||||
end |
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through. |
||||
def note_params |
||||
params.require(:note).permit :contents |
||||
end |
||||
|
||||
def permitted_params |
||||
params.permit note: [:contents] |
||||
# Useful to manage absolute url in mails |
||||
def set_mailer_host |
||||
ActionMailer::Base.default_url_options[:host] = request.host_with_port |
||||
end |
||||
end |
||||
|
@ -0,0 +1,9 @@
|
||||
class NoteMailer < ActionMailer::Base |
||||
default from: 'moderateurs@agendadulibre.org' |
||||
|
||||
def create(note) |
||||
@note = note |
||||
|
||||
mail to: note.event.contact, subject: t('note_mailer.create.subject', subject: note.event.title) |
||||
end |
||||
end |
@ -0,0 +1,13 @@
|
||||
=t '.title' |
||||
\ |
||||
=t '.body', subject: @note.event.title, contents: @note.contents |
||||
\ |
||||
= sanitize @note.contents.html_safe, |
||||
tags: %w(p br table tr td ul ol li a strong b em i img), |
||||
attributes: %w(href src width height) |
||||
\ |
||||
=t '.edit_link' |
||||
\ |
||||
= edit_moderation_url @note.event |
||||
\ |
||||
=t '.signature' |
@ -0,0 +1,14 @@
|
||||
require 'test_helper' |
||||
|
||||
class NoteMailerTest < ActionMailer::TestCase |
||||
setup do |
||||
ActionMailer::Base.default_url_options[:host] = 'localhost:3000' |
||||
end |
||||
|
||||
test "create" do |
||||
mail = NoteMailer.create(Note.last) |
||||
assert_match(/\[Agenda du Libre\] Demande d'informations sur l'évènement .*/, mail.subject) |
||||
assert_equal [Note.last.event.contact], mail.to |
||||
assert_equal ["moderateurs@agendadulibre.org"], mail.from |
||||
end |
||||
end |
@ -0,0 +1,8 @@
|
||||
# Preview all emails at http://localhost:3000/rails/mailers/note_mailer |
||||
class NoteMailerPreview < ActionMailer::Preview |
||||
# Preview this email at http://localhost:3000/rails/mailers/note_mailer/create |
||||
def create |
||||
ActionMailer::Base.default_url_options[:host] = 'localhost:3000' |
||||
NoteMailer.create(Note.last) |
||||
end |
||||
end |
Loading…
Reference in new issue