Refactor smiling answers to using reactions

This commit is contained in:
Karina Kwiatek 2022-02-19 17:19:28 +01:00 committed by Karina Kwiatek
parent 1e53f00032
commit 5f0a546353
5 changed files with 33 additions and 27 deletions

View File

@ -4,7 +4,7 @@ class Answer < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :question belongs_to :question
has_many :comments, dependent: :destroy 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 :subscriptions, dependent: :destroy
has_many :comment_smiles, through: :comments, source: :smiles has_many :comment_smiles, through: :comments, source: :smiles

View File

@ -1,2 +1,4 @@
# frozen_string_literal: true
class Appendable::Reaction < Appendable class Appendable::Reaction < Appendable
end end

View File

@ -8,6 +8,7 @@ class User < ApplicationRecord
include User::BanMethods include User::BanMethods
include User::InboxMethods include User::InboxMethods
include User::QuestionMethods include User::QuestionMethods
include User::ReactionMethods
include User::RelationshipMethods include User::RelationshipMethods
include User::TimelineMethods include User::TimelineMethods
include ActiveModel::OneTimePassword include ActiveModel::OneTimePassword
@ -30,7 +31,7 @@ class User < ApplicationRecord
has_many :answers, dependent: :destroy has_many :answers, dependent: :destroy
has_many :comments, dependent: :destroy has_many :comments, dependent: :destroy
has_many :inboxes, 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 :comment_smiles, dependent: :destroy
has_many :services, dependent: :destroy has_many :services, dependent: :destroy
has_many :notifications, foreign_key: :recipient_id, 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 question.answers.pluck(:user_id).include? self.id
end 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 # smiles a comment
# @param comment [Comment] the comment to smile # @param comment [Comment] the comment to smile
def smile_comment(comment) def smile_comment(comment)
@ -160,10 +144,6 @@ class User < ApplicationRecord
CommentSmile.find_by(user: self, comment: comment).destroy CommentSmile.find_by(user: self, comment: comment).destroy
end end
def smiled?(answer)
answer.smiles.pluck(:user_id).include? self.id
end
def smiled_comment?(comment) def smiled_comment?(comment)
comment.smiles.pluck(:user_id).include? self.id comment.smiles.pluck(:user_id).include? self.id
end end

View File

@ -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

View File

@ -1,17 +1,17 @@
%span.d-none.d-sm-inline.text-muted %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 } %button.btn.btn-info.btn-sm{ name: 'ab-smile', disabled: true }
%i.fa.fa-smile-o %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 user_signed_in?
- if current_user.smiled? a - if current_user.smiled? a
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-smile', data: { a_id: a.id, action: :unsmile } } %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 %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 - else
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-smile', data: { a_id: a.id, action: :smile } } %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 %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 - unless display_all
%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-comments', data: { a_id: a.id, state: :hidden } } %button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-comments', data: { a_id: a.id, state: :hidden } }
%i.fa.fa-fw.fa-comments %i.fa.fa-fw.fa-comments