138 lines
3.8 KiB
Ruby
138 lines
3.8 KiB
Ruby
# Event life cycle
|
|
# This is a central part to this project
|
|
class EventsController < ApplicationController
|
|
before_action :set_events, only: [:index]
|
|
before_action :set_event, only:
|
|
[:show, :edit, :preview, :update, :cancel, :destroy]
|
|
before_action :check_secret, only:
|
|
[:edit, :preview, :update, :cancel, :destroy]
|
|
before_action :set_mailer_host
|
|
|
|
def index
|
|
respond_to do |format|
|
|
format.html
|
|
format.rss do
|
|
@events = @events.future_30.includes :related_city
|
|
@events = @events.limit params[:daylimit] if params[:daylimit]
|
|
end
|
|
format.ics { @events = @events.last_year.order :id }
|
|
format.xml { @events = @events.includes(:related_region).order :id }
|
|
end
|
|
end
|
|
|
|
# GET /users/new
|
|
def new
|
|
@event = Event.new
|
|
@event.start_time ||= Time.now.change(min: 0) + 1.day + 1.hour
|
|
@event.end_time ||= Time.now.change(min: 0) + 1.day + 2.hour
|
|
end
|
|
|
|
# POST /events/preview
|
|
def preview_create
|
|
@event = Event.new event_params
|
|
@event.valid?
|
|
render action: :new
|
|
end
|
|
|
|
# POST /events
|
|
# POST /events.json
|
|
def create
|
|
@event = Event.new event_params
|
|
|
|
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
|
|
@older_event = Event.new @event.attributes
|
|
respond_to do |format|
|
|
if @event.update(event_params) && send_update_mails
|
|
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
|
|
|
|
# DELETE /events/1
|
|
# DELETE /events/1.json
|
|
def destroy
|
|
@event.destroy
|
|
respond_to do |format|
|
|
format.html { redirect_to events_url, notice: t('.ok') }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_events
|
|
@events = Event.moderated
|
|
@events = @events.region params[:region] \
|
|
if params[:region].present? && params[:region] != 'all'
|
|
@events = @events.tag(params[:tag]) if params[:tag]
|
|
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
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list
|
|
# through.
|
|
def event_params
|
|
params.require(:event)
|
|
.permit :title, :start_time, :end_time, :description, :city, :region,
|
|
: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
|
|
|
|
# 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
|
|
end
|