Using paper trail event version to present the mail diff

This commit is contained in:
echarp 2015-09-05 18:56:48 +02:00
parent be7145539c
commit 650d5b2aae
14 changed files with 39 additions and 26 deletions

View File

@ -7,7 +7,6 @@ class EventsController < ApplicationController
before_action :set_event, except: [:index, :new, :preview_create, :create] before_action :set_event, except: [:index, :new, :preview_create, :create]
before_action :set_create_event, only: [:preview_create, :create] before_action :set_create_event, only: [:preview_create, :create]
before_action :check_secret, only: [:edit, :preview, :update, :destroy] before_action :check_secret, only: [:edit, :preview, :update, :destroy]
before_action :set_old_event, only: [:update]
before_action :set_mailer_host before_action :set_mailer_host
rescue_from ActiveRecord::StaleObjectError, with: :locked rescue_from ActiveRecord::StaleObjectError, with: :locked
@ -100,10 +99,6 @@ class EventsController < ApplicationController
@event = Event.new event_params @event = Event.new event_params
end end
def set_old_event
@older_event = Event.new @event.attributes
end
# Never trust parameters from the scary internet, only allow the white list # Never trust parameters from the scary internet, only allow the white list
# through # through
def event_params def event_params
@ -128,7 +123,7 @@ class EventsController < ApplicationController
def send_update_mails def send_update_mails
# Send an update mail to moderators # Send an update mail to moderators
ModerationMailer.update(@older_event, @event, nil).deliver_now ModerationMailer.update(@event, nil).deliver_now
end end
def locked def locked

View File

@ -3,7 +3,6 @@ class ModerationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :set_moderation, :set_mailer_host, only: before_action :set_moderation, :set_mailer_host, only:
[:show, :edit, :preview, :update, :validate, :accept, :refuse, :destroy] [:show, :edit, :preview, :update, :validate, :accept, :refuse, :destroy]
before_action :set_old_mod, only: [:update]
rescue_from ActiveRecord::StaleObjectError, with: :locked rescue_from ActiveRecord::StaleObjectError, with: :locked
def index def index
@ -61,10 +60,6 @@ class ModerationsController < ApplicationController
@moderation = @event @moderation = @event
end end
def set_old_mod
@older_mod = Event.new @event.attributes
end
# Never trust parameters from the scary internet, only allow the white list # Never trust parameters from the scary internet, only allow the white list
# through. # through.
def moderation_params def moderation_params
@ -81,7 +76,7 @@ class ModerationsController < ApplicationController
def send_mails def send_mails
# Send an update mail to moderators # Send an update mail to moderators
ModerationMailer.update(@older_mod, @moderation, current_user).deliver_now ModerationMailer.update(@moderation, current_user).deliver_now
end end
def send_accept_mails def send_accept_mails

View File

@ -11,8 +11,7 @@ class ModerationMailer < ApplicationMailer
subject: event.title}" subject: event.title}"
end end
def update(older_event, event, current_user) def update(event, current_user)
@older_event = older_event
@event = event @event = event
@current_user = current_user @current_user = current_user

View File

@ -28,12 +28,12 @@ class Orga < ActiveRecord::Base
send_secret send_secret
end end
before_update do after_update do
send_secret if secret_changed? send_secret if secret_changed?
if moderated_changed? if moderated_changed?
OrgaMailer.accept(self).deliver_now! OrgaMailer.accept(self).deliver_now!
elsif previous_changes else
OrgaMailer.update(self).deliver_now! OrgaMailer.update(self).deliver_now!
end end
end end

View File

@ -4,7 +4,7 @@
new = render file: '/events/show' new = render file: '/events/show'
former = @event former = @event
@event = @older_event @event = @event.versions.last.reify
prev = render file: '/events/show' prev = render file: '/events/show'
@event = former @event = former

View File

@ -1,7 +1,18 @@
= t '.body', subject: @orga.name, author: @current_user || t('.submitter') = t '.body', subject: @orga.name, author: @current_user || t('.submitter')
\ \
= render file: '/orgas/show' :ruby
\ new = render file: '/orgas/show'
former = @orga
@orga = @orga.versions.last.reify
prev = render file: '/orgas/show'
@orga = former
require 'differ/format/patch'
Differ.format = Differ::Format::Patch
= Differ.diff new, prev
= t '.access' = t '.access'
= orga_url @orga = orga_url @orga
\ \

View File

