Fix rubocop nits

This commit is contained in:
Andreas Nedbal 2024-01-27 13:31:36 +01:00 committed by Andreas Nedbal
parent 245f862bb2
commit 62ba205d3e
4 changed files with 39 additions and 32 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Ajax::InboxController < AjaxController class Ajax::InboxController < AjaxController
def remove def remove
params.require :id params.require :id
@ -28,7 +30,7 @@ class Ajax::InboxController < AjaxController
raise unless user_signed_in? raise unless user_signed_in?
begin begin
InboxEntry.where(user: current_user).each { |i| i.remove } InboxEntry.where(user: current_user).find_each(&:remove)
rescue => e rescue => e
Sentry.capture_exception(e) Sentry.capture_exception(e)
@response[:status] = :err @response[:status] = :err
@ -43,10 +45,10 @@ class Ajax::InboxController < AjaxController
def remove_all_author def remove_all_author
begin begin
@target_user = User.where('LOWER(screen_name) = ?', params[:author].downcase).first! @target_user = User.where("LOWER(screen_name) = ?", params[:author].downcase).first!
@inbox = current_user.inbox_entries.joins(:question) @inbox = current_user.inbox_entries.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false }) .where(questions: { user_id: @target_user.id, author_is_anonymous: false })
@inbox.each { |i| i.remove } @inbox.each(&:remove)
rescue => e rescue => e
Sentry.capture_exception(e) Sentry.capture_exception(e)
@response[:status] = :err @response[:status] = :err

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Question < ApplicationRecord class Question < ApplicationRecord
include Question::AnswerMethods include Question::AnswerMethods
@ -11,7 +13,7 @@ class Question < ApplicationRecord
SHORT_QUESTION_MAX_LENGTH = 512 SHORT_QUESTION_MAX_LENGTH = 512
before_destroy do before_destroy do
rep = Report.where(target_id: self.id, type: 'Reports::Question') rep = Report.where(target_id: self.id, type: "Reports::Question")
rep.each do |r| rep.each do |r|
unless r.nil? unless r.nil?
r.deleted = true r.deleted = true
@ -25,8 +27,9 @@ class Question < ApplicationRecord
end end
def can_be_removed? def can_be_removed?
return false if self.answers.count > 0 return false if self.answers.count.positive?
return false if InboxEntry.where(question: self).count > 1 return false if InboxEntry.where(question: self).count > 1
true true
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RenameInboxesToInboxEntries < ActiveRecord::Migration[7.0] class RenameInboxesToInboxEntries < ActiveRecord::Migration[7.0]
def change def change
rename_table :inboxes, :inbox_entries rename_table :inboxes, :inbox_entries

View File

@ -7,11 +7,11 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
describe "#remove" do describe "#remove" do
let(:params) do let(:params) do
{ {
id: inbox_entry_id id: inbox_entry_id,
} }
end end
subject { delete(:remove, params: params) } subject { delete(:remove, params:) }
context "when user is signed in" do context "when user is signed in" do
before(:each) { sign_in(user) } before(:each) { sign_in(user) }
@ -28,8 +28,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => true, "success" => true,
"status" => "okay", "status" => "okay",
"message" => anything "message" => anything,
} }
end end
@ -46,8 +46,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "fail", "status" => "fail",
"message" => anything "message" => anything,
} }
end end
@ -65,8 +65,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "not_found", "status" => "not_found",
"message" => anything "message" => anything,
} }
end end
@ -79,8 +79,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "not_found", "status" => "not_found",
"message" => anything "message" => anything,
} }
end end
@ -97,8 +97,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => true, "success" => true,
"status" => "okay", "status" => "okay",
"message" => anything "message" => anything,
} }
end end
@ -107,7 +107,7 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
context "when user has some inbox entries" do context "when user has some inbox entries" do
let(:some_other_user) { FactoryBot.create(:user) } let(:some_other_user) { FactoryBot.create(:user) }
before do before do
10.times { FactoryBot.create(:inbox_entry, user: user) } 10.times { FactoryBot.create(:inbox_entry, user:) }
10.times { FactoryBot.create(:inbox_entry, user: some_other_user) } 10.times { FactoryBot.create(:inbox_entry, user: some_other_user) }
end end
@ -123,8 +123,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "err", "status" => "err",
"message" => anything "message" => anything,
} }
end end
@ -135,11 +135,11 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
describe "#remove_all_author" do describe "#remove_all_author" do
let(:params) do let(:params) do
{ {
author: author author:,
} }
end end
subject { delete(:remove_all_author, params: params) } subject { delete(:remove_all_author, params:) }
context "when user is signed in" do context "when user is signed in" do
before(:each) { sign_in(user) } before(:each) { sign_in(user) }
@ -148,8 +148,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => true, "success" => true,
"status" => "okay", "status" => "okay",
"message" => anything "message" => anything,
} }
end end
@ -162,9 +162,9 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
normal_question = FactoryBot.create(:question, user: some_other_user, author_is_anonymous: false) normal_question = FactoryBot.create(:question, user: some_other_user, author_is_anonymous: false)
anon_question = FactoryBot.create(:question, user: some_other_user, author_is_anonymous: true) anon_question = FactoryBot.create(:question, user: some_other_user, author_is_anonymous: true)
10.times { FactoryBot.create(:inbox_entry, user: user) } 10.times { FactoryBot.create(:inbox_entry, user:) }
3.times { FactoryBot.create(:inbox_entry, user: user, question: normal_question) } 3.times { FactoryBot.create(:inbox_entry, user:, question: normal_question) }
2.times { FactoryBot.create(:inbox_entry, user: user, question: anon_question) } 2.times { FactoryBot.create(:inbox_entry, user:, question: anon_question) }
end end
it "deletes all the entries asked by some other user which are not anonymous from the user's inbox" do it "deletes all the entries asked by some other user which are not anonymous from the user's inbox" do
@ -179,8 +179,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "err", "status" => "err",
"message" => anything "message" => anything,
} }
end end
@ -193,8 +193,8 @@ describe Ajax::InboxController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "err", "status" => "err",
"message" => anything "message" => anything,
} }
end end