diff --git a/app/controllers/ajax/relationship_controller.rb b/app/controllers/ajax/relationship_controller.rb index d0bdea37..8c20747b 100644 --- a/app/controllers/ajax/relationship_controller.rb +++ b/app/controllers/ajax/relationship_controller.rb @@ -12,11 +12,11 @@ class Ajax::RelationshipController < AjaxController UseCase::Relationship::Create.call( current_user: current_user.screen_name, - target_user: params[:target_user], - type: params[:type] + target_user: params[:target_user], + type: params[:type] ) @response[:success] = true - @response[:message] = t('messages.friend.create.success') + @response[:message] = t("messages.friend.create.success") rescue Errors::Base => e @response[:message] = t(e.locale_tag) ensure @@ -26,11 +26,11 @@ class Ajax::RelationshipController < AjaxController def destroy UseCase::Relationship::Destroy.call( current_user: current_user.screen_name, - target_user: params[:target_user], - type: params[:type] + target_user: params[:target_user], + type: params[:type] ) @response[:success] = true - @response[:message] = t('messages.friend.create.success') + @response[:message] = t("messages.friend.create.success") rescue Errors::Base => e @response[:message] = t(e.locale_tag) ensure diff --git a/app/controllers/ajax_controller.rb b/app/controllers/ajax_controller.rb index 5a8fc00f..d5bf4b33 100644 --- a/app/controllers/ajax_controller.rb +++ b/app/controllers/ajax_controller.rb @@ -12,7 +12,7 @@ class AjaxController < ApplicationController @response = { success: false, message: "Something went wrong", - status: :err + status: :err } return_response @@ -24,7 +24,7 @@ class AjaxController < ApplicationController @response = { success: false, message: "Missing parameter: #{e.key}", - status: :err + status: :err } return_response @@ -36,7 +36,7 @@ class AjaxController < ApplicationController @response = { success: false, message: "Invalid parameter", - status: :err + status: :err } return_response @@ -48,7 +48,7 @@ class AjaxController < ApplicationController @response = { success: false, message: "Record not found", - status: :not_found + status: :not_found } return_response @@ -59,8 +59,8 @@ class AjaxController < ApplicationController @response = { success: false, - message: I18n.t('messages.parameter_error', parameter: e.param.capitalize), - status: :parameter_error + message: I18n.t("messages.parameter_error", parameter: e.param.capitalize), + status: :parameter_error } return_response @@ -75,8 +75,8 @@ class AjaxController < ApplicationController def build_response @response = { success: false, - message: '', - status: 'unknown' + message: "", + status: "unknown" } end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index c11f0757..802a3905 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -96,6 +96,7 @@ class UserController < ApplicationController end end + # rubocop:disable Metrics/AbcSize def followings @title = 'Following' @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 } end end + # rubocop:enable Metrics/AbcSize def questions @title = 'Questions' diff --git a/app/models/relationships/follow.rb b/app/models/relationships/follow.rb index 11aafc35..41d8fd0d 100644 --- a/app/models/relationships/follow.rb +++ b/app/models/relationships/follow.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Relationships::Follow < Relationship after_create do Notification.notify target, self diff --git a/app/models/user/relationship/follow.rb b/app/models/user/relationship/follow.rb index a85b969d..c45d93ca 100644 --- a/app/models/user/relationship/follow.rb +++ b/app/models/user/relationship/follow.rb @@ -8,12 +8,12 @@ class User extend ActiveSupport::Concern included do - has_many :active_follow_relationships, class_name: "Relationships::Follow", + has_many :active_follow_relationships, class_name: "Relationships::Follow", foreign_key: "source_id", - dependent: :destroy - has_many :passive_follow_relationships, class_name: "Relationships::Follow", + dependent: :destroy + has_many :passive_follow_relationships, class_name: "Relationships::Follow", foreign_key: "target_id", - dependent: :destroy + dependent: :destroy has_many :followings, through: :active_follow_relationships, source: :target has_many :followers, through: :passive_follow_relationships, source: :source end @@ -21,6 +21,7 @@ class User # Follow an user def follow(target_user) raise Errors::FollowingSelf if target_user == self + create_relationship(active_follow_relationships, target_user) end diff --git a/app/models/user/timeline_methods.rb b/app/models/user/timeline_methods.rb index 57c79115..38f1e560 100644 --- a/app/models/user/timeline_methods.rb +++ b/app/models/user/timeline_methods.rb @@ -7,6 +7,6 @@ module User::TimelineMethods # @return [Array] the users' 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 diff --git a/db/migrate/20211231155702_add_type_to_relationships.rb b/db/migrate/20211231155702_add_type_to_relationships.rb index d79b50c4..53812c20 100644 --- a/db/migrate/20211231155702_add_type_to_relationships.rb +++ b/db/migrate/20211231155702_add_type_to_relationships.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddTypeToRelationships < ActiveRecord::Migration[5.2] def up add_column :relationships, :type, :string diff --git a/db/migrate/20211231212335_remove_relationship_counters_from_users.rb b/db/migrate/20211231212335_remove_relationship_counters_from_users.rb index 40cc4033..12f18363 100644 --- a/db/migrate/20211231212335_remove_relationship_counters_from_users.rb +++ b/db/migrate/20211231212335_remove_relationship_counters_from_users.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class RemoveRelationshipCountersFromUsers < ActiveRecord::Migration[5.2] def change - remove_column :users, :follower_count - remove_column :users, :friend_count + remove_column :users, :follower_count, :integer + remove_column :users, :friend_count, :integer end end diff --git a/lib/exporter.rb b/lib/exporter.rb index f4566f86..be446f7f 100644 --- a/lib/exporter.rb +++ b/lib/exporter.rb @@ -232,8 +232,8 @@ class Exporter def user_stub(user) uobj = {} - %i(answered_count asked_count comment_smiled_count commented_count created_at - id permanently_banned screen_name smiled_count).each do |f| + %i[answered_count asked_count comment_smiled_count commented_count created_at + id permanently_banned screen_name smiled_count].each do |f| uobj[f] = user.send f end diff --git a/lib/types.rb b/lib/types.rb index 0995d88f..267760a2 100644 --- a/lib/types.rb +++ b/lib/types.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require 'dry-types' +require "dry-types" module Types include Dry.Types() - RelationshipTypes = Types::String.enum('follow') + RelationshipTypes = Types::String.enum("follow") end diff --git a/lib/use_case/relationship/create.rb b/lib/use_case/relationship/create.rb index 5efab61d..adb0db0a 100644 --- a/lib/use_case/relationship/create.rb +++ b/lib/use_case/relationship/create.rb @@ -17,9 +17,9 @@ module UseCase source_user.public_send(type, target_user) { - status: 201, + status: 201, resource: true, - extra: { + extra: { target_user: target_user } } @@ -29,6 +29,7 @@ module UseCase def find_source_user return current_user if current_user.is_a?(::User) + ::User.find_by!(screen_name: current_user) rescue ActiveRecord::RecordNotFound raise Errors::UserNotFound @@ -36,6 +37,7 @@ module UseCase def find_target_user return target_user if target_user.is_a?(::User) + ::User.find_by!(screen_name: target_user) rescue ActiveRecord::RecordNotFound raise Errors::UserNotFound diff --git a/lib/use_case/relationship/destroy.rb b/lib/use_case/relationship/destroy.rb index d72869d6..b73fe6d0 100644 --- a/lib/use_case/relationship/destroy.rb +++ b/lib/use_case/relationship/destroy.rb @@ -17,9 +17,9 @@ module UseCase source_user.public_send("un#{type}", target_user) { - status: 204, + status: 204, resource: nil, - extra: { + extra: { target_user: target_user } } diff --git a/spec/controllers/ajax/relationship_controller_spec.rb b/spec/controllers/ajax/relationship_controller_spec.rb index 34a941e1..7b212247 100644 --- a/spec/controllers/ajax/relationship_controller_spec.rb +++ b/spec/controllers/ajax/relationship_controller_spec.rb @@ -5,7 +5,7 @@ require "rails_helper" describe Ajax::RelationshipController, type: :controller do shared_examples_for "params is empty" do - let(:params) { } + let(:params) { {} } include_examples "ajax does not succeed", "is required" end diff --git a/spec/lib/use_case/relationship/create_spec.rb b/spec/lib/use_case/relationship/create_spec.rb index 723b1d2e..5fa4be12 100644 --- a/spec/lib/use_case/relationship/create_spec.rb +++ b/spec/lib/use_case/relationship/create_spec.rb @@ -40,9 +40,9 @@ describe UseCase::Relationship::Create do let(:base_params) do { - current_user: current_user, - target_user: target_user, - type: type + current_user: current_user, + target_user: target_user, + type: type } end let(:params) { base_params } diff --git a/spec/lib/use_case/relationship/destroy_spec.rb b/spec/lib/use_case/relationship/destroy_spec.rb index 3a7cf62c..c7c32146 100644 --- a/spec/lib/use_case/relationship/destroy_spec.rb +++ b/spec/lib/use_case/relationship/destroy_spec.rb @@ -28,14 +28,14 @@ describe UseCase::Relationship::Destroy do context "current_user does not exist" do let(:current_user) { "Schweinsbraten" } - let(:target_user) { user2.screen_name } + let(:target_user) { user2.screen_name } include_examples "raises an error", Errors::UserNotFound end context "target_user does not exist" do let(:current_user) { user1.screen_name } - let(:target_user) { "peterwitzig" } + let(:target_user) { "peterwitzig" } include_examples "raises an error", Errors::UserNotFound end @@ -44,8 +44,8 @@ describe UseCase::Relationship::Destroy do let(:base_params) do { current_user: current_user, - target_user: target_user, - type: type + target_user: target_user, + type: type } end let(:params) { base_params }