Skip to content

Commit

Permalink
コントローラーでしていた処理をモデルで処理する
Browse files Browse the repository at this point in the history
  • Loading branch information
obregonia1 committed Jun 15, 2021
1 parent 65c6512 commit fcc37b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 8 additions & 8 deletions app/controllers/api/notifications/unread_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class API::Notifications::UnreadController < API::BaseController
def index
checked_unread_notifications = current_user.notifications.checked_unreads
checked_unread_notifications.each do |notification|
notification_path = notification[:path]
model = notification_path.split('/')[1].classify.constantize
id = notification_path.split('/')[2]
notification_path = notification.notified_path
model = notification.notified_model
id = notification.notified_id
comments = model.find(id).comments
if comments.pluck(:user_id).include?(notification.sender.id)
latest_notification = current_user.notifications.unreads.where(path: notification_path)[0]
new_message = notification.message.gsub(/を確認しました。/, 'の確認とコメントをしました。')
notification.update(message: new_message, kind: 'check_and_comment', created_at: latest_notification.created_at + 3)
end
next unless comments.pluck(:user_id).include?(notification.sender.id)

latest_notification = current_user.notifications.unreads.where(path: notification_path).maximum(:created_at)
new_message = notification.message.gsub(/を確認しました。/, 'の確認とコメントをしました。')
notification.update(message: new_message, kind: 'check_and_comment', created_at: latest_notification + 3)
end
@notifications = if params[:page]
current_user.notifications
Expand Down
12 changes: 12 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,16 @@ def self.following_report(report, receiver)
def self.into_one
select(:path).group(:path).maximum(:created_at)
end

def notified_path
self[:path]
end

def notified_model
notified_path.split('/')[1].classify.constantize
end

def notified_id
notified_path.split('/')[2]
end
end

0 comments on commit fcc37b7

Please sign in to comment.