2014-08-06 14:47:47 +02:00
|
|
|
# Event life cycle
|
|
|
|
# This is a central part to this project
|
2014-06-22 22:06:12 +02:00
|
|
|
class EventsController < ApplicationController
|
2014-08-07 01:06:10 +02:00
|
|
|
before_action :set_events, only: [:index]
|
2014-08-09 18:59:11 +02:00
|
|
|
before_action :set_event, only:
|
|
|
|
[:show, :edit, :preview, :update, :cancel, :destroy]
|
|
|
|
before_action :check_secret, only:
|
|
|
|
[:edit, :preview, :update, :cancel, :destroy]
|
2014-08-06 14:47:47 +02:00
|
|
|
before_action :set_mailer_host
|
2014-10-11 16:12:07 +02:00
|
|
|
rescue_from ActiveRecord::StaleObjectError, with: :locked
|
2014-01-01 21:26:35 +01:00
|
|
|
|
2013-12-28 23:45:13 +01:00
|
|
|
def index
|
2013-12-29 20:42:00 +01:00
|
|
|
respond_to do |format|
|
2014-08-24 14:24:53 +02:00
|
|
|
format.html { render layout: 'iframe' if params[:iframe] }
|
2014-09-05 22:15:38 +02:00
|
|
|
format.rss { @events = @events.future.in params[:daylimit] }
|
2014-08-06 14:47:47 +02:00
|
|
|
format.ics { @events = @events.last_year.order :id }
|
2014-08-09 18:59:11 +02:00
|
|
|
format.xml { @events = @events.includes(:related_region).order :id }
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-22 22:06:12 +02:00
|
|
|
# GET /users/new
|
|
|
|
def new
|
|
|
|
@event = Event.new
|
2014-07-16 22:29:07 +02:00
|
|
|
@event.start_time ||= Time.now.change(min: 0) + 1.day + 1.hour
|
|
|
|
@event.end_time ||= Time.now.change(min: 0) + 1.day + 2.hour
|
2014-06-22 22:06:12 +02:00
|
|
|
end
|
|
|
|
|
2014-08-09 18:59:11 +02:00
|
|
|
# POST /events/preview
|
|
|
|
def preview_create
|
|
|
|
@event = Event.new event_params
|
|
|
|
@event.valid?
|
|
|
|
render action: :new
|
|
|
|
end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
# POST /events
|
|
|
|
# POST /events.json
|
|
|
|
def create
|
2014-07-01 15:50:39 +02:00
|
|
|
@event = Event.new event_params
|
2014-01-01 21:26:35 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
2014-08-06 14:47:47 +02:00
|
|
|
if @event.save && send_creation_mails
|
2014-06-26 21:55:23 +02:00
|
|
|
format.html { redirect_to :root, notice: t('.ok') }
|
2014-08-15 19:20:50 +02:00
|
|
|
# 201 means :created
|
|
|
|
format.json { render action: 'show', status: 201, location: @event }
|
2014-01-01 21:26:35 +01:00
|
|
|
else
|
|
|
|
format.html { render action: 'new' }
|
2014-08-15 19:20:50 +02:00
|
|
|
# 422 means :unprocessable_entity
|
|
|
|
format.json { render json: @event.errors, status: 422 }
|
2014-01-01 21:26:35 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-09 18:59:11 +02:00
|
|
|
# PATCH/PUT /events/1/preview
|
|
|
|
def preview
|
|
|
|
@event.attributes = event_params
|
|
|
|
@event.valid?
|
|
|
|
render action: :edit
|
|
|
|
end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
# PATCH/PUT /events/1
|
|
|
|
# PATCH/PUT /events/1.json
|
|
|
|
def update
|
2014-08-06 14:47:47 +02:00
|
|
|
@older_event = Event.new @event.attributes
|
2014-01-01 21:26:35 +01:00
|
|
|
respond_to do |format|
|
2014-08-06 14:47:47 +02:00
|
|
|
if @event.update(event_params) && send_update_mails
|
2014-07-01 21:11:39 +02:00
|
|
|
format.html { redirect_to :root, notice: t('.ok') }
|
2014-01-01 21:26:35 +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: @event.errors, status: 422 }
|
2014-01-01 21:26:35 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-22 22:06:12 +02:00
|
|
|
# DELETE /events/1
|
|
|
|
# DELETE /events/1.json
|
|
|
|
def destroy
|
|
|
|
@event.destroy
|
|
|
|
respond_to do |format|
|
2014-07-01 15:50:39 +02:00
|
|
|
format.html { redirect_to events_url, notice: t('.ok') }
|
2014-06-22 22:06:12 +02:00
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-28 23:45:13 +01:00
|
|
|
private
|
2014-01-01 21:26:35 +01:00
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
def set_events
|
|
|
|
@events = Event.moderated
|
2014-10-14 10:33:22 +02:00
|
|
|
@events = @events.region params[:region] if params[:region]
|
|
|
|
@events = @events.locality params[:locality] if params[:locality]
|
2014-10-13 23:55:18 +02:00
|
|
|
@events = @events.tag params[:tag] if params[:tag]
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
2014-05-25 23:59:03 +02:00
|
|
|
|
2014-10-13 23:55:18 +02:00
|
|
|
# Use callbacks to share common setup or constraints between actions
|
2014-08-06 14:47:47 +02:00
|
|
|
def set_event
|
|
|
|
if params[:secret].present?
|
|
|
|
@event = Event.where secret: params[:secret]
|
|
|
|
else
|
|
|
|
@event = Event.moderated
|
2014-05-25 23:59:03 +02:00
|
|
|
end
|
2014-08-06 14:47:47 +02:00
|
|
|
@event = @event.find params[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list
|
2014-10-13 23:55:18 +02:00
|
|
|
# through
|
2014-08-06 14:47:47 +02:00
|
|
|
def event_params
|
|
|
|
params.require(:event)
|
2014-10-11 16:12:07 +02:00
|
|
|
.permit :lock_version, :title, :start_time, :end_time, :description,
|
|
|
|
:address, :city, :region, :locality, :url, :contact, :submitter,
|
|
|
|
:tags
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
|
|
|
|
2014-08-09 18:59:11 +02:00
|
|
|
# Check that you can only edit an existing event if you know its secret
|
2014-08-06 14:47:47 +02:00
|
|
|
def check_secret
|
2014-08-09 18:59:11 +02:00
|
|
|
redirect_to :root, alert: t(:forbidden, scope: [:events, :edit]) \
|
2014-08-06 14:47:47 +02:00
|
|
|
unless params[:secret] == @event.secret
|
|
|
|
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_creation_mails
|
|
|
|
# Send an event creation mail to its author
|
|
|
|
EventMailer.create(@event).deliver
|
|
|
|
# Send a mail to moderators
|
|
|
|
ModerationMailer.create(@event).deliver
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_update_mails
|
|
|
|
# Send an update mail to moderators
|
|
|
|
ModerationMailer.update(@older_event, @event, nil).deliver
|
|
|
|
end
|
2014-10-11 16:12:07 +02:00
|
|
|
|
|
|
|
def locked
|
|
|
|
redirect_to edit_event_url(@event, secret: @event.secret),
|
|
|
|
alert: t('staleObjectError')
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|