Refactor smiling answers to using reactions
This commit is contained in:
parent
1e53f00032
commit
5f0a546353
|
@ -4,7 +4,7 @@ class Answer < ApplicationRecord
|
|||
belongs_to :user
|
||||
belongs_to :question
|
||||
has_many :comments, dependent: :destroy
|
||||
has_many :smiles, dependent: :destroy
|
||||
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
|
||||
has_many :subscriptions, dependent: :destroy
|
||||
has_many :comment_smiles, through: :comments, source: :smiles
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Appendable::Reaction < Appendable
|
||||
end
|
||||
|
|
|
@ -8,6 +8,7 @@ class User < ApplicationRecord
|
|||
include User::BanMethods
|
||||
include User::InboxMethods
|
||||
include User::QuestionMethods
|
||||
include User::ReactionMethods
|
||||
include User::RelationshipMethods
|
||||
include User::TimelineMethods
|
||||
include ActiveModel::OneTimePassword
|
||||
|
@ -30,7 +31,7 @@ class User < ApplicationRecord
|
|||
has_many :answers, dependent: :destroy
|
||||
has_many :comments, dependent: :destroy
|
||||
has_many :inboxes, dependent: :destroy
|
||||
has_many :smiles, dependent: :destroy
|
||||
has_many :smiles, class_name: "Appendable::Reaction", dependent: :destroy
|
||||
has_many :comment_smiles, dependent: :destroy
|
||||
has_many :services, dependent: :destroy
|
||||
has_many :notifications, foreign_key: :recipient_id, dependent: :destroy
|
||||
|
@ -126,23 +127,6 @@ class User < ApplicationRecord
|
|||
question.answers.pluck(:user_id).include? self.id
|
||||
end
|
||||
|
||||
# smiles an answer
|
||||
# @param answer [Answer] the answer to smile
|
||||
def smile(answer)
|
||||
# rubocop:disable Style/RedundantSelf
|
||||
raise Errors::ReactingSelfBlockedOther if self.blocking?(answer.user)
|
||||
raise Errors::ReactingOtherBlockedSelf if answer.user.blocking?(self)
|
||||
# rubocop:enable Style/RedundantSelf
|
||||
|
||||
Smile.create!(user: self, answer: answer)
|
||||
end
|
||||
|
||||
# unsmile an answer
|
||||
# @param answer [Answer] the answer to unsmile
|
||||
def unsmile(answer)
|
||||
Smile.find_by(user: self, answer: answer).destroy
|
||||
end
|
||||
|
||||
# smiles a comment
|
||||
# @param comment [Comment] the comment to smile
|
||||
def smile_comment(comment)
|
||||
|
@ -160,10 +144,6 @@ class User < ApplicationRecord
|
|||
CommentSmile.find_by(user: self, comment: comment).destroy
|
||||
end
|
||||
|
||||
def smiled?(answer)
|
||||
answer.smiles.pluck(:user_id).include? self.id
|
||||
end
|
||||
|
||||
def smiled_comment?(comment)
|
||||
comment.smiles.pluck(:user_id).include? self.id
|
||||
end
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module User::ReactionMethods
|
||||
# smiles an answer or comment
|
||||
# @param item [ApplicationRecord] the answer/comment to smile
|
||||
def smile(item)
|
||||
# rubocop:disable Style/RedundantSelf
|
||||
raise Errors::ReactingSelfBlockedOther if self.blocking?(answer.user)
|
||||
raise Errors::ReactingOtherBlockedSelf if answer.user.blocking?(self)
|
||||
# rubocop:enable Style/RedundantSelf
|
||||
|
||||
::Appendable::Reaction.create!(user: self, parent: item, content: "🙂")
|
||||
end
|
||||
|
||||
# unsmile an answer or comment
|
||||
# @param item [ApplicationRecord] the answer/comment to unsmile
|
||||
def unsmile(item)
|
||||
::Appendable::Reaction.find_by(user: self, parent: item).destroy
|
||||
end
|
||||
|
||||
def smiled?(item)
|
||||
item.smiles.pluck(:user_id).include? id
|
||||
end
|
||||
end
|
|
@ -1,17 +1,17 @@
|
|||
%span.d-none.d-sm-inline.text-muted
|
||||
- if !user_signed_in? && a.smile_count.positive?
|
||||
- if !user_signed_in? && a.smiles.count.positive?
|
||||
%button.btn.btn-info.btn-sm{ name: 'ab-smile', disabled: true }
|
||||
%i.fa.fa-smile-o
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smile_count
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smiles.count
|
||||
- if user_signed_in?
|
||||
- if current_user.smiled? a
|
||||
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-smile', data: { a_id: a.id, action: :unsmile } }
|
||||
%i.fa.fa-fw.fa-frown-o
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smile_count
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smiles.count
|
||||
- else
|
||||
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-smile', data: { a_id: a.id, action: :smile } }
|
||||
%i.fa.fa-fw.fa-smile-o
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smile_count
|
||||
%span{ id: "ab-smile-count-#{a.id}" }= a.smiles.count
|
||||
- unless display_all
|
||||
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-comments', data: { a_id: a.id, state: :hidden } }
|
||||
%i.fa.fa-fw.fa-comments
|
||||
|
|
Loading…
Reference in New Issue