Skip to content

Commit

Permalink
Adding a two more tests to EventsController
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Mar 27, 2024
1 parent bf691df commit 71eb7dc
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 19 deletions.
58 changes: 45 additions & 13 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def edit
end

def create
populate_permitted_params_event(permitted_params)

@event = Event.new(permitted_params[:event])
create_params = populate_permitted_params_event(converted_params[:event])
@event = Event.new(create_params)

if @event.save
redirect_to @event
Expand All @@ -43,9 +42,8 @@ def create
end

def update
populate_permitted_params_event(permitted_params)

if @event.update(permitted_params[:event])
update_params = populate_permitted_params_event(converted_params[:event])
if @event.update(update_params)
redirect_to @event
else
render action: 'edit'
Expand Down Expand Up @@ -111,12 +109,17 @@ def download_guest_list

private

def populate_permitted_params_event(permitted_params)
permitted_params[:event][:start_time] = Time.from_picker(permitted_params.delete(:start_time))
permitted_params[:event][:end_time] = Time.from_picker(permitted_params.delete(:end_time))
permitted_params[:event][:ticket_sales_start_time] = Time.from_picker(permitted_params.delete(:ticket_sales_start_time))
permitted_params[:event][:ticket_sales_end_time] = Time.from_picker(permitted_params.delete(:ticket_sales_end_time))
permitted_params[:event][:ticket_requests_end_time] = Time.from_picker(permitted_params.delete(:ticket_requests_end_time))
def converted_params
@converted_params ||= permitted_params.to_h.tap(&:symbolize_keys!)
end

def populate_permitted_params_event(permitted_parameters)
permitted_parameters[:start_time] = Time.from_picker(permitted_parameters.delete(:start_time))
permitted_parameters[:end_time] = Time.from_picker(permitted_parameters.delete(:end_time))
permitted_parameters[:ticket_sales_start_time] = Time.from_picker(permitted_parameters.delete(:ticket_sales_start_time))
permitted_parameters[:ticket_sales_end_time] = Time.from_picker(permitted_parameters.delete(:ticket_sales_end_time))
permitted_parameters[:ticket_requests_end_time] = Time.from_picker(permitted_parameters.delete(:ticket_requests_end_time))
permitted_parameters
end

def completed_ticket_requests
Expand All @@ -133,6 +136,35 @@ def set_event
end

def permitted_params
params.permit(:id, :event, :user_email, :user_id, :start_time, :end_time, :ticket_sales_start_time, :ticket_sales_end_time, :ticket_requests_end_time)
params.permit(
:id,
:user_email,
:user_id,
:start_time,
:end_time,
:ticket_sales_start_time,
:ticket_sales_end_time,
:ticket_requests_end_time,
event: %i[
adult_ticket_price
allow_donations
allow_financial_assistance
cabin_price
early_arrival_price
end_time
kid_ticket_price
late_departure_price
max_adult_tickets_per_request
max_cabin_requests
max_cabins_per_request
max_kid_tickets_per_request
name
require_mailing_address
start_time
tickets_require_approval
]
)
.to_hash
.with_indifferent_access
end
end
8 changes: 3 additions & 5 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
= yield :head

%body
.navbar.navbar-expand-lg.navbar-dark.bg-primary
.navbar.navbar-expand-md.navbar-dark.bg-primary
.container-fluid
%a.navbar-brand{ href: root_path }
.h2
Expand All @@ -27,14 +27,14 @@
%span.navbar-toggler-icon

.collapse.navbar-collapse#navbarSupportedContent
%ul.navbar-nav.me-auto.mb-2.mb-lg-0.justify-content-center.navbar-nav-scroll{style: "--bs-scroll-height: 100px;" }
%ul.navbar-nav.justify-content-center
- if user_signed_in?
%li.nav-item
%a.nav-link{ href: edit_user_registration_path }
Edit Profile
%li.nav-item
= link_to destroy_user_session_path, method: :delete, class: 'nav-link' do
Logouts
Log Out

- else
%li.nav-item.d-flex
Expand All @@ -46,8 +46,6 @@
%input.form-control.me-2{"aria-label" => "Search", :placeholder => "Search", :type => "search"}/
%button.btn.btn-info{:type => "submit", onclick: 'document.alert("Not yet implemented")'} Search



.container
= bootstrap_flash
= yield
Expand Down
4 changes: 3 additions & 1 deletion lib/extensions/time.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Time
TIME_FORMAT = '%Y/%m/%d %H:%M:%S'

DATE_FORMATS.merge!(
friendly: '%A, %B %-d, %Y %l:%M %p %Z',
month_day: '%B %-d',
Expand All @@ -9,6 +11,6 @@ class Time
)

def self.from_picker(datetimepicker_string)
strptime(datetimepicker_string, '%Y/%m/%d %H:%M:%S').to_time if datetimepicker_string.present?
strptime(datetimepicker_string, TIME_FORMAT).to_time if datetimepicker_string.present?
end
end
112 changes: 112 additions & 0 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,116 @@
end
end
end

describe 'GET #new' do
before { get :new }

context 'when the user is not signed in' do
it { redirects_to new_user_session_path }
end

context 'when the user is signed in' do
context 'and is not a site admin' do
let(:viewer) { create(:user) }

it { succeeds }
end

context 'and is a site admin' do
let(:viewer) { create(:site_admin).user }

it { succeeds }
end
end
end

describe 'POST #create' do
let(:new_event) { build(:event) }
let(:new_event_params) do
new_event.as_json.tap do |h|
h[:start_time] = new_event.start_time.strftime(Time::TIME_FORMAT)
h[:end_time] = new_event.end_time.strftime(Time::TIME_FORMAT)
end
end
let(:make_request) { -> { post :create, params: { event: new_event_params } } }

# rubocop: disable RSpec/AnyInstance
before do
allow_any_instance_of(described_class).to receive(:params).and_return(ActionController::Parameters.new(event: new_event_params))
end
# rubocop: enable RSpec/AnyInstance

describe '#permitted_params' do
subject(:permitted_keys) { permitted_event_keys }

let(:permitted_event_params) { described_class.new.send(:permitted_params)[:event].to_h.symbolize_keys }
let(:expected_keys) do
%i[
adult_ticket_price
allow_donations
allow_financial_assistance
cabin_price
early_arrival_price
end_time
kid_ticket_price
late_departure_price
max_adult_tickets_per_request
max_cabin_requests
max_cabins_per_request
max_kid_tickets_per_request
name
require_mailing_address
start_time
tickets_require_approval
]
end
let(:permitted_event_keys) { permitted_event_params.keys.sort }

it { expect(permitted_keys).to eql(expected_keys) }
end

context 'when the user is not signed in' do
before { make_request[] }

it { redirects_to new_user_session_path }
end

context 'when the user is signed in' do
context 'and is not a site admin' do
let(:viewer) { create(:user) }

before { make_request[] }

it { redirects_to root_path }
end

context 'and is a site admin' do
let(:viewer) { create(:site_admin).user }

describe 'flash messages' do
before { make_request[] }

its(:flash) { is_expected.to be_empty }
end

describe 'newly created event' do
subject { Event.last }

before { make_request[] }

it { is_expected.to be_valid }
end

describe 'event creation' do
subject(:response) { make_request[] }

let(:created_event) { subject.instance_variable_get(:@event) }

it { expect { response }.to change(Event, :count) }
it { is_expected.to have_http_status(:redirect) }
it { is_expected.to redirect_to(event_path(Event.last)) }
end
end
end
end
end

0 comments on commit 71eb7dc

Please sign in to comment.