@ -239,6 +239,7 @@ description."
links: Links links: Links
actions: Actions actions: Actions
edit: Edit edit: Edit
cancel: Delete
future: Coming future: Coming
past: In the past past: In the past
count: count:

View File

@ -242,6 +242,7 @@ description plus complète."
links: Liens links: Liens
actions: Actions actions: Actions
edit: Éditer edit: Éditer
cancel: Supprimer
future: Prochainement future: Prochainement
past: Dans le passé past: Dans le passé
count: count:

View File

@ -58,6 +58,8 @@ class ModerationsControllerTest < ActionController::TestCase
end end
test 'should update event' do test 'should update event' do
# Added so paper trail can have some bit of history
patch :update, id: @moderation, event: { title: 'hop hop' }
patch :update, id: @moderation, event: { patch :update, id: @moderation, event: {
title: @moderation.title, title: @moderation.title,
start_time: @moderation.start_time, start_time: @moderation.start_time,

View File

@ -53,7 +53,9 @@ class OrgasControllerTest < ActionController::TestCase
end end
test 'should update orga' do test 'should update orga' do
sign_in users(:one) # Necessary to have the proper paper_trail version
@orga.update_attributes name: 'My Title'
patch :update, id: @orga, orga: { name: @orga.name } patch :update, id: @orga, orga: { name: @orga.name }
assert_empty assigns(:orga).errors.messages assert_empty assigns(:orga).errors.messages

View File

@ -16,14 +16,16 @@ class ModerationMailerTest < ActionMailer::TestCase
test 'update' do test 'update' do
event = Event.last event = Event.last
older_event = Event.new event.attributes
# Added so paper trail can have some bit of history
event.save
event.tags += ' ho' event.tags += ' ho'
event.start_time += 1.day event.start_time += 1.day
event.description = event.description + ' event.description = event.description + '
hello world' hello world'
mail = ModerationMailer.update older_event, event, User.last mail = ModerationMailer.update event, User.last
assert_match(/Édition de l'événement .*/, mail.subject) assert_match(/Édition de l'événement .*/, mail.subject)
assert_equal ['moderateurs@agendadulibre.org'], mail.to assert_equal ['moderateurs@agendadulibre.org'], mail.to
assert_equal ['moderateurs@agendadulibre.org'], mail.from assert_equal ['moderateurs@agendadulibre.org'], mail.from

View File

@ -19,7 +19,10 @@ class OrgaMailerTest < ActionMailer::TestCase
end end
test 'update' do test 'update' do
mail = OrgaMailer.update Orga.last @orga = Orga.last
# Necessary to have the proper paper_trail version
@orga.update_attributes name: 'My Title'
mail = OrgaMailer.update @orga
assert_match(/Organisation .* modifiée/, mail.subject) assert_match(/Organisation .* modifiée/, mail.subject)
assert_equal [Orga.last.submitter], mail.to assert_equal [Orga.last.submitter], mail.to
assert_equal [@config.action_mailer.default_options[:from]], assert_equal [@config.action_mailer.default_options[:from]],

View File

@ -10,14 +10,13 @@ class ModerationMailerPreview < ActionMailer::Preview
def update def update
ActionMailer::Base.default_url_options[:host] = 'localhost:3000' ActionMailer::Base.default_url_options[:host] = 'localhost:3000'
event = Event.last event = Event.last
older_event = Event.new event.attributes
event.tags += ' ho' event.tags += ' ho'
event.start_time += 1.day event.start_time += 1.day
event.description = event.description + ' event.description = event.description + '
hello world' hello world'
ModerationMailer.update older_event, event, nil ModerationMailer.update event, nil
end end
# Preview this email at http://localhost:3000/rails/mailers/moderation_mailer/accept # Preview this email at http://localhost:3000/rails/mailers/moderation_mailer/accept

View File

@ -9,7 +9,10 @@ class OrgaMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/orga_mailer/update # Preview this email at http://localhost:3000/rails/mailers/orga_mailer/update
def update def update
ActionMailer::Base.default_url_options[:host] = 'localhost:3000' ActionMailer::Base.default_url_options[:host] = 'localhost:3000'
OrgaMailer.update Orga.last orga = Orga.last
orga.name += ' et hop'
orga.save!
OrgaMailer.update orga
end end
# Preview this email at http://localhost:3000/rails/mailers/orga_mailer/accept # Preview this email at http://localhost:3000/rails/mailers/orga_mailer/accept