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
|
2017-07-28 00:35:14 +02:00
|
|
|
has_scope :region, :locality, :tag, :daylimit, :year
|
|
|
|
has_scope :near, type: :hash, using: %i[location distance]
|
2014-11-05 21:25:18 +01:00
|
|
|
|
2014-08-07 01:06:10 +02:00
|
|
|
before_action :set_events, only: [:index]
|
2017-04-22 20:01:47 +02:00
|
|
|
before_action :set_event, except: %i[index new preview_create create]
|
2018-03-02 10:23:16 +01:00
|
|
|
before_action :set_create_event, only: %i[new preview_create create]
|
2017-04-22 20:01:47 +02:00
|
|
|
before_action :check_secret, only: %i[edit preview update 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|
|
2016-04-17 15:34:04 +02:00
|
|
|
format.html
|
2018-07-14 16:40:01 +02:00
|
|
|
format.ics { @events = @events.future }
|
2018-07-14 17:32:11 +02:00
|
|
|
format.json { @events = @events.future }
|
2016-09-03 03:41:11 +02:00
|
|
|
format.rss { @events = filter_for_rss }
|
2016-04-16 23:08:03 +02:00
|
|
|
format.xml
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-28 17:28:05 +01:00
|
|
|
# GET /events/new
|
2018-03-02 10:23:16 +01:00
|
|
|
def new; end
|
2014-06-22 22:06:12 +02:00
|
|
|
|
2014-08-09 18:59:11 +02:00
|
|
|
# POST /events/preview
|
|
|
|
def preview_create
|
|
|
|
@event.valid?
|
|
|
|
render action: :new
|
|
|
|
end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
# POST /events
|
|
|
|
# POST /events.json
|
|
|
|
def create
|
|
|
|
respond_to do |format|
|
2015-10-22 21:14:36 +02:00
|
|
|
if @event.save
|
2014-06-26 21:55:23 +02:00
|
|
|
format.html { redirect_to :root, notice: t('.ok') }
|
2018-05-23 09:24:42 +02:00
|
|
|
format.json { render action: :show, status: :created, location: @event }
|
2014-01-01 21:26:35 +01:00
|
|
|
else
|
2018-05-23 09:24:42 +02:00
|
|
|
format.html { render action: :new }
|
2018-04-05 22:00:07 +02:00
|
|
|
status = :unprocessable_entity
|
|
|
|
format.json { render json: @event.errors, status: status }
|
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
|
|
|
|
|
2018-01-01 17:52:33 +01:00
|
|
|
def edit; end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
# PATCH/PUT /events/1
|
|
|
|
# PATCH/PUT /events/1.json
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
2015-10-22 21:14:36 +02:00
|
|
|
if @event.update event_params
|
2018-03-17 16:53:38 +01:00
|
|
|
format.html { redirect_to @event, notice: t('.ok') }
|
2014-01-01 21:26:35 +01:00
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
2018-05-23 09:24:42 +02:00
|
|
|
format.html { render action: :edit }
|
2018-04-05 22:00:07 +02:00
|
|
|
status = :unprocessable_entity
|
|
|
|
format.json { render json: @event.errors, status: status }
|
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
|
2016-09-17 19:41:29 +02:00
|
|
|
@event.reason = params[:event][:reason]
|
2014-06-22 22:06:12 +02:00
|
|
|
@event.destroy
|
|
|
|
respond_to do |format|
|
2014-11-05 21:25:18 +01:00
|
|
|
format.html { redirect_to :root, 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
|
2014-11-05 21:25:18 +01:00
|
|
|
@events = apply_scopes Event.moderated
|
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
|
2016-02-13 17:24:23 +01:00
|
|
|
@event = Event.moderated
|
|
|
|
@event = Event.where secret: params[:secret] if params[:secret].present?
|
2014-08-06 14:47:47 +02:00
|
|
|
@event = @event.find params[:id]
|
|
|
|
end
|
|
|
|
|
2014-11-05 21:25:18 +01:00
|
|
|
def set_create_event
|
2018-03-02 10:23:16 +01:00
|
|
|
time = Time.zone.now.change(min: 0) + 1.day
|
|
|
|
params[:event] ||= {
|
|
|
|
start_time: time,
|
|
|
|
end_time: time + 1.hour,
|
|
|
|
region_id: session[:region]
|
|
|
|
}
|
2014-11-05 21:25:18 +01:00
|
|
|
@event = Event.new event_params
|
|
|
|
end
|
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
# 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)
|
2016-09-11 17:40:49 +02:00
|
|
|
.permit :lock_version, :title, :start_time, :end_time, :repeat, :rule,
|
|
|
|
:description, :place_name, :address, :city, :region_id,
|
2016-12-17 16:59:11 +01:00
|
|
|
:locality, :url, :contact, :submitter, :tag_list
|
2014-08-06 14:47:47 +02:00
|
|
|
end
|
|
|
|
|
2016-09-03 03:41:11 +02:00
|
|
|
def filter_for_rss
|
2017-05-29 22:18:04 +02:00
|
|
|
if params[:future] == 'false'
|
2016-09-11 22:50:03 +02:00
|
|
|
@events.order 'id desc'
|
2016-09-03 03:41:11 +02:00
|
|
|
else
|
2016-09-11 22:50:03 +02:00
|
|
|
@events.future.order('id desc').limit 20
|
2016-09-03 03:41:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-22 21:14:36 +02:00
|
|
|
def locked
|
|
|
|
redirect_to edit_event_url(@event, secret: @event.secret),
|
|
|
|
alert: t('staleObjectError')
|
|
|
|
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
|
2017-04-22 20:01:47 +02:00
|
|
|
redirect_to :root, alert: t(:forbidden, scope: %i[events edit]) \
|
2014-08-06 14:47:47 +02:00
|
|
|
unless params[:secret] == @event.secret
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|