From a2dc4144e7c7c887fd9ef7a8b135b8f77f670c3b Mon Sep 17 00:00:00 2001 From: echarp Date: Sat, 17 Sep 2016 19:41:29 +0200 Subject: [PATCH] Better management of a textual reason when destroying moderated events --- app/assets/stylesheets/form.sass | 2 ++ app/controllers/events_controller.rb | 1 + app/controllers/moderations_controller.rb | 17 ++++++++--------- app/views/events/cancel.html.haml | 4 ++++ app/views/moderations/refuse.html.haml | 4 ++-- config/locales/models/en.yml | 1 + config/locales/models/fr.yml | 1 + test/controllers/events_controller_test.rb | 9 +++++---- test/controllers/moderations_controller_test.rb | 2 +- 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/assets/stylesheets/form.sass b/app/assets/stylesheets/form.sass index e767dc29..d6a609e8 100644 --- a/app/assets/stylesheets/form.sass +++ b/app/assets/stylesheets/form.sass @@ -82,6 +82,8 @@ content: $fa-var-tags .field.active label:before content: $fa-var-heartbeat + .field.reason label:before + content: $fa-var-comment .field.kind > label:before, th.kind:before content: $fa-var-cubes diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 5da637cd..49c67c52 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -74,6 +74,7 @@ class EventsController < ApplicationController # DELETE /events/1 # DELETE /events/1.json def destroy + @event.reason = params[:event][:reason] @event.destroy respond_to do |format| format.html { redirect_to :root, notice: t('.ok') } diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 4455b479..7d5ba4b9 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -3,6 +3,7 @@ class ModerationsController < ApplicationController before_action :authenticate_user! before_action :set_moderation, :set_mailer_host, only: [:show, :edit, :preview, :update, :validate, :accept, :refuse, :destroy] + before_action :generate_destroy_reason, only: :destroy rescue_from ActiveRecord::StaleObjectError, with: :locked def index @@ -44,7 +45,6 @@ class ModerationsController < ApplicationController # DELETE /events/1 # DELETE /events/1.json def destroy - @moderation.reason = generate_destroy_reason(params) @moderation.destroy! respond_to do |format| format.html { redirect_to :moderations, notice: t('.ok') } @@ -54,14 +54,6 @@ class ModerationsController < ApplicationController private - def generate_destroy_reason(params) - if params[:reason] == 'r_4' - params[:event][:reason] - else - t "moderations.refuse.reason_#{params[:reason]}_long" - end - end - # Use callbacks to share common setup or constraints between actions. def set_moderation @event = Event.find params[:id] @@ -85,4 +77,11 @@ class ModerationsController < ApplicationController def locked redirect_to edit_moderation_url(@moderation), alert: t('staleObjectError') end + + def generate_destroy_reason + @moderation.reason = params[:event][:reason] + if params[:reason] != 'r_4' + @moderation.reason = t "moderations.refuse.reason_#{params[:reason]}_long" + end + end end diff --git a/app/views/events/cancel.html.haml b/app/views/events/cancel.html.haml index 54353772..516400ec 100644 --- a/app/views/events/cancel.html.haml +++ b/app/views/events/cancel.html.haml @@ -10,6 +10,10 @@ = hidden_field_tag :secret, params[:secret] %h2= t '.confirm' + .field.reason + = f.label :reason + = f.text_area :reason, cols: 40, rows: 5 + = f.submit t('.ok'), name: :yes = link_to t('.ko'), @event, class: :button diff --git a/app/views/moderations/refuse.html.haml b/app/views/moderations/refuse.html.haml index a7d01977..194a75ab 100644 --- a/app/views/moderations/refuse.html.haml +++ b/app/views/moderations/refuse.html.haml @@ -21,8 +21,8 @@ params[:event] == 'reason_r_4' || !params[:event] = label_tag :reason_r_4, t('.reason_r_4') - .field - = label_tag :reason + .field.reason + = f.label :reason = f.text_area :reason, cols: 40, rows: 5 = link_to moderations_path do diff --git a/config/locales/models/en.yml b/config/locales/models/en.yml index 9145dac5..7ebb0dc2 100644 --- a/config/locales/models/en.yml +++ b/config/locales/models/en.yml @@ -82,6 +82,7 @@ en: submitter: Submitter submission_time: Waiting for tags: Tags + reason: Reason region: name_values: all: All regions diff --git a/config/locales/models/fr.yml b/config/locales/models/fr.yml index 44b14492..684df854 100644 --- a/config/locales/models/fr.yml +++ b/config/locales/models/fr.yml @@ -82,6 +82,7 @@ fr: submitter: Soumetteur submission_time: En attente depuis tags: Mots-clés + reason: Raison region: name_values: all: Toutes les régions diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb index 4bb6937f..20a8a03b 100644 --- a/test/controllers/events_controller_test.rb +++ b/test/controllers/events_controller_test.rb @@ -26,9 +26,7 @@ class EventsControllerTest < ActionController::TestCase start_time: @event.start_time, end_time: @event.end_time, description: @event.description, city: @event.city, region_id: @event.region.id, - url: @event.url, - contact: @event.contact, - tags: @event.tags + url: @event.url, contact: @event.contact, tags: @event.tags } assert_empty assigns(:event).errors @@ -122,9 +120,12 @@ class EventsControllerTest < ActionController::TestCase test 'should destroy event' do assert_difference('Event.count', -1) do - delete :destroy, id: @event, secret: @event.secret + delete :destroy, id: @event, secret: @event.secret, event: { + reason: 'bye' + } end + assert_equal 'bye', assigns(:event).reason assert_redirected_to :root end end diff --git a/test/controllers/moderations_controller_test.rb b/test/controllers/moderations_controller_test.rb index 120987c7..63f26e02 100644 --- a/test/controllers/moderations_controller_test.rb +++ b/test/controllers/moderations_controller_test.rb @@ -106,7 +106,7 @@ class ModerationsControllerTest < ActionController::TestCase delete :destroy, id: @moderation, reason: 'r_4', event: { reason: 'bye' } end - assert_not_empty assigns(:moderation).reason + assert_equal 'bye', assigns(:moderation).reason assert_empty assigns(:moderation).errors assert_redirected_to :moderations