From efffdd3778fc960280c7677906ed43dae5a952b0 Mon Sep 17 00:00:00 2001 From: abcang Date: Thu, 7 Jan 2021 17:40:55 +0900 Subject: [PATCH] Fix rubocop config and warnings (#15503) * disable NewCops * update TargetRubyVersion * Fix Lint/MissingSuper for ActiveModelSerializers::Model * Fix Lint/MissingSuper for feed * Fix Lint/FloatComparison * Do not use instance variables --- .rubocop.yml | 3 ++- app/models/account.rb | 32 +++++++++++------------------- app/models/home_feed.rb | 5 ++--- app/models/list_feed.rb | 3 +-- app/models/poll.rb | 10 ++++++---- app/models/public_feed.rb | 22 ++++++++++---------- app/models/tag_feed.rb | 11 +++++----- app/services/keys/claim_service.rb | 12 ++++++----- app/services/keys/query_service.rb | 18 ++++++++++------- lib/paperclip/color_extractor.rb | 2 +- 10 files changed, 59 insertions(+), 59 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 14728bf0e9..2af0f59bbe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,7 +2,8 @@ require: - rubocop-rails AllCops: - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.5 + NewCops: disable Exclude: - 'spec/**/*' - 'db/**/*' diff --git a/app/models/account.rb b/app/models/account.rb index e6cf03fa84..1f723e2064 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -385,15 +385,17 @@ class Account < ApplicationRecord end class Field < ActiveModelSerializers::Model - attributes :name, :value, :verified_at, :account, :errors + attributes :name, :value, :verified_at, :account def initialize(account, attributes) - @account = account - @attributes = attributes - @name = attributes['name'].strip[0, string_limit] - @value = attributes['value'].strip[0, string_limit] - @verified_at = attributes['verified_at']&.to_datetime - @errors = {} + @original_field = attributes + string_limit = account.local? ? 255 : 2047 + super( + account: account, + name: attributes['name'].strip[0, string_limit], + value: attributes['value'].strip[0, string_limit], + verified_at: attributes['verified_at']&.to_datetime, + ) end def verified? @@ -415,22 +417,12 @@ class Account < ApplicationRecord end def mark_verified! - @verified_at = Time.now.utc - @attributes['verified_at'] = @verified_at + self.verified_at = Time.now.utc + @original_field['verified_at'] = verified_at end def to_h - { name: @name, value: @value, verified_at: @verified_at } - end - - private - - def string_limit - if account.local? - 255 - else - 2047 - end + { name: name, value: value, verified_at: verified_at } end end diff --git a/app/models/home_feed.rb b/app/models/home_feed.rb index 0fe9dae464..d6ebb5fa6b 100644 --- a/app/models/home_feed.rb +++ b/app/models/home_feed.rb @@ -2,12 +2,11 @@ class HomeFeed < Feed def initialize(account) - @type = :home - @id = account.id @account = account + super(:home, account.id) end def regenerating? - redis.exists?("account:#{@id}:regeneration") + redis.exists?("account:#{@account.id}:regeneration") end end diff --git a/app/models/list_feed.rb b/app/models/list_feed.rb index f371e4ed90..47b9281b8c 100644 --- a/app/models/list_feed.rb +++ b/app/models/list_feed.rb @@ -2,7 +2,6 @@ class ListFeed < Feed def initialize(list) - @type = :list - @id = list.id + super(:list, list.id) end end diff --git a/app/models/poll.rb b/app/models/poll.rb index e1ca552521..d2a17277b2 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -73,10 +73,12 @@ class Poll < ApplicationRecord attributes :id, :title, :votes_count, :poll def initialize(poll, id, title, votes_count) - @poll = poll - @id = id - @title = title - @votes_count = votes_count + super( + poll: poll, + id: id, + title: title, + votes_count: votes_count, + ) end end diff --git a/app/models/public_feed.rb b/app/models/public_feed.rb index c8ce1a1403..5e4c3e1cee 100644 --- a/app/models/public_feed.rb +++ b/app/models/public_feed.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class PublicFeed < Feed +class PublicFeed # @param [Account] account # @param [Hash] options # @option [Boolean] :with_replies @@ -33,28 +33,30 @@ class PublicFeed < Feed private + attr_reader :account, :options + def with_reblogs? - @options[:with_reblogs] + options[:with_reblogs] end def with_replies? - @options[:with_replies] + options[:with_replies] end def local_only? - @options[:local] + options[:local] end def remote_only? - @options[:remote] + options[:remote] end def account? - @account.present? + account.present? end def media_only? - @options[:only_media] + options[:only_media] end def public_scope @@ -82,9 +84,9 @@ class PublicFeed < Feed end def account_filters_scope - Status.not_excluded_by_account(@account).tap do |scope| - scope.merge!(Status.not_domain_blocked_by_account(@account)) unless local_only? - scope.merge!(Status.in_chosen_languages(@account)) if @account.chosen_languages.present? + Status.not_excluded_by_account(account).tap do |scope| + scope.merge!(Status.not_domain_blocked_by_account(account)) unless local_only? + scope.merge!(Status.in_chosen_languages(account)) if account.chosen_languages.present? end end end diff --git a/app/models/tag_feed.rb b/app/models/tag_feed.rb index 9a16ffc82b..b8cd63557e 100644 --- a/app/models/tag_feed.rb +++ b/app/models/tag_feed.rb @@ -13,9 +13,8 @@ class TagFeed < PublicFeed # @option [Boolean] :remote # @option [Boolean] :only_media def initialize(tag, account, options = {}) - @tag = tag - @account = account - @options = options + @tag = tag + super(account, options) end # @param [Integer] limit @@ -40,15 +39,15 @@ class TagFeed < PublicFeed private def tagged_with_any_scope - Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(@options[:any]))) + Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(options[:any]))) end def tagged_with_all_scope - Status.group(:id).tagged_with_all(tags_for(@options[:all])) + Status.group(:id).tagged_with_all(tags_for(options[:all])) end def tagged_with_none_scope - Status.group(:id).tagged_with_none(tags_for(@options[:none])) + Status.group(:id).tagged_with_none(tags_for(options[:none])) end def tags_for(names) diff --git a/app/services/keys/claim_service.rb b/app/services/keys/claim_service.rb index 6721191308..69568a0d19 100644 --- a/app/services/keys/claim_service.rb +++ b/app/services/keys/claim_service.rb @@ -8,11 +8,13 @@ class Keys::ClaimService < BaseService :key, :signature def initialize(account, device_id, key_attributes = {}) - @account = account - @device_id = device_id - @key_id = key_attributes[:key_id] - @key = key_attributes[:key] - @signature = key_attributes[:signature] + super( + account: account, + device_id: device_id, + key_id: key_attributes[:key_id], + key: key_attributes[:key], + signature: key_attributes[:signature], + ) end end diff --git a/app/services/keys/query_service.rb b/app/services/keys/query_service.rb index 286fbd8347..ac3388bdc0 100644 --- a/app/services/keys/query_service.rb +++ b/app/services/keys/query_service.rb @@ -7,8 +7,10 @@ class Keys::QueryService < BaseService attributes :account, :devices def initialize(account, devices) - @account = account - @devices = devices || [] + super( + account: account, + devices: devices || [], + ) end def find(device_id) @@ -20,11 +22,13 @@ class Keys::QueryService < BaseService attributes :device_id, :name, :identity_key, :fingerprint_key def initialize(attributes = {}) - @device_id = attributes[:device_id] - @name = attributes[:name] - @identity_key = attributes[:identity_key] - @fingerprint_key = attributes[:fingerprint_key] - @claim_url = attributes[:claim_url] + super( + device_id: attributes[:device_id], + name: attributes[:name], + identity_key: attributes[:identity_key], + fingerprint_key: attributes[:fingerprint_key], + ) + @claim_url = attributes[:claim_url] end def valid_claim_url? diff --git a/lib/paperclip/color_extractor.rb b/lib/paperclip/color_extractor.rb index f850dc067c..a70a3d21f2 100644 --- a/lib/paperclip/color_extractor.rb +++ b/lib/paperclip/color_extractor.rb @@ -142,7 +142,7 @@ module Paperclip g = 0.0 b = 0.0 - if s == 0.0 + if s.zero? r = l.to_f g = l.to_f b = l.to_f # achromatic