如何改进这个通知系统?

How can this notification system be improved?

我正在寻找改进我所做的一段代码。我有一个通知系统,它运行良好,问题是它根本不是 DRY,维护它只是一场噩梦......我正在寻找一些帮助以正确的方式和 rails 方式。
请不要害怕:)

这是我的代码:

控制器:

class NotificationsController < ApplicationController

  def mynotifications
    @u_notifications_paginate = current_user.notifications.all.order('created_at DESC').paginate(:page => params[:page], :per_page => 10)
    @u_notifications_paginate.update read: true
  end

  def destroy
    if current_user
        notif = current_user.notifications.find_by_notification_uid!(params[:id])
        notif.destroy
        redirect_to mynotifications_path(locale: I18n.locale)
    else
        redirect_to root_path(locale: I18n.locale)
    end
  end

end

我的帮手:

module NotificationsHelper

    def user_notifications_number
        user.notifications.where(read: false).count
    end

    def user_notifications_paginate
        user.notifications.where(read: false).order('created_at DESC').last(3)
    end

    def user_notifications_exists?
        user.notifications.where(read: false).exists?
    end

    def newmember_reason?
        user.notifications.where("reason = ?", "New Member").exists?
    end

    def acceptgroup_reason?
        user.notifications.where("reason = ?", "Welcome").exists?
    end

    def deniedgroup_reason?
        user.notifications.where("reason = ?", "Denied").exists?
    end

    def new_ownership_change_reason?
        user.notifications.where("reason = ?", "New Owner").exists?
    end

    def left_ownership_change_reason?
        user.notifications.where("reason = ?", "Owner Left").exists?
    end

    def new_invitation_reason?
        user.notifications.where("reason = ?", "Group Invitation").exists?
    end

    def invitation_accepted_reason?
        user.notifications.where("reason = ?", "Accepted").exists?
    end

    def invitation_declined_reason?
        user.notifications.where("reason = ?", "Declined").exists?
    end
end

我的看法:

<% @u_notifications_paginate.each do |notif| %>
        <% if deniedgroup_reason? || invitation_declined_reason? %>
            <div class="notif-negative">
        <% elsif new_ownership_change_reason? || left_ownership_change_reason? %>
            <div class="notif-info">
        <% else %>
            <div class="notif-positive">
        <% end %>
                <h6 class="d-inline"><strong><%= notif.reason %></strong></h6>
                <div class="float-right d-inline"><%= notif.created_at.strftime("%A the %d/%m/%Y at %H:%M%p") %></div></br></br>
                <% if newmember_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.name %> <%= notif.notified_by.firstname %></strong> want's to join the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to deny_member_path(guid: notif.notified_by, auth_token: notif.group.auth_token, locale: I18n.locale), class: 'btn btn-danger float-right' do %>
                    <i class="fa fa-times"></i>
                    <% end %>
                    <%= link_to accept_member_path(guid: notif.notified_by, auth_token: notif.group.auth_token, locale: I18n.locale), class: 'btn btn-success float-right' do %>
                    <i class="fa fa-check"></i>
                    <% end %>
                <% elsif acceptgroup_reason? %>
                    <div class="d-inline">You have been accepted by <strong><%= notif.group.owner.firstname %></strong> in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-success float-right" do %>
                    <i class="fa fa-check"></i>
                    <% end %>
                <% elsif deniedgroup_reason? %>
                    <div class="d-inline">You have been refused to enter the <strong>Group</strong>. 'Token' (Share Key) : <%= notif.group.auth_token %>.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-danger float-right" do %>
                    <i class="fa fa-times"></i>
                    <% end %>
                <% elsif new_ownership_change_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.firstname %></strong> gave you the Ownership of the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-info float-right" do %>
                    <i class="fa fa-check"></i>
                    <% end %>
                <% elsif left_ownership_change_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.firstname %></strong> left the Group : <strong>'<%= notif.group.name %>'</strong> you are the new Owner.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-info float-right" do %>
                    <i class="fa fa-check"></i>
                    <% end %>   
                <% elsif new_invitation_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.firstname %></strong> invites you in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to deny_invitation_path(guid: notif.user, auth_token: notif.group.auth_token, locale: I18n.locale), class: 'btn btn-danger float-right' do %>
                    <i class="fa fa-times"></i>
                    <% end %>
                    <%= link_to accept_invitation_path(guid: notif.user, auth_token: notif.group.auth_token, locale: I18n.locale), class: 'btn btn-success float-right' do %>
                    <i class="fa fa-check"></i>
                    <% end %>
                <% elsif invitation_accepted_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.firstname %></strong> accepted the Invitation in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-success float-right" do %>
                    <i class="fa fa-check"></i>
                    <% end %>
                <% elsif invitation_declined_reason? %>
                    <div class="d-inline"><strong><%= notif.notified_by.firstname %></strong> refused the Invitation in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                    <%= link_to notification_path(id: notif.notification_uid, locale: I18n.locale), method: :delete, class: "btn btn-danger float-right" do %>
                    <i class="fa fa-times"></i>
                    <% end %>
                <% end %>
            </div></br>
    <% end %>

