]> git.openstreetmap.org Git - rails.git/commitdiff
Select closest value to remaining duration when editing a block
authorAnton Khorev <tony29@yandex.ru>
Mon, 22 Jul 2024 15:52:41 +0000 (18:52 +0300)
committerAnton Khorev <tony29@yandex.ru>
Mon, 22 Jul 2024 15:58:43 +0000 (18:58 +0300)
app/views/user_blocks/edit.html.erb
test/controllers/user_blocks_controller_test.rb

index a5b165e9ad790e5c8b918b471a775ebf4091d0cf..88441a15d9ccbb2d80c694fa425c77a527372cf8 100644 (file)
@@ -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 %>
 
index 97f5171335d4910fd27e53fda34bc6ea8dc8e2f5..b6aaf4ca8267256ece52d00f6eb70c4c29c27e94 100644 (file)
@@ -244,6 +244,34 @@ class UserBlocksControllerTest < ActionDispatch::IntegrationTest
     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