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
This commit is contained in:
Julianna Tetreault 2020-10-29 10:11:15 -06:00 committed by GitHub
parent 5a617f7340
commit 6f4e600a4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 135 additions and 139 deletions

View file

@ -4,35 +4,40 @@ module 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*",
)
@events = Event.order(starts_at: :desc)
end
def edit
@event = Event.find(params[:id])
end
def create
@event = Event.new(event_params)
@events = Event.order(starts_at: :desc)
if @event.save
flash[:success] = "Successfully created event: #{@event.title}"
redirect_to(action: :index)
redirect_to admin_events_path
else
flash[:danger] = @event.errors.full_messages
render "index.html.erb"
render new_admin_event_path
end
end
def update
@event = Event.find(params[:id])
@events = Event.order(starts_at: :desc)
if @event.update(event_params)
flash[:success] = "#{@event.title} was successfully updated"
redirect_to "/admin/events"
redirect_to admin_events_path
else
flash[:danger] = @event.errors.full_messages
render "index.html.erb"
render :edit
end
end

View file

@ -1,50 +1,54 @@
<div class="form-group">
<%= f.label :cover_image %>:
<%= f.file_field :cover_image, class: "form-control" %>
<div class="crayons-card p-6">
<%= form_for [:admin, @event] do |f| %>
<div class="crayons-field">
<%= f.label :cover_image, class: "crayons-field__label" %>
<%= f.file_field :cover_image, class: "crayons-field" %>
</div>
<div class="crayons-field">
<%= f.label :profile_image, class: "crayons-field__label" %> (for live notification):
<%= f.file_field :profile_image, class: "crayons-field" %>
<img src="<%= @event.profile_image_url %>" style="width: 25%;" alt="event profile image">
</div>
<div class="crayons-field">
<%= f.label :title, class: "crayons-field__label" %>
<%= f.text_field :title, maxlength: 90, size: 40, required: true, class: "crayons-textfield" %>
</div>
<div class="crayons-field">
<%= f.label :host_name, class: "crayons-field__label" %>
<%= f.text_field :host_name, size: 40, required: true, class: "crayons-textfield" %>
</div>
<div class="crayons-field">
<%= f.label :category, class: "crayons-field__label" %>
<%= f.select :category, ["AMA", "Workshop", "Talk", "Town Hall"], required: true, class: "crayons-select" %>
</div>
<div class="crayons-field">
<%= f.label :starts_at, class: "crayons-field__label" %>
<%= f.datetime_select :starts_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "crayons-select" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="crayons-field">
<%= f.label :ends_at, class: "crayons-field__label" %>
<%= f.datetime_select :ends_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "crayons-select" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="crayons-field">
<%= f.label :location_name, class: "crayons-field__label" %>
<%= f.text_field :location_name, required: true, class: "crayons-textfield" %>
</div>
<div class="crayons-field">
<%= f.label :location_url, class: "crayons-field__label" %>
<%= f.text_field :location_url, required: true, class: "crayons-textfield" %>
</div>
<div class="crayons-field">
<%= f.label :description_markdown, class: "crayons-field__label" %>
<%= f.text_area :description_markdown, size: "45x10", required: true, class: "crayons-textfield" %>
</div>
<div class="crayons-field">
<%= f.label :published, class: "crayons-field__label" %>
<%= f.check_box :published, class: "crayons-checkbox" %>
</div>
<div class="crayons-field">
<%= f.label :live_now, class: "crayons-field__label" %>
<%= f.check_box :live_now, class: "crayons-checkbox" %>
</div>
<%= f.submit class: "crayons-btn" %>
<% end %>
</div>
<div class="form-group">
<%= f.label :profile_image %> (for live notification):
<%= f.file_field :profile_image, class: "form-control" %>
<img src="<%= event.profile_image_url %>" style="width: 25%;" alt="event profile image">
</div>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, maxlength: 90, size: 40, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :host_name %>
<%= f.text_field :host_name, size: 40, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.select :category, ["AMA", "Workshop", "Talk", "Town Hall"], required: true %>
</div>
<div class="form-group">
<%= f.label :starts_at %>
<%= f.datetime_select :starts_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "form-control" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="form-group">
<%= f.label :ends_at %>
<%= f.datetime_select :ends_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "form-control" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="form-group">
<%= f.label :location_name %>
<%= f.text_field :location_name, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :location_url %>
<%= f.text_field :location_url, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description_markdown %>
<%= f.text_area :description_markdown, size: "45x10", required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :published %>
<%= f.check_box :published %>
</div>
<div class="form-group">
<%= f.label :live_now %>
<%= f.check_box :live_now %>
</div>
<%= f.submit class: "btn btn-primary" %>