我的导航栏:

<div class="dropdown-menu" aria-labelledby="userNotification">
          <% user_notifications_paginate.each do |notif| %>
            <div class="dropdown-item card-pad">
                <h6 class="d-inline"><strong><%= notif.reason %></strong></h6>
                <div class="float-right d-inline"><%= notif.created_at.strftime("%d/%m/%Y") %></div>
              <% if newmember_reason? %>
                <div><strong><%= notif.notified_by.firstname %></strong> want's to join the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif acceptgroup_reason? %>
                <div>You have been accepted by <strong><%= notif.group.owner.firstname %></strong> in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif deniedgroup_reason? %>
                <div>You have been refused to enter the <strong>Group</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif new_ownership_change_reason? %>
                <div>You are the new Owner of the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif left_ownership_change_reason? %>
                <div><strong><%= notif.notified_by.firstname %></strong> left the Group : <strong>'<%= notif.group.name %>'</strong> you are the new Owner.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif new_invitation_reason? %>
                <div><strong><%= notif.notified_by.firstname %></strong> invites you in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif invitation_accepted_reason? %>
                <div><strong><%= notif.notified_by.firstname %></strong> accepted the Invitation in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% elsif invitation_declined_reason? %>
                <div><strong><%= notif.notified_by.firstname %></strong> refused the Invitation in the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
                <sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>
              <% end %>
              <hr class="col-5">
            </div>
          <% end %>
            <% if user_notifications_exists? %>
              <div class="dropdown-divider"></div>
              <%= link_to(t(".notif_showall"), mynotifications_path(locale: I18n.locale), class: 'dropdown-item center') %>
            <% else %>
              <div class="center"><%= t(".notif_no_notif") %></div>
            <% end %>
        </div>

我很惭愧地表示真的...

不管怎样,我在这里基本上是,如果用户有通知,导航栏中会显示一个弹出窗口,然后您可以单击通知并在视图中阅读它。一旦用户进入视图,就会读取通知(切换布尔值)。
要创建通知,我使用这个:

Notification.create!(read: false, user_id: user.id, notified_by_id: group.owner.id, group_id: group.id, reason: "Welcome")

例如,这段代码在我处理成员资格的控制器中,这一段代码,更具体地说,是添加一个成员,并且创建此通知是为了通知用户他已被接受到组中。

我想你能看出我的问题,而且很明显。在我看来,我正在使用条件调用 good "reason",因此,根据该条件显示正确的文本。我多次重复我的代码,这真的很糟糕,我知道。我的问题是我该如何改进它。我不是在寻找直接的答案或代码,我只是想知道我如何才能做得更好,以及你们是否有一些技巧可以使它能够维护。

非常感谢。

这里有一个简单而有效的想法:不要在通知及其 html 表示之间对 link 进行硬编码。相反,让通知自己定义它们应该如何呈现。比如让每个通知都有一个字段partial_name.

<Notification id: 1, partial_name: 'newmember', ...>
<Notification id: 2, partial_name: 'accept_group', ...>

然后你提取相应的部分

views/notifications/types/_newmember.html.erb

<div><strong><%= notif.notified_by.firstname %></strong> want's to join the Group : <strong>'<%= notif.group.name %>'</strong>.</div>
<sub><%= link_to(t(".notif_readmore"), mynotifications_path(locale: I18n.locale)) %></sub>

然后你的循环变得非常简单:

<% user_notifications_paginate.each do |notif| %>
  <%= render partial: "notifications/types/#{notif.partial_name}", locals: { notif: notif } %>
<% end %>

我可能漏掉了一些小细节,但这是总体思路。