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

82 lines
2.6 KiB
Ruby
Raw Normal View History

2013-12-28 23:45:13 +01:00
class EventsController < InheritedResources::Base
before_action :set_event, only: [:show, :edit, :update, :destroy]
2013-12-28 23:45:13 +01:00
def index
@events = Event
if (params[:region] && params[:region].present? && params[:region] != 'all')
@events = @events.region(params[:region])
end
2014-01-02 00:28:11 +01: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] and !params[:month])
# Whole year calendar
@events = @events.year params[:year]
2013-12-29 20:42:00 +01:00
else
@events = @events.month(params[:year] || Date.today.year, params[:month] || Date.today.month)
2013-12-29 20:42:00 +01:00
end
}
format.rss {
2014-01-02 00:28:11 +01:00
@events = @events.future
@events = @events.limit params[:daylimit] if params[:daylimit]
2013-12-29 20:42:00 +01:00
}
2013-12-28 23:45:13 +01:00
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
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
2013-12-28 23:45:13 +01:00
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
2013-12-28 23:45:13 +01:00
end