View file

@ -0,0 +1,4 @@
<div class="crayons-card p-6 mb-6">
<h2 class="crayons-title mb-6">Edit Event: <%= @event.title.to_s %></h2>
<%= render "event_form" %>
</div>

View file

@ -1,31 +1,38 @@
<div class="crayons-card p-6 mb-6">
<h2 class="crayons-title mb-6">Create New Event</h2>
<header class="flex items-center mb-6">
<h2 class="crayons-title">Events</h2>
<%= link_to "New Event", new_admin_event_path, class: "ml-auto crayons-btn crayons-btn--s" %>
</header>
<br>
<%= form_for @event, url: { controller: "events", action: "create" } do |f| %>
<%= render "event_form", f: f, event: @event %>
<% end %>
</div>
<div class="crayons-card p-6 mb-6">
<h2 class="crayons-title mb-6">Upcoming Events</h2>
<div class="crayons-card p-6 pages__table">
<h3>Upcoming Events</h3>
<% @events.each do |event| %>
<% if event.starts_at.future? %>
<img src="<%= event.cover_image_url %>" alt="event cover image">
<%= form_for [:admin, event] do |f| %>
<%= render "event_form", f: f, event: event %>
<% end %>
<div class="flex py-2 items-center">
<%= event.title %>
<% if event.cover_image_url.present? %>
<img class="mx-auto mt-3" width="40" height="40" src="<%= event.cover_image_url %>" alt="event cover image">
<% end %>
<%= link_to "Edit", edit_admin_event_path(event.id), class: "ml-auto crayons-btn crayons-btn--s crayons-btn--secondary" %>
</div>
<hr>
<% end %>
<% end %>
</div>
<br>
<div class="crayons-card p-6 mb-6">
<h2 class="crayons-title mb-6">Past Events</h2>
<div class="crayons-card p-6 pages__table">
<h3>Past Events</h3>
<% @events.each do |event| %>
<% if !event.starts_at.future? %>
<img src="<%= event.cover_image_url %>" alt="event cover image">
<%= form_for [:admin, event] do |f| %>
<%= render "event_form", f: f, event: event %>
<% end %>
<div class="flex py-2 items-center">
<%= event.title %>
<% if event.cover_image_url.present? %>
<img class="mx-auto mt-3 justify-content-center" width="40" height="40" src="<%= event.cover_image_url %>" alt="event cover image">
<% end %>
<%= link_to "Edit", edit_admin_event_path(event.id), class: "ml-auto crayons-btn crayons-btn--s crayons-btn--secondary" %>
</div>
<hr>
<% end %>
<% end %>
</div>

View file

@ -1,55 +1,4 @@
<h2 class="crayons-title mb-6">Create Event</h2>
<div class="crayons-card p-6">
<%# form_with url: admin_ %>
<div class="crayons-card p-6 mb-6">
<h2 class="crayons-title mb-6">Create New Event</h2>
<%= render "event_form" %>
</div>
<div class="form-group">
<%= f.label :cover_image %>:
<%= f.file_field :cover_image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :profile_image %> (for live notification):
<%= f.file_field :profile_image, class: "form-control" %>
<img src="<%= event.profile_image_url %>" style="width: 25%;" alt="event profile image">
</div>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, maxlength: 90, size: 40, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :host_name %>
<%= f.text_field :host_name, size: 40, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.select :category, ["AMA", "Workshop", "Talk", "Town Hall"], required: true %>
</div>
<div class="form-group">
<%= f.label :starts_at %>
<%= f.datetime_select :starts_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "form-control" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="form-group">
<%= f.label :ends_at %>
<%= f.datetime_select :ends_at, required: true, include_blank: true, start_year: Time.current.year, end_year: Time.current.year + 2, class: "form-control" %> UTC Time Only (4 hours ahead of eastern time)
</div>
<div class="form-group">
<%= f.label :location_name %>
<%= f.text_field :location_name, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :location_url %>
<%= f.text_field :location_url, required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description_markdown %>
<%= f.text_area :description_markdown, size: "45x10", required: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :published %>
<%= f.check_box :published %>
</div>
<div class="form-group">
<%= f.label :live_now %>
<%= f.check_box :live_now %>
</div>
<%= f.submit class: "btn btn-primary" %>

