Appease the dog overlords

This commit is contained in:
Andreas Nedbal 2022-07-09 19:16:18 +02:00 committed by Karina Kwiatek
parent 0555279cd2
commit 47120e8d24
3 changed files with 28 additions and 26 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Ajax::ListController < AjaxController class Ajax::ListController < AjaxController
before_action :authenticate_user! before_action :authenticate_user!
@ -6,13 +8,13 @@ class Ajax::ListController < AjaxController
@response[:status] = :err @response[:status] = :err
target_user = User.find_by_screen_name!(params[:user]) target_user = User.find_by!(screen_name: params[:user])
list = List.create! user: current_user, display_name: params[:name] list = List.create! user: current_user, display_name: params[:name]
@response[:status] = :okay @response[:status] = :okay
@response[:success] = true @response[:success] = true
@response[:message] = t(".success") @response[:message] = t(".success")
@response[:render] = render_to_string(partial: 'modal/list/item', locals: { list: list, user: target_user }) @response[:render] = render_to_string(partial: "modal/list/item", locals: { list: list, user: target_user })
end end
def destroy def destroy
@ -34,20 +36,20 @@ class Ajax::ListController < AjaxController
@response[:status] = :err @response[:status] = :err
add = params[:add] == 'true' add = params[:add] == "true"
target_user = User.find_by_screen_name!(params[:user]) target_user = User.find_by!(screen_name: params[:user])
list = current_user.lists.find_by_name!(params[:list]) list = current_user.lists.find_by!(name: params[:list])
raise Errors::ListingSelfBlockedOther if current_user.blocking?(target_user) raise Errors::ListingSelfBlockedOther if current_user.blocking?(target_user)
raise Errors::ListingOtherBlockedSelf if target_user.blocking?(current_user) raise Errors::ListingOtherBlockedSelf if target_user.blocking?(current_user)
if add if add
list.add_member target_user if list.members.find_by_user_id(target_user.id).nil? list.add_member target_user if list.members.find_by(user_id: target_user.id).nil?
@response[:checked] = true @response[:checked] = true
@response[:message] = t(".success.add") @response[:message] = t(".success.add")
else else
list.remove_member target_user unless list.members.find_by_user_id(target_user.id).nil? list.remove_member target_user unless list.members.find_by(user_id: target_user.id).nil?
@response[:checked] = false @response[:checked] = false
@response[:message] = t(".success.remove") @response[:message] = t(".success.remove")
end end

View File

@ -7,14 +7,14 @@ class List < ApplicationRecord
has_many :list_members, dependent: :destroy has_many :list_members, dependent: :destroy
validates :name, validates :name,
length: { minimum: 1 }, length: { minimum: 1 },
uniqueness: { scope: :user_id } uniqueness: { scope: :user_id }
validates :display_name, length: { maximum: 30 } validates :display_name, length: { maximum: 30 }
before_validation do before_validation do
self.display_name.strip! display_name.strip!
self.name = self.display_name.parameterize self.name = display_name.parameterize
self.name = '-followers-' if self.name == 'followers' self.name = "-followers-" if name == "followers"
end end
alias members list_members alias members list_members

View File

@ -21,9 +21,9 @@ describe Ajax::ListController, :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,
"render" => anything "render" => anything
} }
end end
@ -40,7 +40,7 @@ describe Ajax::ListController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "parameter_error", "status" => "parameter_error",
"message" => anything "message" => anything
} }
end end
@ -57,7 +57,7 @@ describe Ajax::ListController, :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
@ -74,7 +74,7 @@ describe Ajax::ListController, :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
@ -91,7 +91,7 @@ describe Ajax::ListController, :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
@ -137,7 +137,7 @@ describe Ajax::ListController, :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
@ -156,7 +156,7 @@ describe Ajax::ListController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
{ {
"success" => false, "success" => false,
"status" => "parameter_error", "status" => "parameter_error",
"message" => anything "message" => anything
} }
end end
@ -173,7 +173,7 @@ describe Ajax::ListController, :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
@ -218,7 +218,7 @@ describe Ajax::ListController, :ajax_controller, type: :controller do
{ {
"list" => list_param, "list" => list_param,
"user" => target_user_param, "user" => target_user_param,
"add" => add_param "add" => add_param
} }
end end
@ -228,7 +228,7 @@ describe Ajax::ListController, :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,
"checked" => expected_checked "checked" => expected_checked
} }
@ -242,7 +242,7 @@ describe Ajax::ListController, :ajax_controller, type: :controller do
it "does not do anything" do it "does not do anything" do
expect { subject }.not_to(change { list.members }) expect { subject }.not_to(change { list.members })
expect(list.members.map { |gm| gm.user.id }.sort ).to eq([]) expect(list.members.map { |gm| gm.user.id }.sort).to eq([])
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -273,7 +273,7 @@ describe Ajax::ListController, :ajax_controller, type: :controller do
it "does not add the user to the list again" do it "does not add the user to the list again" do
expect { subject }.not_to(change { list.members }) expect { subject }.not_to(change { list.members })
expect(list.members.map { |gm| gm.user.id }.sort ).to eq([target_user.id]) expect(list.members.map { |gm| gm.user.id }.sort).to eq([target_user.id])
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -286,7 +286,7 @@ describe Ajax::ListController, :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
@ -300,7 +300,7 @@ describe Ajax::ListController, :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