Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rails_application/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ApplicationController < ActionController::Base
around_action :set_time_zone

def event_store
Rails.configuration.event_store
end
Expand All @@ -10,4 +12,10 @@ def command_bus
def not_found
render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found
end

private

def set_time_zone(&block)
Time.use_zone(cookies[:timezone], &block)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// app/javascript/controllers/timezone_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {

connect() {
this.detectAndSetTimezone()
}

detectAndSetTimezone() {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone

document.cookie = `timezone=${timezone}`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def call(event)
<tr class="border-t">
<td class="py-2">#{time_promotion.label}</td>
<td class="py-2">#{time_promotion.discount}</td>
<td class="py-2">#{time_promotion.start_time}</td>
<td class="py-2">#{time_promotion.end_time}</td>
<td class="py-2">#{time_promotion.start_time.strftime("%Y-%m-%d %H:%M")}</td>
<td class="py-2">#{time_promotion.end_time.strftime(("%Y-%m-%d %H:%M"))}</td>
</tr>
HTML
)
Expand Down
1 change: 1 addition & 0 deletions rails_application/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
</main>
</body>
<%= render 'footer' %>
<div data-controller="timezone"></div>
</html>
4 changes: 2 additions & 2 deletions rails_application/app/views/time_promotions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<tr class="border-t">
<td class="py-2"><%= time_promotion.label %></td>
<td class="py-2"><%= time_promotion.discount %></td>
<td class="py-2"><%= time_promotion.start_time %></td>
<td class="py-2"><%= time_promotion.end_time %></td>
<td class="py-2"><%= time_promotion.start_time.strftime("%Y-%m-%d %H:%M") %></td>
<td class="py-2"><%= time_promotion.end_time.strftime("%Y-%m-%d %H:%M") %></td>
</tr>
<% end %>
</tbody>
Expand Down
28 changes: 20 additions & 8 deletions rails_application/test/integration/time_promotions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,49 @@

class TimePromotionsTest < InMemoryRESIntegrationTestCase
def test_happy_path
cookies[:timezone] = "Europe/Warsaw"

post "/time_promotions", params: {
label: "Last Minute June 2022",
discount: "50",
start_time: "2022-06-30 15:00:00 UTC",
end_time: "2022-07-01 00:00:00 UTC"
start_time: "2022-06-30 15:00",
end_time: "2022-07-01 00:00"
}
follow_redirect!
assert_response :success

time_promotion = TimePromotions::TimePromotion.find_by(label: "Last Minute June 2022")

assert_equal("2022-06-30 13:00:00 UTC", time_promotion.start_time.to_s)
assert_equal("2022-06-30 22:00:00 UTC", time_promotion.end_time.to_s)

assert_select("p", "Time promotion was successfully created")

post "/time_promotions", params: {
label: "Black Monday July 2022",
discount: "40",
start_time: "2022-07-04 01:00:00 UTC",
end_time: "2022-07-05 00:00:00 UTC"
start_time: "2022-07-04 01:00:00",
end_time: "2022-07-05 00:00:00"
}
follow_redirect!
assert_response :success

time_promotion = TimePromotions::TimePromotion.find_by(label: "Black Monday July 2022")

assert_equal("2022-07-03 23:00:00 UTC", time_promotion.start_time.to_s)
assert_equal("2022-07-04 22:00:00 UTC", time_promotion.end_time.to_s)

assert_select("p", "Time promotion was successfully created")

get "/time_promotions"
assert_select("td", "Last Minute June 2022")
assert_select("td", "50")
assert_select("td", "2022-06-30 15:00:00 UTC")
assert_select("td", "2022-07-01 00:00:00 UTC")
assert_select("td", "2022-06-30 15:00")
assert_select("td", "2022-07-01 00:00")

assert_select("td", "Black Monday July 2022")
assert_select("td", "40")
assert_select("td", "2022-07-04 01:00:00 UTC")
assert_select("td", "2022-07-05 00:00:00 UTC")
assert_select("td", "2022-07-04 01:00")
assert_select("td", "2022-07-05 00:00")
end
end