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

139 lines
3.8 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
2014-08-07 01:06:10 +02:00
before_action :set_events, only: [:index]
before_action :set_event, except: [:index, :new, :preview_create, :create]
before_action :set_create_event, only: [:preview_create, :create]
before_action :check_secret, only: [:edit, :preview, :update, :destroy]
before_action :set_old_event, only: [:update]
before_action :set_mailer_host
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 { render layout: 'iframe' if params[:iframe] }
format.rss { @events = @events.future }
format.ics { @events = @events.last_year.includes :region }
format.xml { @events = @events.includes :region }
2013-12-28 23:45:13 +01:00
end
end
# GET /events/new
2014-06-22 22:06:12 +02:00
def new
@event = Event.new start_time: Time.now.change(min: 0) + 1.day + 1.hour,
end_time: Time.now.change(min: 0) + 1.day + 2.hour
2014-06-22 22:06:12 +02:00
end
# 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 && send_creation_mails
format.html { redirect_to :root, notice: t('.ok') }
# 201 means :created
format.json { render action: 'show', status: 201, location: @event }
else
format.html { render action: 'new' }
# 422 means :unprocessable_entity
format.json { render json: @event.errors, status: 422 }
end
end
end
# PATCH/PUT /events/1/preview
def preview
@event.attributes = event_params
@event.valid?
render action: :edit
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if @event.update(event_params) && send_update_mails
2014-07-01 21:11:39 +02:00
format.html { redirect_to :root, notice: t('.ok') }
format.json { head :no_content }
else
format.html { render action: 'edit' }
# 422 means :unprocessable_entity
format.json { render json: @event.errors, status: 422 }
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|
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
@events = apply_scopes Event.moderated
end
# Use callbacks to share common setup or constraints between actions
def set_event
if params[:secret].present?
@event = Event.where secret: params[:secret]
else
@event = Event.moderated
end
@event = @event.find params[:id]
end
def set_create_event
@event = Event.new event_params
end
def set_old_event
@older_event = Event.new @event.attributes
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, :description,
:address, :city, :region_id, :locality, :url, :contact,
:submitter, :tags
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: [:events, :edit]) \
unless params[:secret] == @event.secret
end
def send_creation_mails
# Send an event creation mail to its author
2015-01-10 01:00:06 +01:00
EventMailer.create(@event).deliver_now
# Send a mail to moderators
2015-01-10 01:00:06 +01:00
ModerationMailer.create(@event).deliver_now
end
def send_update_mails
# Send an update mail to moderators
2015-01-10 01:00:06 +01:00
ModerationMailer.update(@older_event, @event, nil).deliver_now
end
def locked
redirect_to edit_event_url(@event, secret: @event.secret),
alert: t('staleObjectError')
end
2013-12-28 23:45:13 +01:00
end