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

93 lines
2.8 KiB
Ruby

class EventsController < InheritedResources::Base
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
@events = Event
if (params[:region] && params[:region].present? && params[:region] != 'all')
@events = @events.region(params[:region])
end
@events = @events.tag(params[:tag]) if (params[:tag])
respond_to do |format|
format.html {
if (params[:year] and !params[:month])
# Whole year calendar
@events = @events.year params[:year]
else
@events = @events.month(params[:year] || Date.today.year, params[:month] || Date.today.month)
end
}
format.rss {
@events = @events.future_30
@events = @events.limit params[:daylimit] if params[:daylimit]
}
format.ics {
@events = @events.where('start_time > ?', 360.days.ago).order :id
}
end
end
# POST /events
# POST /events.json
def create
@event = Event.new(event_params)
# This is a special case, required to handle the region attribute with same foreign key name
@event.region = Region.find(params[:event][:region])
if params[:visu]
render action: :new
return
end
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
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
if params[:visu]
@event.attributes = params[:event]
render action: :edit
return
end
respond_to do |format|
# This is a special case, required to handle the region attribute with same foreign key name
@event.region = Region.find(params[:event][:region])
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
private
def permitted_params
params.permit event: [:title, :start_time, :end_time, :description, :city, :locality, :url, :contact, :submitter, :tags]
end
# Use callbacks to share common setup or constraints between actions.
def set_event
@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, :locality, :url, :contact, :submitter, :tags
end
end