我希望改进我所做的一段代码。我有一个通知系统,它运作良好,问题是它根本不干,并且维持它只是一场噩梦...我是寻求一些帮助,以适当的方式和轨道方式完成这项工作 请不要害怕:)
这是我的代码:
控制器:
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")
我非常羞于表明真的......
无论如何我在这里,基本上如果用户有通知,导航栏中会显示弹出窗口,您可以点击通知并在视图中阅读。一旦用户进入视图,就会读取通知(切换布尔值) 要创建通知,我使用它:
{{1}}
这段代码例如在我的控制器中处理成员资格,更具体地说,这是添加成员,并且创建此通知是为了通知用户他已被群组接受。
我认为你可以看到我的问题而且非常明显。在我看来,我正在玩这个条件来称呼好的&#34;原因&#34;所以,根据它显示正确的文本。我多次重复我的代码,这真的太可怕了,我知道。我的问题是如何才能改善这一点。我不是在寻找一个直接的答案或代码,我只是想知道如何做得更好,如果你们有一些技巧可以使它能够维持。
非常感谢。
答案 0 :(得分:1)
这是一个简单而有效的想法:不要对通知及其html表示之间的链接进行硬编码。相反,让通知本身定义它们应该如何呈现。例如,让每个通知都有一个字段partial_name
。
<Notification id: 1, partial_name: 'newmember', ...>
<Notification id: 2, partial_name: 'accept_group', ...>
然后你提取相应的部分
<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 %>
我可能错过了一些细节,但这是一般的想法。