4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails】Railsチュートリアルの追加機能~フォロワー通知 Vol.2~

Last updated at Posted at 2023-08-30

前回の記事では、フォロワー通知機能を実装しました。

前回の記事

今回は、この通知機能をON/OFFに切り替える方法について説明します。
ユーザーが自分にフォローされた際に通知を受け取るかどうかを設定できるようにします。

目次

データベースのカラムを追加する

まず、ユーザーテーブルに通知設定のON/OFFを管理するためのカラムを追加します。
これにより、各ユーザーが通知を受け取るかどうかを記録できます。

以下のコマンドを実行して、マイグレーションファイルを生成します。
rails generate migration add_follow_notification_to_users follow_notification:boolean

生成されたマイグレーションファイル(db/migrate/<日付>_add_follow_notification_to_user.rb)を開き、add_column の行を以下のように変更します:

db/migrate/<日付>_add_follow_notification_to_user.rb
add_column :users, :follow_notification, :boolean, default: false

これにより、通知をデフォルトでオフに設定します。

次に、データベースのスキーマを更新するために以下のコマンドを実行します。
rails db:migrate

ユーザー設定フォームを更新する

ユーザーが通知を受け取るかどうかを選択できるように、
ユーザー設定フォームにチェックボックスを追加します。

app/views/users/_form.html.erb ファイルを開き、submitボタンの上に以下のコードを追加します。

app/views/users/_form.html.erb
<%= f.label :follow_notification, class: "checkbox inline" do %>
    <%= f.check_box :follow_notification %>
    <span>フォローされた際にメール通知を受け取る</span>
<% end %>

これにより、ユーザーはプロフィールの編集画面・新規登録画面で
通知設定を変更できるようになります。

チェックボックスのスタイルを調整する

チェックボックスのスタイルを整えるために、
app/assets/stylesheets/custom.scss ファイルに以下のコードを追加します。

app/assets/stylesheets/custom.scss
#user_follow_notification {
  width: auto;
  margin-left: 0;
}

これにより、フォームの見た目が整えられます。

フォロー通知の送信条件を変更する

app/models/user.rb ファイルを開き、follow メソッドを以下のように変更します。

app/models/user.rb
def follow(other_user)
  active_relationships.create(followed_id: other_user.id)
  if other_user.follow_notification && self != other_user
    UserMailer.follow_notification(other_user, self).deliver_now
  end
end

通知がオンの場合かつ自分自身ではない場合にのみメールを送信するようになります。

テストデータの設定を変更する

テスト用のデータを設定するために、test/fixtures/users.yml ファイルを開き、
フォロワー通知をONとOFFに設定したユーザーを追加します。

例:

test/fixtures/users.yml
michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  follow_notification: true

inactive:
  name: Inactive User
  email: inactive@example.com
  password_digest: <%= User.digest('password') %>
  follow_notification: false

テストケースを追加する

通知のON/OFF切り替えが正しく機能していることを確認するために、テストケースを追加します。

test/integration/following_test.rb ファイルに以下のコードを追加します。

test/integration/following_test.rb
test "should send follow notification email" do
  post relationships_path, params: { followed_id: @user.id }
  assert_equal 1, ActionMailer::Base.deliveries.size
end

test "should not send unfollow notification email" do
  not_notify = @other
  post relationships_path, params: { followed_id: not_notify.id }
  assert_equal 0, ActionMailer::Base.deliveries.size
end

1つ目のテストでは通知がONの場合、
2つ目のテストでは通知がOFFの場合をテストしています。
これにより、通知のON/OFF切り替えが正しく機能することをテストで確認できます。

これで、ユーザーが通知設定を変更し、通知を受け取るかどうかを管理できるようになりました。

参考

4
0
0

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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?