2014-08-06 14:47:47 +02:00
|
|
|
# Event management life cycle
|
2014-06-23 23:39:42 +02:00
|
|
|
class ModerationsController < ApplicationController
|
2014-08-06 14:47:47 +02:00
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :set_moderation, :set_mailer_host, only:
|
2014-08-09 18:59:11 +02:00
|
|
|
[:show, :edit, :preview, :update, :validate, :accept, :refuse, :destroy]
|
2014-01-06 11:22:39 +01:00
|
|
|
|
|
|
|
def index
|
2014-08-06 14:47:47 +02:00
|
|
|
@events = Event.unmoderated
|
2014-01-06 11:22:39 +01:00
|
|
|
end
|
|
|
|
|
2014-08-09 18:59:11 +02:00
|
|
|
def preview
|
|
|
|
@moderation.attributes = moderation_params
|
|
|
|
@moderation.valid?
|
|
|
|
render action: :edit
|
|
|
|
end
|
|
|
|
|
2014-01-06 11:22:39 +01:00
|
|
|
# PATCH/PUT /moderations/1
|
|
|
|
# PATCH/PUT /moderations/1.json
|
|
|
|
def update
|
2014-08-06 14:47:47 +02:00
|
|
|
@older_mod = Event.new @event.attributes
|
2014-01-06 11:22:39 +01:00
|
|
|
respond_to do |format|
|
2014-08-06 14:47:47 +02:00
|
|
|
if @moderation.update(moderation_params) && send_moderation_mails
|
2014-07-01 15:50:39 +02:00
|
|
|
format.html { redirect_to moderations_path, notice: t('.ok') }
|
2014-01-06 11:22:39 +01:00
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
|
|
|
format.html { render action: 'edit' }
|
2014-08-15 19:20:50 +02:00
|
|
|
# 422 means :unprocessable_entity
|
|
|
|
format.json { render json: @moderation.errors, status: 422 }
|
2014-01-06 11:22:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-01 15:50:39 +02:00
|
|
|
# PATCH/PUT /accept/1
|
|
|
|
# PATCH/PUT /accept/1.json
|
|
|
|
def accept
|
|
|
|
respond_to do |format|
|
2014-08-06 14:47:47 +02:00
|
|
|
if @moderation.update(moderated: true) && send_accept_mails
|
2014-07-01 15:50:39 +02:00
|
|
|
format.html { redirect_to moderations_path, notice: t('.ok') }
|
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
|
|
|
format.html { render action: 'edit' }
|
2014-08-15 19:20:50 +02:00
|
|
|
# 422 means :unprocessable_entity
|
|
|
|
format.json { render json: @moderation.errors, status: 422 }
|
2014-07-01 15:50:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /events/1
|
|
|
|
# DELETE /events/1.json
|
|
|
|
def destroy
|
2014-08-06 14:47:47 +02:00
|
|
|
if @moderation.destroy && send_destroy_mails
|
2014-07-07 13:14:17 +02:00
|
|
|
EventMailer.destroy(@moderation, current_user, @reason).deliver
|
2014-07-01 15:50:39 +02:00
|
|
|
ModerationMailer.destroy(@moderation, current_user, @reason).deliver
|
2014-01-06 11:22:39 +01:00
|
|
|
end
|
2014-07-01 15:50:39 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to moderations_url, notice: t('.ok') }
|
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
|
|
|
end
|
2014-01-06 11:22:39 +01:00
|
|
|
|
2014-07-01 15:50:39 +02:00
|
|
|
private
|
2014-01-06 11:22:39 +01:00
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_moderation
|
|
|
|
@event = Event.find params[:id]
|
|
|
|
@moderation = @event
|
|
|
|
end
|
2014-06-09 12:18:40 +02:00
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
# Never trust parameters from the scary internet, only allow the white list
|
|
|
|
# through.
|
|
|
|
def moderation_params
|
|
|
|
params.require(:event)
|
2014-08-23 16:59:42 +02:00
|
|
|
.permit :title, :start_time, :end_time, :description, :address, :city,
|
|
|
|
:region, :locality, :url, :contact, :submitter, :tags
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Useful to manage absolute url in mails
|
|
|
|
def set_mailer_host
|
|
|
|
ActionMailer::Base.default_url_options[:host] = request.host_with_port
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_moderation_mails
|
|
|
|
# Send an update mail to moderators
|
|
|
|
ModerationMailer.update(@older_mod, @moderation, current_user).deliver
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_accept_mails
|
|
|
|
# Send an acceptation mail to its author
|
|
|
|
EventMailer.accept(@moderation, current_user).deliver
|
|
|
|
|
|
|
|
# Send an acceptation mail to moderators
|
|
|
|
ModerationMailer.accept(@moderation, current_user).deliver
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_destroy_mails
|
|
|
|
# Send a notification to its author
|
|
|
|
if params[:reason] == 'r_4'
|
|
|
|
@reason = params[:reason_text]
|
|
|
|
else
|
|
|
|
@reason = t "moderations.refuse.reason_#{params[:reason]}_long"
|
2014-06-09 12:18:40 +02:00
|
|
|
end
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
2014-01-06 11:22:39 +01:00
|
|
|
end
|