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

166 lines
4.8 KiB
Ruby
Raw Normal View History

2014-06-22 22:06:12 +02:00
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :cancel, :destroy]
before_action :check_secret, only: [:edit, :update, :cancel, :destroy]
before_filter :set_mailer_host
2013-12-28 23:45:13 +01:00
def index
@events = Event.moderated
2014-07-01 15:50:39 +02:00
if params[:region] && params[:region].present? && params[:region] != 'all'
@events = @events.region params[:region]
end
2014-07-01 15:50:39 +02:00
@events = @events.tag(params[:tag]) if params[:tag]
2013-12-29 20:42:00 +01:00
respond_to do |format|
format.html {
if params[:year]
2013-12-29 20:42:00 +01:00
# Whole year calendar
@events = @events.year params[:year].to_i
2013-12-29 20:42:00 +01:00
else
if params[:start_date]
start_date = Date.parse params[:start_date]
else
start_date = Date.today
end
@events = @events.month start_date.change day: 1
2013-12-29 20:42:00 +01:00
end
set_meta_tags description: "#{t('layouts.application.subtitle')} - #{start_date ? t('date.month_names')[start_date.month] : ''} #{start_date ? start_date.year.to_s : params[:year]} - #{t '.nb_events', count: @events.size}",
keywords: @events.pluck(:tags)
.join(' ')
.split
.group_by { |i| i }
.reject { |k, v| v.size < 2 }
.collect { |k, v| k },
DC: {
title: t('layouts.application.title'),
subject: t('layouts.application.subtitle'),
date: start_date.to_s
}
2013-12-29 20:42:00 +01:00
}
format.rss {
@events = @events.future_30.includes(:related_city)
2014-01-02 00:28:11 +01:00
@events = @events.limit params[:daylimit] if params[:daylimit]
2013-12-29 20:42:00 +01:00
}
format.ics {
@events = @events.where('start_time > ?', 360.days.ago).order :id
}
2014-07-19 09:03:08 +02:00
format.xml {
@events = @events.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
@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
# POST /events
# POST /events.json
def create
2014-07-01 15:50:39 +02:00
@event = Event.new event_params
if params[:visu]
2014-05-31 23:13:43 +02:00
@event.valid?
render action: :new
return
end
respond_to do |format|
if @event.save
# Send an event creation mail to its author
EventMailer.create(@event).deliver
# Send a mail to moderators
ModerationMailer.create(@event).deliver
format.html { redirect_to :root, notice: t('.ok') }
format.json { render action: 'show', status: :created, location: @event }
else
format.html { render action: 'new' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
olderEvent = Event.new @event.attributes
2014-01-06 11:22:39 +01:00
if params[:visu]
@event.attributes = event_params
2014-05-31 23:13:43 +02:00
@event.valid?
render action: :edit
2014-01-06 11:22:39 +01:00
return
end
respond_to do |format|
2014-07-01 15:50:39 +02:00
if @event.update event_params
# Send an update mail to moderators
ModerationMailer.update(olderEvent, @event, nil).deliver
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' }
format.json { render json: @event.errors, status: :unprocessable_entity }
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
# 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]
set_meta_tags title: @event.title,
description: @event.description,
keywords: @event.tags,
DC: {
title: @event.title,
date: @event.start_time.to_s
},
geo: {
region: @event.related_region,
placename: @event.city,
position: "#{@event.related_city.try :latitude};#{@event.related_city.try :longitude}"
}
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
2014-01-06 11:22:39 +01:00
params.require(:event)
2014-07-01 15:50:39 +02:00
.permit :title, :start_time, :end_time, :description, :city, :region, :locality, :url, :contact, :submitter, :tags
end
def check_secret
if params[:secret] != @event.secret
redirect_to :root, notice: t(:forbidden, scope: [:events, :edit])
end
end
# Useful to manage absolute url in mails
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
2013-12-28 23:45:13 +01:00
end