From 6a7895095e5dd36e731fc3369f7ad4829b27d286 Mon Sep 17 00:00:00 2001 From: echarp Date: Sat, 21 May 2016 12:02:19 +0200 Subject: [PATCH] Event refusals was not properly sent to the submitter --- app/assets/javascripts/moderations.js.coffee | 4 ++-- app/controllers/application_controller.rb | 1 + app/controllers/moderations_controller.rb | 11 ++------- app/models/event.rb | 18 ++++++-------- app/models/orga.rb | 2 +- app/views/event_mailer/destroy.text.haml | 2 +- app/views/moderation_mailer/destroy.text.haml | 2 +- app/views/moderations/refuse.html.haml | 24 ++++++++++--------- config/initializers/paper_trail.rb | 1 + config/locales/views/en.yml | 2 +- config/locales/views/fr.yml | 2 +- lib/differ/format/patch.rb | 7 +++--- .../moderations_controller_test.rb | 4 ++-- test/mailers/event_mailer_test.rb | 2 +- test/mailers/moderation_mailer_test.rb | 2 +- test/mailers/previews/event_mailer_preview.rb | 2 +- .../previews/moderation_mailer_preview.rb | 2 +- test/mailers/previews/orga_mailer_preview.rb | 2 +- 18 files changed, 41 insertions(+), 49 deletions(-) create mode 100644 config/initializers/paper_trail.rb diff --git a/app/assets/javascripts/moderations.js.coffee b/app/assets/javascripts/moderations.js.coffee index 72fb8b00..773aa17c 100644 --- a/app/assets/javascripts/moderations.js.coffee +++ b/app/assets/javascripts/moderations.js.coffee @@ -1,5 +1,5 @@ $(document).ready -> $('body.moderations .field.closer input[type=radio]').click -> - $('body.moderations #reason_text').parent().slideUp() + $('body.moderations #event_reason_text').parent().slideUp() $('body.moderations .field.opener input[type=radio]').click -> - $('body.moderations #reason_text').parent().slideDown() + $('body.moderations #event_reason_text').parent().slideDown() diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1e91eca4..b4d95d9c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ # The top level controller, where can be centralised almost everything class ApplicationController < ActionController::Base + before_action :set_paper_trail_whodunnit before_action :set_locale # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 6d340967..c2b2a26c 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -44,7 +44,8 @@ class ModerationsController < ApplicationController # DELETE /events/1 # DELETE /events/1.json def destroy - @moderation.reason_for_deletion = get_reason_from_params params[:reason] + @moderation.reason = params[:event][:reason] || + t("moderations.refuse.reason_#{params[:reason]}_long") @moderation.destroy! respond_to do |format| format.html { redirect_to :moderations, notice: t('.ok') } @@ -77,12 +78,4 @@ class ModerationsController < ApplicationController def locked redirect_to edit_moderation_url(@moderation), alert: t('staleObjectError') end - - def get_reason_from_params(reason) - if reason == 'r_4' - reason - else - t "moderations.refuse.reason_#{reason}_long" - end - end end diff --git a/app/models/event.rb b/app/models/event.rb index e7e4670e..62548799 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - # This is the central ADL class, where are managed all events class Event < ActiveRecord::Base extend SimpleCalendar @@ -25,7 +23,7 @@ class Event < ActiveRecord::Base after_validation :geocode # Mechanism to store some reason which can be used when sending notifications - attr_accessor :reason_for_deletion + attr_accessor :reason after_create EventCallbacks after_update EventCallbacks @@ -71,10 +69,8 @@ class Event < ActiveRecord::Base end before_validation on: :update do - if address_changed? - self.latitude = nil - self.longitude = nil - end + self.latitude = nil if address_changed? + self.longitude = nil if address_changed? end before_create do @@ -113,7 +109,8 @@ class Event < ActiveRecord::Base def to_tweet url = Rails.application.routes.url_helpers.event_url( self, - host: ActionMailer::Base.default_url_options[:host]) + host: ActionMailer::Base.default_url_options[:host] + ) tweet = "#{self} #{url}" if tweet.size >= 140 @@ -125,8 +122,7 @@ class Event < ActiveRecord::Base private def end_after_start - return if end_time.blank? || start_time.blank? - - errors.add :end_time, :before_start if end_time <= start_time + errors.add :end_time, :before_start if + !end_time.blank? && !start_time.blank? && end_time <= start_time end end diff --git a/app/models/orga.rb b/app/models/orga.rb index 6d587a61..d9e6efb2 100644 --- a/app/models/orga.rb +++ b/app/models/orga.rb @@ -28,7 +28,7 @@ class Orga < ActiveRecord::Base scope :locality, -> {} scope :daylimit, -> {} scope :future, -> {} - scope :period, ->(year, week) {} + scope :period, ->(_year, _week) {} scope :region, ->(region) { where region: region unless region == 'all' } scope :tag, ->(tag) { where 'tags like ?', "%#{tag}%" } diff --git a/app/views/event_mailer/destroy.text.haml b/app/views/event_mailer/destroy.text.haml index a4c33362..19a578c8 100644 --- a/app/views/event_mailer/destroy.text.haml +++ b/app/views/event_mailer/destroy.text.haml @@ -1,6 +1,6 @@ = t '.body', author: @current_user \ -= @event.reason_for_deletion += @event.reason \ = t '.reclamation' \ diff --git a/app/views/moderation_mailer/destroy.text.haml b/app/views/moderation_mailer/destroy.text.haml index 1e7fa269..cb64e2d9 100644 --- a/app/views/moderation_mailer/destroy.text.haml +++ b/app/views/moderation_mailer/destroy.text.haml @@ -1,6 +1,6 @@ = t '.body', subject: @event.title, author: @current_user \ -= @event.reason_for_deletion += @event.reason \ = t '.reminder' = render file: '/events/show' diff --git a/app/views/moderations/refuse.html.haml b/app/views/moderations/refuse.html.haml index db6c9934..b24ffe42 100644 --- a/app/views/moderations/refuse.html.haml +++ b/app/views/moderations/refuse.html.haml @@ -1,35 +1,37 @@ %h2 %em.fa.fa-exclamation-triangle - =t '.title' + = t '.title' -= form_tag moderation_path(@moderation), method: :delete do |f| - .field.closer(title="#{t '.reason_r_1_long'}") += form_for @moderation, url: { action: :destroy }, + html: { method: :delete } do |f| + .field.closer{ title: t('.reason_r_1_long') } = radio_button_tag :reason, :r_1, params[:event] == 'reason_r_1' = label_tag :reason_r_1, t('.reason_r_1') - .field.closer(title="#{t '.reason_r_2_long'}") + .field.closer{ title: t('.reason_r_2_long') } = radio_button_tag :reason, :r_2, params[:event] == 'reason_r_2' = label_tag :reason_r_2, t('.reason_r_2') - .field.closer(title="#{t '.reason_r_3_long'}") + .field.closer{ title: t('.reason_r_3_long') } = radio_button_tag :reason, :r_3, params[:event] == 'reason_r_3' = label_tag :reason_r_3, t('.reason_r_3') - .field.opener(title="#{t '.reason_r_4_long'}") - = radio_button_tag :reason, :r_4, params[:event] == 'reason_r_4' || !params[:event] + .field.opener{ title: t('.reason_r_4_long') } + = radio_button_tag :reason, :r_4, + params[:event] == 'reason_r_4' || !params[:event] = label_tag :reason_r_4, t('.reason_r_4') .field - = label_tag :reason_text, t('.reason') - = text_area_tag :reason_text, nil, cols: 40, rows: 5 + = label_tag :reason + = f.text_area :reason, cols: 40, rows: 5 = link_to moderations_path do %em.fa.fa-arrow-left - =t '.ko' + = t '.ko' = button_tag do %em.fa.fa-thumbs-down - =t '.ok' + = t '.ok' %fieldset %legend diff --git a/config/initializers/paper_trail.rb b/config/initializers/paper_trail.rb new file mode 100644 index 00000000..39a66791 --- /dev/null +++ b/config/initializers/paper_trail.rb @@ -0,0 +1 @@ +PaperTrail.config.track_associations = false diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml index b14e99f1..dbc7b543 100644 --- a/config/locales/views/en.yml +++ b/config/locales/views/en.yml @@ -208,7 +208,7 @@ don't hesitate to repeat each time these informations, they are important. \nWe strongly invite you to submit again this event with a more complete description." reason_r_3_long: Your event is relevant, but has already been submitted. - reason: Your reason + reason_text: Your reason destroy: ok: Event rejected notes: diff --git a/config/locales/views/fr.yml b/config/locales/views/fr.yml index cf049e34..23955d1e 100644 --- a/config/locales/views/fr.yml +++ b/config/locales/views/fr.yml @@ -209,7 +209,7 @@ répéter à chaque fois ces informations, elles sont importantes. description plus complète." reason_r_3_long: Votre événement a tout à fait sa place ici, mais il a déjà été enregistré. - reason: Votre raison + reason_text: Votre raison destroy: ok: Événement rejeté notes: diff --git a/lib/differ/format/patch.rb b/lib/differ/format/patch.rb index c021171e..7bf62db3 100644 --- a/lib/differ/format/patch.rb +++ b/lib/differ/format/patch.rb @@ -5,12 +5,11 @@ module Differ module Patch class << self def format(change) - case - when change.change? + if change.change? as_change change - when change.delete? + elsif change.delete? as_delete change - when change.insert? + elsif change.insert? as_insert change else '' diff --git a/test/controllers/moderations_controller_test.rb b/test/controllers/moderations_controller_test.rb index 5fc71cd9..9d61e65f 100644 --- a/test/controllers/moderations_controller_test.rb +++ b/test/controllers/moderations_controller_test.rb @@ -92,7 +92,7 @@ class ModerationsControllerTest < ActionController::TestCase test 'should reject event' do assert_difference 'Event.count', -1 do - delete :destroy, id: @moderation + delete :destroy, id: @moderation, event: {} end assert_empty assigns(:moderation).errors @@ -102,7 +102,7 @@ class ModerationsControllerTest < ActionController::TestCase test 'should reject event with a reason' do assert_difference 'Event.count', -1 do - delete :destroy, id: @moderation, reason: 'r_4' + delete :destroy, id: @moderation, reason: 'r_4', event: {} end assert_empty assigns(:moderation).errors diff --git a/test/mailers/event_mailer_test.rb b/test/mailers/event_mailer_test.rb index be74ee04..c45adf97 100644 --- a/test/mailers/event_mailer_test.rb +++ b/test/mailers/event_mailer_test.rb @@ -24,7 +24,7 @@ class EventMailerTest < ActionMailer::TestCase test 'destroy' do event = Event.last - event.reason_for_deletion = 'hello world' + event.reason = 'hello world' mail = EventMailer.destroy Event.last assert_match(/Événement .* refusé/, mail.subject) assert_equal [Event.last.contact], mail.to diff --git a/test/mailers/moderation_mailer_test.rb b/test/mailers/moderation_mailer_test.rb index 1f1a30d4..654e0d9d 100644 --- a/test/mailers/moderation_mailer_test.rb +++ b/test/mailers/moderation_mailer_test.rb @@ -42,7 +42,7 @@ hello world' test 'destroy' do event = Event.unscoped.last - event.reason_for_deletion = 'hello world' + event.reason = 'hello world' mail = ModerationMailer.destroy event assert_match(/Événement .* refusé/, mail.subject) assert_equal ['moderateurs@agendadulibre.org'], mail.to diff --git a/test/mailers/previews/event_mailer_preview.rb b/test/mailers/previews/event_mailer_preview.rb index c665db26..ff8c6534 100644 --- a/test/mailers/previews/event_mailer_preview.rb +++ b/test/mailers/previews/event_mailer_preview.rb @@ -16,7 +16,7 @@ class EventMailerPreview < ActionMailer::Preview def destroy ActionMailer::Base.default_url_options[:host] = 'localhost:3000' event = Event.last - event.reason_for_deletion = 'hello world' + event.reason = 'hello world' EventMailer.destroy event end end diff --git a/test/mailers/previews/moderation_mailer_preview.rb b/test/mailers/previews/moderation_mailer_preview.rb index 1567e0ad..0131db8a 100644 --- a/test/mailers/previews/moderation_mailer_preview.rb +++ b/test/mailers/previews/moderation_mailer_preview.rb @@ -29,7 +29,7 @@ hello world' def destroy ActionMailer::Base.default_url_options[:host] = 'localhost:3000' event = Event.last - event.reason_for_deletion = 'hello world' + event.reason = 'hello world' ModerationMailer.destroy event end end diff --git a/test/mailers/previews/orga_mailer_preview.rb b/test/mailers/previews/orga_mailer_preview.rb index 6505f7ae..04c6f2cc 100644 --- a/test/mailers/previews/orga_mailer_preview.rb +++ b/test/mailers/previews/orga_mailer_preview.rb @@ -25,7 +25,7 @@ class OrgaMailerPreview < ActionMailer::Preview def destroy ActionMailer::Base.default_url_options[:host] = 'localhost:3000' orga = Orga.last - # orga.reason_for_deletion = 'hello world' + # orga.reason = 'hello world' OrgaMailer.destroy orga end end