【发布时间】: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