agenda-libre-ruby/app/controllers/moderations_controller.rb

94 lines
2.9 KiB
Ruby
Raw Normal View History

2014-06-09 12:18:40 +02:00
require 'differ'
2014-06-23 23:39:42 +02:00
class ModerationsController < ApplicationController
before_filter :authenticate_user!
2014-07-01 15:50:39 +02:00
before_action :set_moderation, only: [:show, :edit, :update, :validate, :accept, :refuse, :destroy]
before_filter :set_mailer_host, only: [:update, :accept, :destroy]
2014-01-06 11:22:39 +01:00
def index
2014-06-23 23:39:42 +02:00
@events = Event.where moderated: 0
2014-01-06 11:22:39 +01:00
end
# PATCH/PUT /moderations/1
# PATCH/PUT /moderations/1.json
def update
olderModeration = Event.new @event.attributes
2014-01-06 11:22:39 +01:00
if params[:visu]
2014-07-01 15:50:39 +02:00
@moderation.attributes = moderation_params
2014-01-06 11:22:39 +01:00
render action: 'edit'
return
end
respond_to do |format|
2014-07-01 15:50:39 +02:00
if @moderation.update(moderation_params)
# Send an update mail to moderators
ModerationMailer.update(olderModeration, @moderation, current_user).deliver
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-07-01 15:50:39 +02:00
format.json { render json: @moderation.errors, status: :unprocessable_entity }
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|
if @moderation.update(moderated: 1)
# Send an acceptation mail to its author
EventMailer.accept(@moderation, current_user).deliver
# Send an acceptation mail to moderators
2014-07-01 15:50:39 +02:00
ModerationMailer.accept(@moderation, current_user).deliver
format.html { redirect_to moderations_path, notice: t('.ok') }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @moderation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
if @moderation.destroy
# Send a notification to its author
if params[:reason] == 'r_4'
@reason = params[:reason_text]
else
@reason = t("reason_#{params[:reason]}_long", scope: [:moderations, :refuse])
end
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
# Use callbacks to share common setup or constraints between actions.
2014-07-01 15:50:39 +02:00
def set_moderation
2014-06-23 23:39:42 +02:00
@event = Event.find params[:id]
2014-01-06 11:22:39 +01:00
@moderation = @event
end
# Never trust parameters from the scary internet, only allow the white list through.
2014-07-01 15:50:39 +02:00
def moderation_params
2014-01-06 11:22:39 +01:00
params.require(:event)
2014-07-01 15:50:39 +02:00
.permit :title, :start_time, :end_time, :description, :city, :region, :locality, :url, :contact, :submitter, :tags
2014-01-06 11:22:39 +01:00
end
2014-06-09 12:18:40 +02:00
# Useful to manage absolute url in mails
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
2014-01-06 11:22:39 +01:00
end