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

130 lines
3.5 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
2014-07-01 15:50:39 +02:00
@events = Event.all_moderated
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
}
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
}
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
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
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
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
2014-07-01 15:50:39 +02:00
@event = Event.all_moderated
end
@event = @event.find params[:id]
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