[Rails] Notification function

Development environment

Synopsis

Create a notification model

Terminal


rails g model notification user:references task:references action:integer checked:boolean

db/migrate


class CreateNotifications < ActiveRecord::Migration[6.0]
  def change
    create_table :notifications do |t|
      t.references :task, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
      t.integer :action
      t.boolean :checked, default: false, null: false

      t.timestamps
    end
  end
end

Terminal


rails db:migrate

Various models

User model

app/models/user.rb


class User < ApplicationRecord
  has_many :notifications, dependent: :destroy
end

Task model

app/models/task.rb


class Task < ApplicationRecord
  belongs_to :user
  has_one :notification, dependent: :destroy
end

Notification model

app/models/ntification.rb


class Notification < ApplicationRecord
  default_scope->{order(created_at: :desc)}

  belongs_to :task
  belongs_to :user

  enum action: { warning: 1, expired: 2}
end

What is an enum

enum column name: {Name definition:Numerical value}
It is an enumeration type, and when actually registering, save the name definition.

view side

python


rails g controller notifications index

config/routes.rb


Rails.application.routes.draw do
  resources :notifications,only: [:index]
end

app/controllers/notifications_controller.rb


class NotificationsController < ApplicationController
  def index
    @notifications = current_user.notifications
    @notifications.where(checked: false).each do |notification|
      notification.update(checked: true)
    end
  end
end

Notify unread and read notifications

ruby:app/views/layouts/_header.html.slim


- if unchecked_notifications.any?
  p.text-danger ★
= link_to "notification",notifications_path, class: 'nav-link'

app/helpers/notifications_heler.rb


module NotificationsHelper
  def unchecked_notifications
    @notifications = current_user.notifications.where(checked: false)
  end
end

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  include NotificationsHelper
end

Create a rake task to run on a regular basis

What is Rake
Rake is a function that allows you to create code written in Ruby as a task and call and execute it as needed.

Here are some situations where you can use the rake task.

Linkage of some kind of data
Database backup
Update and delete data on a regular basis

As for adding a path to be autoloaded did not work, I write it directly in the rake file.

python


rails g task notification
rails g task filename

lib/tasks/notification.rake


namespace :task do
  desc "notification/Move once a day."
  task :notification => :environment do
    today = Date.today
    tasks = Task.all
    n = tasks.count
    today = Date.today
    tasks.each do |task|
      user = task.user
      if today + 1 == task.deadline.to_date
        Notification.create(task_id: task.id, user_id: user.id, action: "warning")
      end
      if task.notification.nil? && today > task.deadline.to_date
        Notification.create(task_id: task.id, user_id: user.id, action: "expired")
      elsif task.notification.present? && today > task.deadline.to_date
        task.notification.update(action: "expired")
      end
      n -= 1
      puts "remaining#{n}/#{tasks.count}"
    end
  end
end

Run

rake tasks:notification
rake [name of namespece]:[task name]

For the production environment

In my case, it's heroku, so it seems to use heroku scheduler. I will summarize it again. gem: whenever is a gem that has been added / rewritten to crontab.

[Addition] Introduced heroku scheduler

  1. Set the time and task you want to run on Heroku Scheduler
heroku run rake task:notification

Recommended Posts

[Rails] Notification function
[Rails 6] Ranking function
Rails follow function
[Rails] Implement search function
[Rails] Implemented hashtag function
[rails] tag ranking function
Rails search function implementation
Simple notification function in Rails (only when followed)
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
Rails Tutorial Extension: I created a follower notification function
[Rails 6] Pagination function implementation (kaminari)
[Rails DM] Let's create a notification function when DM is sent!
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
[Rails] Tag management function (using acts-as-taggable-on)
Implemented mail sending function with rails
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
[Ruby on Rails] CSV output function
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Rails basics
[Rails] Create an evaluation function using raty.js
Rails Review 1
[Rails] Function restrictions in devise (login / logout)
Rails API
Rails migration
[Rails] first_or_initialize
rails tutorial
About Rails 6
Rails Addition of easy and easy login function
[Rails withdrawal] Create a simple withdrawal function with rails
Rails foundation
Rails memorandum