Skip to content

Commit

Permalink
Update controller to adjust timezone correctly, add test (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
crespire authored Nov 20, 2024
1 parent 436daaf commit 2eabe89
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Admin
class EventsController < BaseController
TZ_STRING = 'Eastern Time (US & Canada)'
before_action :set_event, only: %i[show edit update destroy]
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

Expand All @@ -20,7 +21,7 @@ def show; end
# POST /events or /events.json
def create
@event = Event.new(event_params)
@event.start_at = ActiveSupport::TimeZone.new('Eastern Time (US & Canada)').local_to_utc(@event.start_at)
@event.start_at = @event.start_at.change(zone: TZ_STRING)

respond_to do |format|
if @event.save
Expand All @@ -36,7 +37,7 @@ def create
# PATCH/PUT /events/1 or /events/1.json
def update
@event.assign_attributes(event_params)
@event.start_at = ActiveSupport::TimeZone.new('Eastern Time (US & Canada)').local_to_utc(@event.start_at)
@event.start_at = @event.start_at.change(zone: TZ_STRING)

respond_to do |format|
if @event.save
Expand Down
29 changes: 29 additions & 0 deletions test/controllers/admin/events_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,33 @@ def setup
css_select "##{attr}"
end
end

test 'should create the correct event details' do
post admin_events_path,
params: {
'event' => {
'name' => "Toronto Ruby's One Year Anniversary",
'location' => '<div>Some Office</div>',
'rsvp_link' => 'https://test.com',
'description' => '<div>DESCRIPTION</div>',
'sponsor' => '',
'sponsor_link' => '',
'sponsor_logo' => '',
'start_at' => '2024-11-25T19:30',
'status' => 'published'
}
},
headers: {
Authorization:
ActionController::HttpAuthentication::Basic.encode_credentials(
'admin', 'admin'
)
}

got = Event.last
want = {
start_at: DateTime.parse('2024-11-25T19:30-05:00')
}
assert_equal(want[:start_at].utc, got.start_at)
end
end

0 comments on commit 2eabe89

Please sign in to comment.