From a3282b8542fa08673c68f72eeaf7d15914e8b6b7 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 22 Jul 2024 18:52:41 +0300 Subject: [PATCH] Select closest value to remaining duration when editing a block --- app/views/user_blocks/edit.html.erb | 3 +- .../user_blocks_controller_test.rb | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/views/user_blocks/edit.html.erb b/app/views/user_blocks/edit.html.erb index a5b165e9ad..88441a15d9 100644 --- a/app/views/user_blocks/edit.html.erb +++ b/app/views/user_blocks/edit.html.erb @@ -15,7 +15,8 @@ <%= f.form_group do %> <%= label_tag "user_block_period", t(".period"), :class => "form-label" %> <%= select_tag("user_block_period", - options_for_select(UserBlock::PERIODS.collect { |h| [block_duration_in_words(h.hours), h.to_s] }, params[:user_block_period]), + options_for_select(UserBlock::PERIODS.collect { |h| [block_duration_in_words(h.hours), h.to_s] }, + UserBlock::PERIODS.min_by { |h| (params[:user_block_period].to_i - h).abs }), :class => "form-select") %> <% end %> diff --git a/test/controllers/user_blocks_controller_test.rb b/test/controllers/user_blocks_controller_test.rb index 97f5171335..b6aaf4ca82 100644 --- a/test/controllers/user_blocks_controller_test.rb +++ b/test/controllers/user_blocks_controller_test.rb @@ -244,6 +244,34 @@ def test_edit assert_select "p", "Sorry, the user block with ID 99999 could not be found." end + ## + # test the edit action when the remaining block duration doesn't match the available select options + def test_edit_duration + moderator_user = create(:moderator_user) + + freeze_time do + active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours) + + session_for(moderator_user) + get edit_user_block_path(active_block) + + assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do + assert_select "select#user_block_period", :count => 1 do + assert_select "option[value='96'][selected]", :count => 1 + end + end + + travel 2.hours + get edit_user_block_path(active_block) + + assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do + assert_select "select#user_block_period", :count => 1 do + assert_select "option[value='96'][selected]", :count => 1 + end + end + end + end + ## # test the create action def test_create