From a7f635d2ee0c2a4d1005e1fda8c828ecf0256a26 Mon Sep 17 00:00:00 2001 From: cavis Date: Wed, 2 Aug 2023 14:21:53 -0600 Subject: [PATCH] Use second-offsets in dropdown; change timezone on frontend --- app/helpers/podcast_planner_helper.rb | 13 ++++++------- app/models/podcast_planner.rb | 4 ++-- .../podcast_planner/_form_draft_settings.html.erb | 2 +- config/locales/en.yml | 2 +- test/models/podcast_planner_test.rb | 7 +++++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/helpers/podcast_planner_helper.rb b/app/helpers/podcast_planner_helper.rb index e6f18b115..6fdd2e8e9 100644 --- a/app/helpers/podcast_planner_helper.rb +++ b/app/helpers/podcast_planner_helper.rb @@ -18,14 +18,13 @@ def week_options end def time_options - opts = [] - 24.times do |hour| - time = Time.new - opts.push(time.change({hour: hour})) - opts.push(time.change({hour: hour, min: 30})) + epoch = Time.at(0).utc + 24.times.flat_map do |hour| + [0, 30].map do |minute| + time = epoch.change(hour: hour, min: minute) + [I18n.l(time, format: :time_12_hour), time.to_i] + end end - - opts.map { |opt| [I18n.l(opt, format: :time_12_hour), opt] } end def days_in_month(month) diff --git a/app/models/podcast_planner.rb b/app/models/podcast_planner.rb index 798cf4a77..a3a4fcc19 100644 --- a/app/models/podcast_planner.rb +++ b/app/models/podcast_planner.rb @@ -13,7 +13,7 @@ def initialize(params = {}) @week_condition = params[:week_condition] @number_of_episodes = params[:number_of_episodes].try(:to_i) @end_date = params[:end_date].try(:to_datetime) - @publish_time = params[:publish_time].try(:to_time) + @publish_time = params[:publish_time].try(:to_i) @segment_count = params[:segment_count].try(:to_i) @medium = params[:medium] end @@ -157,7 +157,7 @@ def generate_drafts! end def apply_publish_time(date) - date.change(hour: @publish_time.hour, min: @publish_time.min) + Time.at(date.to_i + @publish_time) end def generate_default_title(date) diff --git a/app/views/podcast_planner/_form_draft_settings.html.erb b/app/views/podcast_planner/_form_draft_settings.html.erb index 3642b38df..4c4e8f0a0 100644 --- a/app/views/podcast_planner/_form_draft_settings.html.erb +++ b/app/views/podcast_planner/_form_draft_settings.html.erb @@ -16,7 +16,7 @@
- <%= form.select :publish_time, time_options %> + <%= form.select :publish_time, time_options, {selected: 12.hours.to_i}, data: {controller: "select-local-time"} %> <%= form.label :publish_time, t(".label.publish_time") %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 9402f3a06..287862a6e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -38,7 +38,7 @@ en: formats: default: "%-m/%-d/%Y %-I:%M %p %Z" short: "%m/%d" - time_12_hour: "%l:%M %p %Z" + time_12_hour: "%-l:%M %p %Z" day_and_date: "%A, %m/%d" # validation error messages https://guides.rubyonrails.org/i18n.html#error-message-interpolation activerecord: diff --git a/test/models/podcast_planner_test.rb b/test/models/podcast_planner_test.rb index f4afd0b5d..fd0440e6d 100644 --- a/test/models/podcast_planner_test.rb +++ b/test/models/podcast_planner_test.rb @@ -80,7 +80,7 @@ planner.dates = dates assert_equal planner.ready_to_generate_drafts?, false - planner.publish_time = DateTime.new(2001, 2, 3, 4, 30) + planner.publish_time = 4.hours.to_i + 30.minutes.to_i assert_equal planner.ready_to_generate_drafts?, false planner.medium = "video" @@ -96,6 +96,9 @@ assert_equal planner.ready_to_generate_drafts?, false planner.dates = dates + planner.publish_time = 0 + assert_equal planner.ready_to_generate_drafts?, true + planner.publish_time = nil assert_equal planner.ready_to_generate_drafts?, false end @@ -397,7 +400,7 @@ DateTime.new(2001, 7, 4) ] # 9:30 AM - planner.publish_time = DateTime.new(2001, 2, 3, 9, 30) + planner.publish_time = 9.hours.to_i + 30.minutes.to_i planner.segment_count = 2 end