update(hoge: "piyo", completed_at: Time.current) task.comments.each do |comment| comment.update( hoge: fuga) end task.owner.update( "piyo") if task.piyopiyo? UserMailer.send_hoge_mail(task).deliver_later if task.ponyo? # みたいな処理が100行 end end
`completed_at` を更新するメソッドがいたりすると責務が曖昧になる class CompleteTaskService def call(task:, user:) update(hoge: "piyo", completed_at: Time.current) task.comments.each do |comment| comment.update( hoge: fuga) end task.owner.update( piyo: "piyo") if task.piyopiyo? UserMailer.send_hoge_mail(task).deliver_later if task.ponyo? # みたいな処理が100行 end end
content:, author:) # タスクが完了してたらコメントは追加できない if task.completed? raise "This task is already completed" end # タスクの作者はコメントできない。変な仕様だけど例なので許してください :bow: if task.owner?(author) raise "Task owner can’t add comment" end Comment.create!(body: content, task_id: task.id, author_id: author.id) end end
if completed? raise "This task is already completed" end # タスクの作者はコメントできない。変な仕様だけど例なので許してください :bow: if owner?(author) raise "Task owner can't add comment" end task.comments.create!( body: content, task_id: id, author_id: author.id) end end
tasks @user = user end def complete_all @tasks.each do |task| task.complete end UserMailer.tasks_completed(user: @user, tasks: @tasks).deliver_now end # 省略 def expired_tasks end end