docbrown/app/controllers/admin/events_controller.rb
Julianna Tetreault 6f4e600a4e
Refactor Admin Events Index (#11103) [deploy]
* Adds new and edit views for events and removes them from the index view

* Fixes formatting issues in /admin/pages/index.html.erb

* Refactors /admin/events partial, new, and edit forms

* Updates admin_events routes to include all routes except destroy

* Refactors Admin::EventsController and adds #new and #edit actions

* specs: Adjusts admin_creates_new_event_spec to work with /admin/event changes

* Refactors the Admin::EventsController, routes, and events index even further
  - Adds back placeholder text in Events form
  - Removes unnecessary code from controller
  - Reverts changes to routes
  - Adjusts event_cover_image size on index.html.erb

* specs: Adds tests around creating and updating events to events_spec

* Adds pagination to Admin::EventsController #index to cap events at 20 per page

* Removes .all from Admin::EventsController and adds a line to index.html.erb

* Uses crayons classes for _event_form.html.erb
2020-10-29 10:11:15 -06:00

55 lines
1.4 KiB
Ruby

module Admin
class EventsController < ApplicationController
layout "admin"
include ApplicationHelper
def index
@events = Event.order(starts_at: :desc).page(params[:page]).per(20)
end
def new
@event = Event.new(
location_name: "#{URL.domain}/live",
location_url: app_url,
description_markdown: "*Description* *Pre-requisites:* *Bio*",
)
end
def edit
@event = Event.find(params[:id])
end
def create
@event = Event.new(event_params)
if @event.save
flash[:success] = "Successfully created event: #{@event.title}"
redirect_to admin_events_path
else
flash[:danger] = @event.errors.full_messages
render new_admin_event_path
end
end
def update
@event = Event.find(params[:id])
if @event.update(event_params)
flash[:success] = "#{@event.title} was successfully updated"
redirect_to admin_events_path
else
flash[:danger] = @event.errors.full_messages
render :edit
end
end
private
def event_params
allowed_params = %i[
title category event_date starts_at ends_at
location_name cover_image location_url description_markdown published
host_name profile_image live_now
]
params.require(:event).permit(allowed_params)
end
end
end