From 5f0a546353f7957b48cc841609a2716cfa36d2cd Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 19 Feb 2022 17:19:28 +0100 Subject: [PATCH] Refactor smiling answers to using reactions --- app/models/answer.rb | 2 +- app/models/appendable/reaction.rb | 2 ++ app/models/user.rb | 24 ++---------------------- app/models/user/reaction_methods.rb | 24 ++++++++++++++++++++++++ app/views/answerbox/_actions.haml | 8 ++++---- 5 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 app/models/user/reaction_methods.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index 5be72352..414d1d80 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -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 diff --git a/app/models/appendable/reaction.rb b/app/models/appendable/reaction.rb index 51283ff4..3d1a6e28 100644 --- a/app/models/appendable/reaction.rb +++ b/app/models/appendable/reaction.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + class Appendable::Reaction < Appendable end diff --git a/app/models/user.rb b/app/models/user.rb index f5deb218..8f40e3f2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user/reaction_methods.rb b/app/models/user/reaction_methods.rb new file mode 100644 index 00000000..895853eb --- /dev/null +++ b/app/models/user/reaction_methods.rb @@ -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 diff --git a/app/views/answerbox/_actions.haml b/app/views/answerbox/_actions.haml index ed9b053f..f8490da1 100644 --- a/app/views/answerbox/_actions.haml +++ b/app/views/answerbox/_actions.haml @@ -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