LoginSignup
0

More than 1 year has passed since last update.

redmine管理画面のuser一覧に、メール通知機能の情報を追加する

Last updated at Posted at 2022-03-21

■やりたいこと

 管理画面のuser一覧に、メール通知機能の情報も表示させたい。

■環境:

Environment:
Redmine version 4.2.3.stable
Ruby version 2.6.9-p207 (2021-11-24) [x64-mingw32]
Rails version 5.2.6
Environment production
Database adapter Mysql2
Mailer queue ActiveJob::QueueAdapters::AsyncAdapter
Mailer delivery smtp
SCM:
Git 2.34.0
Filesystem
Redmine plugins:
no plugin installed

■調査(app\views\users_mail_notifications.html.erb)

まずはメール通知機能を調査したら、3項目あった。欲しい情報は一番目の項目なので、これを一覧表示に追加できないかを検討する。
ソースコード(_mail_notifications.html.erb)の情報をログ出力させて調べると@user.mail_notificatioの変数に選択されているメール通知機能が格納されており
また、user_mail_notification_options(@user)関数でメール通知機能の選択肢が2次元配列で取得できる事が分かった。

・メール通知機能(3項目)
image.png

_mail_notifications.html.erb
<p>
<%= label_tag "user_mail_notification", l(:description_user_mail_notification), :class => "hidden-for-sighted" %>
<%= select_tag(
      'user[mail_notification]',
      options_for_select(
         user_mail_notification_options(@user), @user.mail_notification),
      :onchange => 'if (this.value == "selected") {$("#notified-projects").show();} else {$("#notified-projects").hide();}'
     ) %>
</p>

・ログ出力結果
@user.mail_notificatio:only_my_events
user_mail_notification_options(@user):|[["参加しているプロジェクトのすべての通知", "all"], ["ウォッチ中または自分が関係しているもの", "only_my_events"], ["ウォッチ中または自分が担当しているもの", "only_assigned"], ["ウォッチ中または自分が作成したもの", "only_owner"], ["通知しない", "none"]]|

■実装(app\views\users\index.html.erb)

調査で得られた、メール通知機能の情報を基にindex.html.erbを以下に修正する。
redmineを再起動し正しく動作しているか確認する。
※ソースコード変更箇所は差分参照

index.html.erb
・・・
<% if @users.any? %>
<div class="autoscroll">
<table class="list users">
  <thead><tr>
  <%= sort_header_tag('login', :caption => l(:field_login)) %>
  <%= sort_header_tag('firstname', :caption => l(:field_firstname)) %>
  <%= sort_header_tag('lastname', :caption => l(:field_lastname)) %>
  <th><%= l(:field_mail) %></th>
  <%= sort_header_tag('admin', :caption => l(:field_admin), :default_order => 'desc') %>
  <%= sort_header_tag('created_on', :caption => l(:field_created_on), :default_order => 'desc') %>
  <%= sort_header_tag('last_login_on', :caption => l(:field_last_login_on), :default_order => 'desc') %>
  <%= sort_header_tag('mail_notification', :caption => l(:field_mail_notification), :default_order => 'desc') %>
    <th></th>
  </tr></thead>
  <tbody>
<% for user in @users -%>
  <tr class="<%= user.css_classes %>">
  <td class="username"><%= avatar(user, :size => "14") %><%= link_to user.login, edit_user_path(user) %></td>
  <td class="firstname"><%= user.firstname %></td>
  <td class="lastname"><%= user.lastname %></td>
  <td class="email"><%= mail_to(user.mail) %></td>
  <td class="tick"><%= checked_image user.admin? %></td>
  <td class="created_on"><%= format_time(user.created_on) %></td>
  <td class="last_login_on"><%= format_time(user.last_login_on) unless user.last_login_on.nil? %></td>
  <td class="mail">
<% user_mail_notification_options(user).each do |youso| %>
<%  if youso[1] == user.mail_notification %>
<%=  youso[0] %>
<%  end %>
<% end %>
  </td>
    <td class="buttons">
      <%= change_status_link(user) %>
      <%= delete_link user_path(user, :back_url => request.original_fullpath), :data => {} unless User.current == user %>
    </td>
  </tr>
<% end -%>
・・・

■ソースコード差分

image.png

■確認結果

image.png

以上です、お疲れ様です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0