Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper text to datetime-local form fields #833

Merged
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
16 changes: 15 additions & 1 deletion app/helpers/maintenance_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def parameter_field(form_builder, parameter_name)
when ActiveModel::Type::Decimal, ActiveModel::Type::Float
form_builder.number_field(parameter_name, { step: "any" })
when ActiveModel::Type::DateTime
form_builder.datetime_field(parameter_name)
form_builder.datetime_field(parameter_name) + datetime_field_help_text
when ActiveModel::Type::Date
form_builder.date_field(parameter_name)
when ActiveModel::Type::Time
Expand All @@ -120,5 +120,19 @@ def parameter_field(form_builder, parameter_name)
form_builder.text_area(parameter_name, class: "textarea")
end
end

# Return helper text for the datetime-local form field.
def datetime_field_help_text
text =
if Time.zone_default.nil? || Time.zone_default.name == "UTC"
"Timezone: UTC."
else
"Timezone: #{Time.now.zone}."
end
tag.div(
tag.p(text),
class: "content is-small",
)
end
end
end
2 changes: 1 addition & 1 deletion test/dummy/app/tasks/maintenance/params_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def collection
def process(post)
sleep(1) unless self.class.fast_task

post.update!(content: "New content added on #{Time.now.utc}")
post.update!(content: "New content added on #{Time.now.utc}:\ndatetime_attr: #{datetime_attr.inspect}")
end

private
Expand Down
54 changes: 54 additions & 0 deletions test/helpers/maintenance_tasks/tasks_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,59 @@ class TasksHelperTest < ActionView::TestCase
assert_match %r{rails/active_storage/blobs/\S+/sample.csv},
csv_file_download_path(run)
end

test "#parameter_field adds information about datetime fields when Time.zone_default is not set" do
with_zone_default(nil) do
markup = render(inline: <<~TEMPLATE)
<%= fields_for(Maintenance::ParamsTask.new) do |form| %>
<%= parameter_field(form, 'datetime_attr') %>
<% end %>
TEMPLATE
assert_match "UTC", markup
end
end

test "#datetime_field_help_text is correct about the timezone when Time.zone_default is not set/default" do
with_zone_default(nil) do
value = ActiveModel::Type::DateTime.new.cast("2023-06-01T12:00:00") # a local ISO8601 time (no timezone)
assert_predicate(value, :utc?) # uses UTC
end
end

test "#parameter_field adds information about datetime fields when Time.zone_default is set" do
with_zone_default(Time.find_zone!("EST")) do # ignored
markup = render(inline: <<~TEMPLATE)
<%= fields_for(Maintenance::ParamsTask.new) do |form| %>
<%= parameter_field(form, 'datetime_attr') %>
<% end %>
TEMPLATE
assert_match Time.now.zone.to_s, markup
end
end

test "#datetime_field_help_text is correct about the timezone when Time.zone_default is set" do
now = Time.now
sign = now.utc_offset > 0 ? -1 : 1 # make sure we don't overflow
sign = -1
# Time.use_zone leaks Time.zone_default in the IsolatedExecutionState, so it needs to be called before changing
# Time.zone_default
Time.use_zone(now.utc_offset + 2.hour * sign) do # a timezone that is not the system timezone
with_zone_default(Time.find_zone!(now.utc_offset + 1.hour * sign)) do # another non-system timezone
value = ActiveModel::Type::DateTime.new.cast("2023-06-01T12:00:00") # a local ISO8601 time (no timezone)
refute_predicate(value, :utc?)
assert_equal(now.utc_offset, value.utc_offset) # uses system time zone
end
end
end

private

def with_zone_default(new_zone)
zone = Time.zone_default
Time.zone_default = new_zone
yield
ensure
Time.zone_default = zone
end
end
end