내 레일 앱의 알림 모델에 다음 코드가 있습니다.
나는 그것을 조금 리팩토링하고 싶습니다. 확실히 잘못된 것은 "객체가 존재하는지 확인한 다음 그것으로 무언가를한다"부분입니다. 나는 사용할 수 있다고 생각 try
하지만 정확히 어떻게 작동하는지 모르겠습니다. sby가 어떻게해야하는지 말해 줄 수 있나요 (그것 try
보다 더 좋은 방법이 있다면)?
나머지 코드와 관련하여 다른 관찰이 있습니까?
#check and decrease chat notification that happens between 2 given users (since 1v1 chat notification number max limit is 1)
def self.decreasing_chat_notification_number(current_user, user)
if self.between_chat_recipient(current_user, user).unchecked.any?
notification = self.between_chat_recipient(current_user, user).unchecked.first
checking_and_decreasing_notification(notification)
end
end
#check and decrease all the task notifications that happens between 2 given users
def self.decreasing_task_notification_number(current_user, user)
if self.task.between_other_recipient(current_user, user).unchecked
self.task.between_other_recipient(current_user, user).unchecked.each do |notification|
checking_and_decreasing_notification(notification)
end
end
end
#check and decrease all the post notifications that happened on a given post
def self.decreeasing_post_notification_number(current_user, post)
if self.post.this_post_comments(current_user, post).unchecked
self.post.this_post_comments(current_user, post).unchecked.each do |notification|
checking_and_decreasing_notification(notification)
end
end
end
private
def check_notification #chat notification gets checked
if self.checked_at == nil
update_attributes(checked_at: Time.zone.now)
end
end
def checking_and_decreasing_notification(notification)
notification.check_notification
current_user.decrease_new_other_notifications
current_user.decreased_other_number_pusher
end
범위 :
scope :not_chat, -> { where.not(notifiable_type: "Message") }
scope :chat, -> { where(notifiable_type: "Message") }
scope :task, -> { where(notifiable_type: "Task") }
scope :post, -> { where(notifiable_type: "Post") }
scope :checked, -> { where.not(checked_at: nil) }
scope :unchecked, -> { where(checked_at: nil) }
scope :this_post_comments, -> (recipient_id, post_id) do
where("notifications.recipient_id = ? AND notifications.notifiable_id = ?", recipient_id, post_id)
end
scope :between_chat_recipient, -> (recipient_id, sender_id) do
where("notifications.recipient_id = ? AND notifications.sender_id = ? AND notifications.notifiable_type = ?", recipient_id, sender_id, "Message")
end
scope :between_other_recipient, -> (recipient_id, sender_id) do
where("notifications.recipient_id = ? AND notifications.sender_id = ?", recipient_id, sender_id)
end
최신 정보:
모든 것은 Notification.class_method 대신 인스턴스 메소드로 current_user에서 호출됩니다. Btw. 사용자 테이블에는 현재 알림 수를 계산하는 "new_chat_notification",default: 0
및 "new_other_notification", default: 0
속성이 있습니다.
user.rb
#check and decrease chat notification that happens between 2 given users (max 1)
def decreasing_chat_notification_number(user)
notification = self.notifications.between_chat_recipient(user).unchecked.first
self.checking_and_decreasing_notification(notification) if notification.present?
end
#check and decrease task notifications that happens between 2 given users
def decreasing_task_notification_number(user)
self.notifications.task.between_other_recipient(user).unchecked.each do |notification|
self.checking_and_decreasing_notification(notification)
end
end
#check and decrease the post notification that belongs to a given post
def decreasing_post_notification_number(post_id)
self.notifications.this_post_comments(post_id).unchecked.each do |notification|
self.checking_and_decreasing_notification(notification)
end
end
def checking_and_decreasing_notification(notification)
notification.check_notification
if notification.notifiable_type == "Message"
self.decrease_new_chat_notifications
self.decreased_chat_number_pusher
else
self.decrease_new_other_notifications
self.decreased_other_number_pusher
end
end
def decrease_new_chat_notifications
decrement!(:new_chat_notification) if self.new_chat_notification > 0
end
def decrease_new_other_notifications
decrement!(:new_other_notification) if self.new_other_notification > 0
end
def decreased_chat_number_pusher
number = self.new_chat_notification
Pusher['private-'+ self.id.to_s].trigger('new_chat_notification', {:number => number})
end
def decreased_other_number_pusher
number = self.new_other_notification
Pusher['private-'+ self.id.to_s].trigger('new_other_notification', {:number => number})
end
notification.rb
scope :not_chat, -> { where.not(notifiable_type: "Message") }
scope :chat, -> { where(notifiable_type: "Message") }
scope :task, -> { where(notifiable_type: "Task") }
scope :post, -> { where(notifiable_type: "Post") }
scope :checked, -> { where.not(checked_at: nil) }
scope :unchecked, -> { where(checked_at: nil) }
scope :this_post_comments, -> (post_id) do
where("notifications.notifiable_id = ?", post_id)
end
scope :between_chat_recipient, -> (sender_id) do
where("notifications.sender_id = ? AND notifications.notifiable_type = ?", sender_id, "Message")
end
scope :between_other_recipient, -> (sender_id) do
where("notifications.sender_id = ? AND notifications.notifiable_type != ?", sender_id, "Message")
end
def check_notification #chat notification gets checked
update_attribute(:checked_at, Time.zone.now) if self.checked_at.nil?
end
try
개체에 메시지를 보내려고 시도 하는 데 사용할 수 있습니다 . 객체가 응답하면 실행되고, 그렇지 않으면 무시됩니다.
string = "foo"
string.try(:length) # => 3
nil.try(:length) => nil
try!
대신 사용 하는 것이 좋습니다 . 수신자가 메시지에 응답하지 않고 응답하지 않으면 발생합니다.nil
그러나 그것은 여기서 당신을별로 도움이되지 않을 것입니다.
반복하면 무언가 존재하는지 확인할 필요가 없습니다. 반복 할 수 있으며 연결이 "비어 있으면"아무 일도 일어나지 않습니다.
예를 들어 다음을 다시 작성할 수 있습니다 decreeasing_post_notification_number
.
def self.decreeasing_post_notification_number(current_user, post)
self.post.this_post_comments(current_user, post).unchecked.each do |notification|
checking_and_decreasing_notification(notification)
end
end
동일은 간다 decreasing_chat_notification_number
가 호출 괜찮 IFF checking_and_decreasing_notification
에 대한 each
뿐 아니라first
여기에 전체 코드가 표시되지 않으므로 몇 가지 가정을해야합니다. 코드가 OO처럼 보이지 않습니다 ( self
필요한 모든 값을 매개 변수로받는 메서드가 있습니다). 다음 중 하나로 리팩토링하는 것이 좋습니다.
BTW : Time.zone.now
그냥 사용하는 것이 아니라 사용하는 것이 좋습니다 Time.now
!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다