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

134 lines
3.6 KiB
Ruby
Raw Normal View History

# Event life cycle
# This is a central part to this project
2014-06-22 22:06:12 +02:00
class EventsController < ApplicationController
has_scope :region, :locality, :tag, :daylimit, :year
has_scope :near, type: :hash, using: %i[location distance]
has_scope :future, type: :boolean, default: true, only: [:index], if: :future?
2018-09-30 19:02:49 +02:00
before_action :set_events, only: :index
before_action :set_event, except: %i[index new preview_create create]
before_action :set_create_event, only: %i[new preview_create create]
before_action :check_secret, only: %i[edit preview update destroy]
2018-09-30 19:02:49 +02:00
before_action :set_mailer_host, only: %i[create update destroy]
rescue_from ActiveRecord::StaleObjectError, with: :locked
2013-12-28 23:45:13 +01:00
def index
2013-12-29 20:42:00 +01:00
respond_to do |format|
format.html
format.ics
format.json
format.rss
format.xml
2013-12-28 23:45:13 +01:00
end
end
# GET /events/new
def new; end
2014-06-22 22:06:12 +02:00
# POST /events/preview
def preview_create
@event.valid?
render action: :new
end
# POST /events
# POST /events.json
def create
respond_to do |format|
if @event.save
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 }
else
2018-05-23 09:24:42 +02:00
format.html { render action: :new }
status = :unprocessable_entity
format.json { render json: @event.errors, status: status }
end
end
end
# 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
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if @event.update event_params
format.html { redirect_to @event, notice: t('.ok') }
format.json { head :no_content }
else
2018-05-23 09:24:42 +02:00
format.html { render action: :edit }
status = :unprocessable_entity
format.json { render json: @event.errors, status: status }
end
end
end
2014-06-22 22:06:12 +02:00
# DELETE /events/1
# DELETE /events/1.json
def destroy
@event.reason = params[:event][:reason]
2014-06-22 22:06:12 +02:00
@event.destroy
respond_to do |format|
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
def set_events
# The 3000 limit is purely arbitrary...
@events = apply_scopes Event.moderated.order('id desc')
.limit(params[:format] == 'rss' ? 20 : 3000)
@events = @events.includes :region if params[:format].present?
end
# Use callbacks to share common setup or constraints between actions
def set_event
2018-09-30 19:02:49 +02:00
@event = Event.find params[:id]
end
def set_create_event
time = Time.zone.now.change(min: 0) + 1.day
params[:event] ||= {
start_time: time,
end_time: time + 1.hour,
region_id: session[:region]
}
@event = Event.new event_params
end
# Never trust parameters from the scary internet, only allow the white list
# through
def event_params
params.require(:event)
.permit :lock_version, :title, :start_time, :end_time, :repeat, :rule,
:description, :place_name, :address, :city, :region_id,
:locality, :url, :contact, :submitter, :tag_list
end
def locked
redirect_to edit_event_url(@event, secret: @event.secret),
alert: t('staleObjectError')
end
# Check that you can only edit an existing event if you know its secret
def check_secret
redirect_to :root, alert: t(:forbidden, scope: %i[events edit]) \
unless params[:secret] == @event.secret
end
# Should the future scope be applied?
# Only on non html pages...
def future?
params[:format].present? && params[:format] != 'html'
end
2013-12-28 23:45:13 +01:00
end