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

84 lines
2.5 KiB
Ruby

require 'differ'
class ModerationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_moderation, only: [:show, :edit, :update, :validate, :accept, :refuse, :destroy]
before_filter :set_mailer_host, only: [:update, :accept, :destroy]
def index
@events = Event.where moderated: 0
end
# PATCH/PUT /moderations/1
# PATCH/PUT /moderations/1.json
def update
if params[:visu]
@moderation.attributes = moderation_params
render action: 'edit'
return
end
respond_to do |format|
if @moderation.update(moderation_params)
# Send an update mail to its author
ModerationMailer.update(@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
# PATCH/PUT /accept/1
# PATCH/PUT /accept/1.json
def accept
respond_to do |format|
if @moderation.update(moderated: 1)
# Send an update mail to its author
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
@reason = 'pas cool'
ModerationMailer.destroy(@moderation, current_user, @reason).deliver
end
respond_to do |format|
format.html { redirect_to moderations_url, notice: t('.ok') }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_moderation
@event = Event.find params[:id]
@moderation = @event
end
# Never trust parameters from the scary internet, only allow the white list through.
def moderation_params
params.require(:event)
.permit :title, :start_time, :end_time, :description, :city, :region, :locality, :url, :contact, :submitter, :tags
end
# Useful to manage absolute url in mails
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
end