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 :user_id, presence: true
|
||||||
validates :answer_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)
|
def notification_type(*_args)
|
||||||
Notifications::Smiled
|
Notifications::Smiled
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,12 +43,6 @@ class User < ActiveRecord::Base
|
||||||
end unless website.blank?
|
end unless website.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
before_destroy do
|
|
||||||
friends.each do |u|
|
|
||||||
unfollow u
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def login=(login)
|
def login=(login)
|
||||||
@login = login
|
@login = login
|
||||||
end
|
end
|
||||||
|
@ -89,19 +83,13 @@ class User < ActiveRecord::Base
|
||||||
# smiles an answer
|
# smiles an answer
|
||||||
# @param answer [Answer] the answer to smile
|
# @param answer [Answer] the answer to smile
|
||||||
def smile(answer)
|
def smile(answer)
|
||||||
smile = Smile.create(user: self, answer: answer)
|
Smile.create(user: self, answer: answer)
|
||||||
Notification.notify answer.user, smile unless answer.user == self
|
|
||||||
increment! :smiled_count
|
|
||||||
answer.increment! :smile_count
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# unsmile an answer
|
# unsmile an answer
|
||||||
# @param answer [Answer] the answer to unsmile
|
# @param answer [Answer] the answer to unsmile
|
||||||
def unsmile(answer)
|
def unsmile(answer)
|
||||||
smile = Smile.find_by(user: self, answer: answer).destroy
|
Smile.find_by(user: self, answer: answer).destroy
|
||||||
Notification.denotify answer.user, smile unless answer.user == self
|
|
||||||
decrement! :smiled_count
|
|
||||||
answer.decrement! :smile_count
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def smiled?(answer)
|
def smiled?(answer)
|
||||||
|
|
Loading…
Reference in New Issue