From b82fcecf27eb8d36f3c9adb465e6a74c40489c87 Mon Sep 17 00:00:00 2001
From: "Yuya.Nishida." <yuya@j96.org>
Date: Sun, 23 Mar 2025 22:09:48 +0900
Subject: [PATCH] fix: "Email notification" value in /my/account when empty
 string users.mail_notification

---
 app/models/user.rb                           |  4 ++++
 app/views/users/_mail_notifications.html.erb |  2 +-
 test/functional/my_controller_test.rb        | 10 ++++++++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/app/models/user.rb b/app/models/user.rb
index 4ce63f809..86fbc8fec 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -157,6 +157,10 @@ class User < Principal
     end
   end)
 
+  def normalized_mail_notification
+    mail_notification.presence || 'none'
+  end
+
   def set_mail_notification
     self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
     true
diff --git a/app/views/users/_mail_notifications.html.erb b/app/views/users/_mail_notifications.html.erb
index abf890e46..c441ec41a 100644
--- a/app/views/users/_mail_notifications.html.erb
+++ b/app/views/users/_mail_notifications.html.erb
@@ -3,7 +3,7 @@
 <%= select_tag(
       'user[mail_notification]',
       options_for_select(
-         user_mail_notification_options(@user), @user.mail_notification),
+         user_mail_notification_options(@user), @user.normalized_mail_notification),
       :onchange => 'if (this.value == "selected") {$("#notified-projects").show();} else {$("#notified-projects").hide();}'
      ) %>
 </p>
diff --git a/test/functional/my_controller_test.rb b/test/functional/my_controller_test.rb
index 342c97a3d..9be5f0a1a 100644
--- a/test/functional/my_controller_test.rb
+++ b/test/functional/my_controller_test.rb
@@ -416,6 +416,16 @@ def test_my_account_should_not_show_language_select_with_force_default_language_
     end
   end
 
+  def test_my_account_should_show_no_events_email_notification_when_empty_mail_notification
+    user = User.find(2)
+    user.mail_notification = ''
+    user.save(validate: false)
+
+    get :account
+    assert_response :success
+    assert_select 'select[name=?] option[selected]', 'user[mail_notification]', text: 'No events'
+  end
+
   def test_page_with_calendar
     date = '2020-10-21'
     subject = 'calendar on my page'
-- 
2.49.0

