From fdc17bea58f210f62ac0d9e836b68e84c6dbd15c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 15 Nov 2016 16:56:29 +0100 Subject: Fix rubocop issues, introduce usage of frozen literal to improve performance --- app/models/account.rb | 10 ++++++---- app/models/application_record.rb | 2 ++ app/models/block.rb | 2 ++ app/models/concerns/paginable.rb | 2 ++ app/models/concerns/streamable.rb | 2 ++ app/models/concerns/targetable.rb | 2 ++ app/models/domain_block.rb | 2 ++ app/models/favourite.rb | 2 ++ app/models/feed.rb | 4 +++- app/models/follow.rb | 2 ++ app/models/follow_suggestion.rb | 2 ++ app/models/media_attachment.rb | 22 ++++++++++++---------- app/models/mention.rb | 2 ++ app/models/status.rb | 15 ++++++--------- app/models/stream_entry.rb | 8 +++++++- app/models/tag.rb | 2 ++ app/models/user.rb | 2 ++ 17 files changed, 58 insertions(+), 25 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 81b724935..90434f975 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Account < ApplicationRecord include Targetable include PgSearch @@ -92,11 +94,11 @@ class Account < ApplicationRecord end def favourited?(status) - (status.reblog? ? status.reblog : status).favourites.where(account: self).count > 0 + (status.reblog? ? status.reblog : status).favourites.where(account: self).count.positive? end def reblogged?(status) - (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0 + (status.reblog? ? status.reblog : status).reblogs.where(account: self).count.positive? end def keypair @@ -115,8 +117,8 @@ class Account < ApplicationRecord def avatar_remote_url=(url) self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url self[:avatar_remote_url] = url - rescue OpenURI::HTTPError - # + rescue OpenURI::HTTPError => e + Rails.logger.debug "Error fetching remote avatar: #{e}" end def object_type diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba84..71fbba5b3 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end diff --git a/app/models/block.rb b/app/models/block.rb index 418afdbdf..dc05bce87 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Block < ApplicationRecord belongs_to :account belongs_to :target_account, class_name: 'Account' diff --git a/app/models/concerns/paginable.rb b/app/models/concerns/paginable.rb index 08f207621..b3df081c0 100644 --- a/app/models/concerns/paginable.rb +++ b/app/models/concerns/paginable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Paginable extend ActiveSupport::Concern diff --git a/app/models/concerns/streamable.rb b/app/models/concerns/streamable.rb index b3354c819..d9f5dc4d8 100644 --- a/app/models/concerns/streamable.rb +++ b/app/models/concerns/streamable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Streamable extend ActiveSupport::Concern diff --git a/app/models/concerns/targetable.rb b/app/models/concerns/targetable.rb index d46590a02..4c335a302 100644 --- a/app/models/concerns/targetable.rb +++ b/app/models/concerns/targetable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Targetable extend ActiveSupport::Concern diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 8f9eb1182..9075b90a0 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class DomainBlock < ApplicationRecord validates :domain, presence: true, uniqueness: true diff --git a/app/models/favourite.rb b/app/models/favourite.rb index 60d853ce2..0a4f60ecb 100644 --- a/app/models/favourite.rb +++ b/app/models/favourite.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Favourite < ApplicationRecord include Paginable include Streamable diff --git a/app/models/feed.rb b/app/models/feed.rb index 408403873..e7f2ab3a5 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Feed def initialize(type, account) @type = type @@ -28,6 +30,6 @@ class Feed end def redis - $redis + Redis.current end end diff --git a/app/models/follow.rb b/app/models/follow.rb index 720812b6d..c918dabf2 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Follow < ApplicationRecord include Paginable include Streamable diff --git a/app/models/follow_suggestion.rb b/app/models/follow_suggestion.rb index ee76d4b6a..2daa40dcb 100644 --- a/app/models/follow_suggestion.rb +++ b/app/models/follow_suggestion.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class FollowSuggestion class << self def get(for_account_id, limit = 10) diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index a740792f2..d3e3d73be 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class MediaAttachment < ApplicationRecord IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze @@ -6,9 +8,9 @@ class MediaAttachment < ApplicationRecord belongs_to :status, inverse_of: :media_attachments has_attached_file :file, - styles: -> (f) { file_styles f }, - processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] }, - convert_options: { all: "-strip" } + styles: -> (f) { file_styles f }, + processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] }, + convert_options: { all: '-strip' } validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES validates_attachment_size :file, less_than: 4.megabytes @@ -20,8 +22,8 @@ class MediaAttachment < ApplicationRecord def file_remote_url=(url) self.file = URI.parse(url) - rescue OpenURI::HTTPError - # + rescue OpenURI::HTTPError => e + Rails.logger.debug "Error fetching remote attachment: #{e}" end def image? @@ -43,19 +45,19 @@ class MediaAttachment < ApplicationRecord if f.instance.image? { original: '100%', - small: '510x680>' + small: '510x680>', } else { small: { convert_options: { output: { - vf: 'scale=\'min(510\, iw):min(680\, ih)\':force_original_aspect_ratio=decrease' - } + vf: 'scale=\'min(510\, iw):min(680\, ih)\':force_original_aspect_ratio=decrease', + }, }, format: 'png', - time: 1 - } + time: 1, + }, } end end diff --git a/app/models/mention.rb b/app/models/mention.rb index b39fa2cbb..a3c8baf21 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Mention < ApplicationRecord belongs_to :account, inverse_of: :mentions belongs_to :status diff --git a/app/models/status.rb b/app/models/status.rb index 9e2711008..c6f5c8d6c 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Status < ApplicationRecord include Paginable include Streamable @@ -89,22 +91,17 @@ class Status < ApplicationRecord def as_public_timeline(account = nil) query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id').where('accounts.silenced = FALSE') - - unless account.nil? - query = filter_timeline(query, account) - end + query = filter_timeline(query, account) unless account.nil? query.with_includes.with_counters end def as_tag_timeline(tag, account = nil) query = tag.statuses - .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id') - .where('accounts.silenced = FALSE') + .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id') + .where('accounts.silenced = FALSE') - unless account.nil? - query = filter_timeline(query, account) - end + query = filter_timeline(query, account) unless account.nil? query.with_includes.with_counters end diff --git a/app/models/stream_entry.rb b/app/models/stream_entry.rb index f8272be17..f6c8f461b 100644 --- a/app/models/stream_entry.rb +++ b/app/models/stream_entry.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class StreamEntry < ApplicationRecord include Paginable @@ -15,7 +17,11 @@ class StreamEntry < ApplicationRecord scope :with_includes, -> { includes(:account, status: STATUS_INCLUDES, favourite: [:account, :stream_entry, status: STATUS_INCLUDES], follow: [:target_account, :stream_entry]) } def object_type - orphaned? ? :activity : (targeted? ? :activity : activity.object_type) + if orphaned? + :activity + else + targeted? ? :activity : activity.object_type + end end def verb diff --git a/app/models/tag.rb b/app/models/tag.rb index ac89c9bff..e5b0511ae 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Tag < ApplicationRecord has_and_belongs_to_many :statuses diff --git a/app/models/user.rb b/app/models/user.rb index de3521989..4a330d8ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable -- cgit