Skip to content

Commit

Permalink
Update routing and add tests for admin controller
Browse files Browse the repository at this point in the history
  • Loading branch information
crespire committed Oct 6, 2024
1 parent e5d53e2 commit 0810227
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
13 changes: 11 additions & 2 deletions app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Admin
class EventsController < BaseController
before_action :set_event, only: %i[edit update destroy]
before_action :set_event, only: %i[show edit update destroy]
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

def index
@events = Event.all
end
Expand All @@ -12,6 +14,9 @@ def new
# GET /events/1/edit
def edit; end

# GET /events/1
def show; end

# POST /events or /events.json
def create
@event = Event.new(event_params)
Expand Down Expand Up @@ -58,13 +63,17 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find_by(slug: params[:id])
@event = Event.find_by!(slug: params[:id])
end

# Only allow a list of trusted parameters through.
def event_params
params.require(:event).permit(:name, :rsvp_link, :location, :presentation, :presenter, :sponsor, :sponsor_link,
:sponsor_logo, :start_at, :status)
end

def record_not_found
redirect_to admin_events_path, warning: 'Event not found'
end
end
end
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :events, only: [:index, :show], param: :slug do
resources :events, only: %i[index show], param: :slug do
collection do
get 'past', to: 'events#past'
get 'all', to: 'events#all', as: :all
Expand All @@ -8,7 +8,7 @@
end

namespace :admin do
resources :events, except: %i[show]
resources :events
end

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
Expand Down
62 changes: 62 additions & 0 deletions test/controllers/admin/events_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'test_helper'

class Admin::EventsControllerTest < ActionDispatch::IntegrationTest
def setup
Event.destroy_all
begining = Date.parse('2024-01-01').beginning_of_year
dates = 12.times.map { |i| begining + i.months }
@events = dates.map do |date|
Event.create!(
start_at: date,
name: "Event #{date.strftime('%B %Y')}",
location: 'Some Office',
city: 'Toronto, Canada',
presentation: "Presentation #{date}",
status: :published,
sponsor: 'Some Sponsor',
sponsor_link: 'https://example.com'
)
end
end

test 'should load the index page' do
get admin_events_path,
headers: {
Authorization:
ActionController::HttpAuthentication::Basic.encode_credentials(
'admin', 'admin'
)
}

assert_response :success
end

test 'should show draft events' do
Event.update_all(status: :draft)

get admin_events_path,
headers: {
Authorization:
ActionController::HttpAuthentication::Basic.encode_credentials(
'admin', 'admin'
)
}

assert_response :success
assert_equal 'text/html; charset=utf-8', response.content_type
assert response.body.include?(Event.last.name)
end

test 'should redirect to events page if event is not found' do
get admin_event_path('nonexistent-event'),
headers: {
Authorization:
ActionController::HttpAuthentication::Basic.encode_credentials(
'admin', 'admin'
)
}

assert_response :redirect
assert_redirected_to admin_events_path
end
end

0 comments on commit 0810227

Please sign in to comment.