even more after_create and before_destroy
This commit is contained in:
parent
7d157cde8f
commit
2c29dd9a18
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue