【问题标题】:How can this notification system be improved?如何改进这个通知系统?
【发布时间】:2018-02-19 07:44:17
【问题描述】:

我希望改进我所做的一段代码。我有一个通知系统,它运行良好,问题是它根本不是 DRY,要保持它只是一场噩梦......我正在寻找一些帮助,以正确的方式和轨道完成这项工作方式。
请不要害怕:)

这是我的代码:

控制器:

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")

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

我认为您可以看到我的问题,而且很明显。在我看来,我正在使用条件来调用良好的“原因”,因此,根据该条件显示正确的文本。我重复我的代码好几次,这真的很可怕,我知道。我的问题是如何改进它。我不是在寻找一个直接的答案或代码,我只是想知道我怎样才能做得更好,以及你们是否有一些技巧可以使它能够维持。

非常感谢。

【问题讨论】:

  • 您可以从修复代码缩进开始。 (此处发布的 ERB 似乎还不错,但控制器......)。仅此一项就可以使该代码的可维护性提高 10 分。 :)
  • 抱歉,如果有点乱。我应该打电话给 Notification.all 以外的东西吗?也许如果我得到 Notification.where("reason = ?" .... 在控制器中,在我看来,我会遍历它们中的每一个?
  • 你不需要.all,顺便说一句。
  • 是的 .. 是的...对不起,我不是专业人士

标签: ruby-on-rails notifications ruby-on-rails-5 dry maintainability


【解决方案1】:

这里有一个简单而有效的想法:不要对通知和它的 html 表示之间的链接进行硬编码。相反,让通知本身定义它们应该如何呈现。例如,让每个通知都有一个字段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 %>

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

【讨论】:

  • 哇好,非常好。我实际上可以使用我的原因字段而不是新字段partial_name 对吗?谢谢,我知道你想用这个去哪里,我会试一试
  • @Asso:是的,你完全可以重复使用你的reason 字段。
  • @Asso: JFYI,你不必必须在你所有的问题前加上“rails 5.1”(除非问题真的是这个版本特有的,这不是您过去所有问题的案例)。另外,人们会知道您正在使用标签中的 rails 5,[ruby-on-rails-5]
  • 我有很多东西要修改,但效果很好,非常感谢!
  • 感谢您的提示;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2020-05-01
  • 2012-03-11
  • 1970-01-01
  • 2016-08-09
相关资源
最近更新 更多