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

123 lines
3.5 KiB
Ruby
Raw Normal View History

# Event management life cycle
2014-06-23 23:39:42 +02:00
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 :set_old_mod, only: [:update]
rescue_from ActiveRecord::StaleObjectError, with: :locked
2014-01-06 11:22:39 +01:00
def index
@events = Event.unmoderated
2014-01-06 11:22:39 +01:00
end
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
respond_to do |format|
if @moderation.update_attributes(moderation_params) && send_mails
format.html { redirect_to :moderations, notice: t('.ok') }
2014-01-06 11:22:39 +01:00
format.json { head :no_content }
else
format.html { render action: 'edit' }
# 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
@moderation.update moderated: true
send_accept_mails
2014-07-01 15:50:39 +02:00
respond_to do |format|
format.html { redirect_to :moderations, notice: t('.ok') }
format.json { head :no_content }
2014-07-01 15:50:39 +02:00
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
send_destroy_mails if @moderation.destroy
2014-07-01 15:50:39 +02:00
respond_to do |format|
format.html { redirect_to :moderations, notice: t('.ok') }
2014-07-01 15:50:39 +02:00
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.
def set_moderation
@event = Event.find params[:id]
@moderation = @event
end
2014-06-09 12:18:40 +02:00
def set_old_mod
@older_mod = Event.new @event.attributes
end
# Never trust parameters from the scary internet, only allow the white list
# through.
def moderation_params
params.require(:event)
.permit :lock_version, :title, :start_time, :end_time, :description,
:address, :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
def send_mails
# Send an update mail to moderators
2015-01-10 01:00:06 +01:00
ModerationMailer.update(@older_mod, @moderation, current_user).deliver_now
end
def send_accept_mails
tweet
# Send an acceptation mail to its author
2015-01-10 01:00:06 +01:00
EventMailer.accept(@moderation, current_user).deliver_now
# Send an acceptation mail to moderators
2015-01-10 01:00:06 +01:00
ModerationMailer.accept(@moderation, current_user).deliver_now
end
# Tweet this event, if configured using apache/system variables!
def tweet
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_SECRET']
end
client.update "#{@event.to_tweet event_url(@event)}" if client.consumer_key
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
2015-01-10 01:00:06 +01:00
EventMailer.destroy(@moderation, current_user, @reason).deliver_now
ModerationMailer.destroy(@moderation, current_user, @reason).deliver_now
end
def locked
redirect_to edit_moderation_url(@moderation), alert: t('staleObjectError')
end
2014-01-06 11:22:39 +01:00
end