From 2c29dd9a182a3002433b9e0cf0417f631e043fda Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 28 Dec 2014 21:46:57 +0100 Subject: [PATCH] even more after_create and before_destroy --- app/models/smile.rb | 12 ++++++++++++ app/models/user.rb | 16 ++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/models/smile.rb b/app/models/smile.rb index b73d5386..e94c1bd4 100644 --- a/app/models/smile.rb +++ b/app/models/smile.rb @@ -4,6 +4,18 @@ class Smile < ActiveRecord::Base validates :user_id, presence: true validates :answer_id, presence: true + after_create do + Notification.notify answer.user, self unless answer.user == user + user.increment! :smiled_count + answer.increment! :smile_count + end + + before_destroy do + Notification.denotify answer.user, self unless answer.user == user + user.decrement! :smiled_count + answer.decrement! :smile_count + end + def notification_type(*_args) Notifications::Smiled end diff --git a/app/models/user.rb b/app/models/user.rb index d0e30692..09e15219 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -43,12 +43,6 @@ class User < ActiveRecord::Base end unless website.blank? end - before_destroy do - friends.each do |u| - unfollow u - end - end - def login=(login) @login = login end @@ -89,19 +83,13 @@ class User < ActiveRecord::Base # smiles an answer # @param answer [Answer] the answer to smile def smile(answer) - smile = Smile.create(user: self, answer: answer) - Notification.notify answer.user, smile unless answer.user == self - increment! :smiled_count - answer.increment! :smile_count + Smile.create(user: self, answer: answer) end # unsmile an answer # @param answer [Answer] the answer to unsmile def unsmile(answer) - smile = Smile.find_by(user: self, answer: answer).destroy - Notification.denotify answer.user, smile unless answer.user == self - decrement! :smiled_count - answer.decrement! :smile_count + Smile.find_by(user: self, answer: answer).destroy end def smiled?(answer)