From 5e5a60c29a4e1c61c6f1a255218f56a70270e099 Mon Sep 17 00:00:00 2001 From: echarp Date: Mon, 23 Jun 2014 23:39:42 +0200 Subject: [PATCH] Mail d'envoi de demande d'information --- app/controllers/events_controller.rb | 4 -- app/controllers/moderations_controller.rb | 8 ++-- app/controllers/notes_controller.rb | 48 +++++++++++++++----- app/mailers/event_mailer.rb | 2 +- app/mailers/moderation_mailer.rb | 7 +-- app/mailers/note_mailer.rb | 9 ++++ app/views/moderations/index.html.haml | 2 +- app/views/note_mailer/create.text.haml | 13 ++++++ app/views/notes/_form.html.haml | 1 + config/locales/fr.yml | 15 +++++- test/mailers/event_mailer_test.rb | 2 +- test/mailers/moderation_mailer_test.rb | 12 ++--- test/mailers/note_mailer_test.rb | 14 ++++++ test/mailers/previews/note_mailer_preview.rb | 8 ++++ 14 files changed, 111 insertions(+), 34 deletions(-) create mode 100644 app/mailers/note_mailer.rb create mode 100644 app/views/note_mailer/create.text.haml create mode 100644 test/mailers/note_mailer_test.rb create mode 100644 test/mailers/previews/note_mailer_preview.rb diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 407fa37c..7d347091 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -100,10 +100,6 @@ class EventsController < ApplicationController end private - def permitted_params - params.require(:event).permit(:title, :start_time, :end_time, :description, :city, :locality, :url, :contact, :submitter, :tags) - end - # Use callbacks to share common setup or constraints between actions. def set_event if (params[:secret].present?) diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 894f0dba..e7472c3e 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -1,12 +1,12 @@ require 'differ' -class ModerationsController < InheritedResources::Base +class ModerationsController < ApplicationController before_filter :authenticate_user! before_action :set_event, only: [:show, :edit, :update, :destroy] before_filter :set_mailer_host, only: [:update, :destroy] def index - @events = Event.unscoped.where moderated: 0 + @events = Event.where moderated: 0 end # PATCH/PUT /moderations/1 @@ -37,12 +37,12 @@ class ModerationsController < InheritedResources::Base private def permitted_params - params.permit event: [:title, :start_time, :end_time, :description, :city, :locality, :url, :contact, :submitter, :tags] + params.require(:event).permit(:title, :start_time, :end_time, :description, :city, :locality, :url, :contact, :submitter, :tags) end # Use callbacks to share common setup or constraints between actions. def set_event - @event = Event.unscoped.find params[:id] + @event = Event.find params[:id] @moderation = @event end diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index e6389e53..71844df2 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -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 - def permitted_params - params.permit note: [:contents] + # Never trust parameters from the scary internet, only allow the white list through. + def note_params + params.require(:note).permit :contents + end + + # Useful to manage absolute url in mails + def set_mailer_host + ActionMailer::Base.default_url_options[:host] = request.host_with_port end end diff --git a/app/mailers/event_mailer.rb b/app/mailers/event_mailer.rb index 26c6cb81..2ae26cb5 100644 --- a/app/mailers/event_mailer.rb +++ b/app/mailers/event_mailer.rb @@ -3,6 +3,6 @@ class EventMailer < ActionMailer::Base def create(event) @event = event - mail to: "to@example.org", subject: t('event_mailer.create.subject', subject: event.title) + mail to: event.contact, subject: t('event_mailer.create.subject', subject: event.title) end end diff --git a/app/mailers/moderation_mailer.rb b/app/mailers/moderation_mailer.rb index 7fab85b1..03fb968d 100644 --- a/app/mailers/moderation_mailer.rb +++ b/app/mailers/moderation_mailer.rb @@ -1,5 +1,6 @@ class ModerationMailer < ActionMailer::Base default from: 'moderateurs@agendadulibre.org' + TO = 'moderateurs@agendadulibre.org' # Subject can be set in your I18n file at config/locales/en.yml # with the following lookup: @@ -9,7 +10,7 @@ class ModerationMailer < ActionMailer::Base def create(event) @event = event - mail to: "to@example.org", subject: t('moderation_mailer.create.subject', subject: event.title) + mail to: TO, subject: t('moderation_mailer.create.subject', subject: event.title) end # Subject can be set in your I18n file at config/locales/en.yml @@ -21,7 +22,7 @@ class ModerationMailer < ActionMailer::Base @event = event @current_user = current_user - mail to: "to@example.org", subject: t('moderation_mailer.update.subject', subject: event.title) + mail to: TO, subject: t('moderation_mailer.update.subject', subject: event.title) end # Subject can be set in your I18n file at config/locales/en.yml @@ -33,6 +34,6 @@ class ModerationMailer < ActionMailer::Base @event = event @current_user = current_user - mail to: "to@example.org", subject: t('moderation_mailer.moderate.subject', subject: event.title) + mail to: TO, subject: t('moderation_mailer.moderate.subject', subject: event.title) end end diff --git a/app/mailers/note_mailer.rb b/app/mailers/note_mailer.rb new file mode 100644 index 00000000..e0d6364c --- /dev/null +++ b/app/mailers/note_mailer.rb @@ -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 diff --git a/app/views/moderations/index.html.haml b/app/views/moderations/index.html.haml index a295bc9c..55b39cca 100644 --- a/app/views/moderations/index.html.haml +++ b/app/views/moderations/index.html.haml @@ -38,7 +38,7 @@ \- = link_to t('refuse'), edit_moderation_path(event), class: 'fa-thumbs-down' %br/ - = link_to t('.askInfos'), edit_moderation_path(event), class: 'fa-bullhorn' + = link_to t('.askInfos'), new_moderation_note_path(event, envoiParMail: :oui), class: 'fa-bullhorn' %br/ = link_to t('.createNote'), new_moderation_note_path(event), class: 'fa-files-o' diff --git a/app/views/note_mailer/create.text.haml b/app/views/note_mailer/create.text.haml new file mode 100644 index 00000000..e7715e04 --- /dev/null +++ b/app/views/note_mailer/create.text.haml @@ -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' diff --git a/app/views/notes/_form.html.haml b/app/views/notes/_form.html.haml index 6fc401c3..cee45c48 100644 --- a/app/views/notes/_form.html.haml +++ b/app/views/notes/_form.html.haml @@ -1,4 +1,5 @@ = form_for [:moderation, @note] do |f| + = hidden_field_tag :envoiParMail, params[:envoiParMail] - if @note.errors.any? #error_explanation %h2= "#{pluralize(@note.errors.count, "error")} prohibited this note from being saved:" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index efb86603..e2a08842 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -178,8 +178,8 @@ fr: new: back: Modération title: Ajout d'une note de modération - actions: - added: La note a bien été ajoutée, merci! + create: + ok: La note a bien été ajoutée, merci! maps: index: title: Carte des évènements @@ -220,3 +220,14 @@ fr: title: Bonjour, body: L'évènement a été modéré par %{author} signature: "-- \nL'équipe de modération" + note_mailer: + create: + subject: "[Agenda du Libre] Demande d'informations sur l'évènement '%{subject}'" + title: Bonjour, + body: "Nous avons bien reçu votre proposition d'évènement '%{subject}', +\net celui-ci a toute sa place dans l'Agenda du Libre. Néanmoins, avant +\nd'être validé, nous avons besoin de quelques informations +\ncomplémentaires sur cet évènement:" + edit_link: "Nous vous invitons à ajouter ces informations en éditant directement +\nl'évènement à l'adresse suivante:" + signature: "Avec tous nos remerciements pour votre contribution,\n\n-- \nL'équipe des modérateurs de l'Agenda du Libre" diff --git a/test/mailers/event_mailer_test.rb b/test/mailers/event_mailer_test.rb index 0731444b..41cb15ce 100644 --- a/test/mailers/event_mailer_test.rb +++ b/test/mailers/event_mailer_test.rb @@ -8,7 +8,7 @@ class EventMailerTest < ActionMailer::TestCase test "create" do mail = EventMailer.create(Event.last) assert_match(/\[Agenda du Libre\] Votre évènement: .* est en attente de modération/, mail.subject) - assert_equal ["to@example.org"], mail.to + assert_equal [Event.last.contact], mail.to assert_equal ["moderateurs@agendadulibre.org"], mail.from assert_match(/Bonjour.*/, mail.body.encoded) end diff --git a/test/mailers/moderation_mailer_test.rb b/test/mailers/moderation_mailer_test.rb index 0459dc46..2424c072 100644 --- a/test/mailers/moderation_mailer_test.rb +++ b/test/mailers/moderation_mailer_test.rb @@ -6,23 +6,23 @@ class ModerationMailerTest < ActionMailer::TestCase end test "create" do - mail = ModerationMailer.create(Event.last) + mail = ModerationMailer.create(Event.unscoped.last) assert_match(/\[Agenda du Libre\] Nouvel évènement à modérer: .*/, mail.subject) - assert_equal ["to@example.org"], mail.to + assert_equal ["moderateurs@agendadulibre.org"], mail.to assert_equal ["moderateurs@agendadulibre.org"], mail.from end test "update" do - mail = ModerationMailer.update(Event.last, User.last) + mail = ModerationMailer.update(Event.unscoped.last, User.last) assert_match(/\[Agenda du Libre\] Édition de l'évènement .*/, mail.subject) - assert_equal ["to@example.org"], mail.to + assert_equal ["moderateurs@agendadulibre.org"], mail.to assert_equal ["moderateurs@agendadulibre.org"], mail.from end test "moderate" do - mail = ModerationMailer.moderate(Event.last, User.last) + mail = ModerationMailer.moderate(Event.unscoped.last, User.last) assert_match(/\[Agenda du Libre\] Évènement .* modéré/, mail.subject) - assert_equal ["to@example.org"], mail.to + assert_equal ["moderateurs@agendadulibre.org"], mail.to assert_equal ["moderateurs@agendadulibre.org"], mail.from end end diff --git a/test/mailers/note_mailer_test.rb b/test/mailers/note_mailer_test.rb new file mode 100644 index 00000000..8100284d --- /dev/null +++ b/test/mailers/note_mailer_test.rb @@ -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 diff --git a/test/mailers/previews/note_mailer_preview.rb b/test/mailers/previews/note_mailer_preview.rb new file mode 100644 index 00000000..70a91990 --- /dev/null +++ b/test/mailers/previews/note_mailer_preview.rb @@ -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