View file

@ -16,7 +16,7 @@
<div class="mt-8 pages__override_defaults">
<div class="my-6">
<% if !@code_of_conduct || !@privacy || !@terms%>
<% if !@code_of_conduct || !@privacy || !@terms %>
<h2 class="mb-4 crayons-title">Override defaults</h2>
<div class="crayons-notice mb-4 crayons-notice--warning">
<strong>Note: Proceed with caution. When you edit any of the following pages, it will diverge from the original Forem version and you will no longer receive updates. You will see your updated version in the section above.</strong>
@ -31,7 +31,7 @@
<% if !@privacy %>
<div class="flex py-2 items-center">
<%= link_to "Privacy Policy", privacy_path %>
<%= link_to "Override", new_admin_page_path(slug: "privacy"), class: "ml-auto crayons-btn crayons-btn--s crayons-btn--danger" %>
<%= link_to "Override", new_admin_page_path(slug: "privacy"), class: "ml-auto crayons-btn crayons-btn--s crayons-btn--danger" %>
</div>
<% end %>
<% if !@terms %>
@ -47,6 +47,4 @@
</div>
<% end %>
</div>
</div>

View file

@ -80,14 +80,14 @@
<span class="location"><%= event.location_url %></span>
<span class="alarm_reminder">15</span>
<span class="description">
<%= event.description_html.html_safe %>
<%= event.description_html&.html_safe %>
------
Link to attend - <%= event.location_url %>
</span>
</div>
</div>
<div class="event-description">
<%= event.description_html.html_safe %>
<%= event.description_html&.html_safe %>
</div>
</div>

View file

@ -66,7 +66,7 @@ Rails.application.routes.draw do
destroy], path: "listings/categories"
resources :comments, only: [:index]
resources :events, only: %i[index create update]
resources :events, only: %i[index create update new edit]
resources :feedback_messages, only: %i[index show]
resources :invitations, only: %i[index new create destroy]
resources :pages, only: %i[index new create edit update destroy]

View file

@ -1,8 +1,19 @@
require "rails_helper"
RSpec.describe "/admin/events", type: :request do
let(:event) { create(:event) }
let(:event) { create(:event, title: "Hey") }
let(:admin) { create(:user, :super_admin) }
let(:params) do
{
event: {
title: "Hello, world!",
description_markdown: "This is an event",
starts_at: Time.current,
ends_at: 3660.seconds.from_now,
category: "Talk"
}
}
end
describe "PUT admin/events" do
before do
@ -19,5 +30,23 @@ RSpec.describe "/admin/events", type: :request do
patch "/admin/events/#{event.id}", params: { event: { live_now: "1" } }
expect(event.reload.live_now).to eq true
end
it "successfully updates the event title" do
expect do
patch "/admin/events/#{event.id}", params: params
end.to change { event.reload.title }.to("Hello, world!")
end
end
describe "POST /admin/events" do
let(:post_resource) { post "/admin/events", params: params }
before { sign_in admin }
it "successfully creates an event" do
expect do
post_resource
end.to change { Event.all.count }.by(1)
end
end
end

View file

@ -6,7 +6,7 @@ RSpec.describe "Admin creates new event", type: :system do
before do
sign_in admin
visit "/admin/events"
visit "/admin/events/new"
end
def select_date_and_time(year, month, date, hour, min, field_name)
@ -26,7 +26,7 @@ RSpec.describe "Admin creates new event", type: :system do
end
it "loads /admin/events" do
expect(page).to have_content("Create New Event")
expect(page).to have_content("New Event")
end
it "loads published events on /events" do