Fix lint errors

This commit is contained in:
Karina Kwiatek 2022-01-16 18:51:27 +01:00 committed by Karina Kwiatek
parent 0ab822c33a
commit b4e0699e82
15 changed files with 48 additions and 37 deletions

View File

@ -12,11 +12,11 @@ class Ajax::RelationshipController < AjaxController
UseCase::Relationship::Create.call( UseCase::Relationship::Create.call(
current_user: current_user.screen_name, current_user: current_user.screen_name,
target_user: params[:target_user], target_user: params[:target_user],
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true
@response[:message] = t('messages.friend.create.success') @response[:message] = t("messages.friend.create.success")
rescue Errors::Base => e rescue Errors::Base => e
@response[:message] = t(e.locale_tag) @response[:message] = t(e.locale_tag)
ensure ensure
@ -26,11 +26,11 @@ class Ajax::RelationshipController < AjaxController
def destroy def destroy
UseCase::Relationship::Destroy.call( UseCase::Relationship::Destroy.call(
current_user: current_user.screen_name, current_user: current_user.screen_name,
target_user: params[:target_user], target_user: params[:target_user],
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true
@response[:message] = t('messages.friend.create.success') @response[:message] = t("messages.friend.create.success")
rescue Errors::Base => e rescue Errors::Base => e
@response[:message] = t(e.locale_tag) @response[:message] = t(e.locale_tag)
ensure ensure

View File

@ -12,7 +12,7 @@ class AjaxController < ApplicationController
@response = { @response = {
success: false, success: false,
message: "Something went wrong", message: "Something went wrong",
status: :err status: :err
} }
return_response return_response
@ -24,7 +24,7 @@ class AjaxController < ApplicationController
@response = { @response = {
success: false, success: false,
message: "Missing parameter: #{e.key}", message: "Missing parameter: #{e.key}",
status: :err status: :err
} }
return_response return_response
@ -36,7 +36,7 @@ class AjaxController < ApplicationController
@response = { @response = {
success: false, success: false,
message: "Invalid parameter", message: "Invalid parameter",
status: :err status: :err
} }
return_response return_response
@ -48,7 +48,7 @@ class AjaxController < ApplicationController
@response = { @response = {
success: false, success: false,
message: "Record not found", message: "Record not found",
status: :not_found status: :not_found
} }
return_response return_response
@ -59,8 +59,8 @@ class AjaxController < ApplicationController
@response = { @response = {
success: false, success: false,
message: I18n.t('messages.parameter_error', parameter: e.param.capitalize), message: I18n.t("messages.parameter_error", parameter: e.param.capitalize),
status: :parameter_error status: :parameter_error
} }
return_response return_response
@ -75,8 +75,8 @@ class AjaxController < ApplicationController
def build_response def build_response
@response = { @response = {
success: false, success: false,
message: '', message: "",
status: 'unknown' status: "unknown"
} }
end end

View File

@ -96,6 +96,7 @@ class UserController < ApplicationController
end end
end end
# rubocop:disable Metrics/AbcSize
def followings def followings
@title = 'Following' @title = 'Following'
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first! @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
@ -109,6 +110,7 @@ class UserController < ApplicationController
format.js { render "show_follow", layout: false } format.js { render "show_follow", layout: false }
end end
end end
# rubocop:enable Metrics/AbcSize
def questions def questions
@title = 'Questions' @title = 'Questions'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Relationships::Follow < Relationship class Relationships::Follow < Relationship
after_create do after_create do
Notification.notify target, self Notification.notify target, self

View File

@ -8,12 +8,12 @@ class User
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
has_many :active_follow_relationships, class_name: "Relationships::Follow", has_many :active_follow_relationships, class_name: "Relationships::Follow",
foreign_key: "source_id", foreign_key: "source_id",
dependent: :destroy dependent: :destroy
has_many :passive_follow_relationships, class_name: "Relationships::Follow", has_many :passive_follow_relationships, class_name: "Relationships::Follow",
foreign_key: "target_id", foreign_key: "target_id",
dependent: :destroy dependent: :destroy
has_many :followings, through: :active_follow_relationships, source: :target has_many :followings, through: :active_follow_relationships, source: :target
has_many :followers, through: :passive_follow_relationships, source: :source has_many :followers, through: :passive_follow_relationships, source: :source
end end
@ -21,6 +21,7 @@ class User
# Follow an user # Follow an user
def follow(target_user) def follow(target_user)
raise Errors::FollowingSelf if target_user == self raise Errors::FollowingSelf if target_user == self
create_relationship(active_follow_relationships, target_user) create_relationship(active_follow_relationships, target_user)
end end

View File

@ -7,6 +7,6 @@ module User::TimelineMethods
# @return [Array] the users' timeline # @return [Array] the users' timeline
def timeline def timeline
Answer.where('user_id in (?) OR user_id = ?', following_ids, id).order(:created_at).reverse_order.includes(comments: [:user, :smiles], question: [:user], user: [:profile], smiles: [:user]) Answer.where("user_id in (?) OR user_id = ?", following_ids, id).order(:created_at).reverse_order.includes(comments: %i[user smiles], question: [:user], user: [:profile], smiles: [:user])
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AddTypeToRelationships < ActiveRecord::Migration[5.2] class AddTypeToRelationships < ActiveRecord::Migration[5.2]
def up def up
add_column :relationships, :type, :string add_column :relationships, :type, :string

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
class RemoveRelationshipCountersFromUsers < ActiveRecord::Migration[5.2] class RemoveRelationshipCountersFromUsers < ActiveRecord::Migration[5.2]
def change def change
remove_column :users, :follower_count remove_column :users, :follower_count, :integer
remove_column :users, :friend_count remove_column :users, :friend_count, :integer
end end
end end

View File

@ -232,8 +232,8 @@ class Exporter
def user_stub(user) def user_stub(user)
uobj = {} uobj = {}
%i(answered_count asked_count comment_smiled_count commented_count created_at %i[answered_count asked_count comment_smiled_count commented_count created_at
id permanently_banned screen_name smiled_count).each do |f| id permanently_banned screen_name smiled_count].each do |f|
uobj[f] = user.send f uobj[f] = user.send f
end end

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'dry-types' require "dry-types"
module Types module Types
include Dry.Types() include Dry.Types()
RelationshipTypes = Types::String.enum('follow') RelationshipTypes = Types::String.enum("follow")
end end

View File

@ -17,9 +17,9 @@ module UseCase
source_user.public_send(type, target_user) source_user.public_send(type, target_user)
{ {
status: 201, status: 201,
resource: true, resource: true,
extra: { extra: {
target_user: target_user target_user: target_user
} }
} }
@ -29,6 +29,7 @@ module UseCase
def find_source_user def find_source_user
return current_user if current_user.is_a?(::User) return current_user if current_user.is_a?(::User)
::User.find_by!(screen_name: current_user) ::User.find_by!(screen_name: current_user)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound raise Errors::UserNotFound
@ -36,6 +37,7 @@ module UseCase
def find_target_user def find_target_user
return target_user if target_user.is_a?(::User) return target_user if target_user.is_a?(::User)
::User.find_by!(screen_name: target_user) ::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound raise Errors::UserNotFound

View File

@ -17,9 +17,9 @@ module UseCase
source_user.public_send("un#{type}", target_user) source_user.public_send("un#{type}", target_user)
{ {
status: 204, status: 204,
resource: nil, resource: nil,
extra: { extra: {
target_user: target_user target_user: target_user
} }
} }

View File

@ -5,7 +5,7 @@ require "rails_helper"
describe Ajax::RelationshipController, type: :controller do describe Ajax::RelationshipController, type: :controller do
shared_examples_for "params is empty" do shared_examples_for "params is empty" do
let(:params) { } let(:params) { {} }
include_examples "ajax does not succeed", "is required" include_examples "ajax does not succeed", "is required"
end end

View File

@ -40,9 +40,9 @@ describe UseCase::Relationship::Create do
let(:base_params) do let(:base_params) do
{ {
current_user: current_user, current_user: current_user,
target_user: target_user, target_user: target_user,
type: type type: type
} }
end end
let(:params) { base_params } let(:params) { base_params }

View File

@ -28,14 +28,14 @@ describe UseCase::Relationship::Destroy do
context "current_user does not exist" do context "current_user does not exist" do
let(:current_user) { "Schweinsbraten" } let(:current_user) { "Schweinsbraten" }
let(:target_user) { user2.screen_name } let(:target_user) { user2.screen_name }
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
end end
context "target_user does not exist" do context "target_user does not exist" do
let(:current_user) { user1.screen_name } let(:current_user) { user1.screen_name }
let(:target_user) { "peterwitzig" } let(:target_user) { "peterwitzig" }
include_examples "raises an error", Errors::UserNotFound include_examples "raises an error", Errors::UserNotFound
end end
@ -44,8 +44,8 @@ describe UseCase::Relationship::Destroy do
let(:base_params) do let(:base_params) do
{ {
current_user: current_user, current_user: current_user,
target_user: target_user, target_user: target_user,
type: type type: type
} }
end end
let(:params) { base_params } let(:params) { base_params }