From 927333f4f89403c5a6a2b421065112e517d88193 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 29 Sep 2016 21:28:21 +0200 Subject: Improve code style --- app/models/account.rb | 32 ++++++++++++++------------------ app/models/concerns/streamable.rb | 2 +- app/models/favourite.rb | 4 ++-- app/models/feed.rb | 4 ++-- app/models/follow.rb | 6 +++--- app/models/media_attachment.rb | 8 ++++---- app/models/status.rb | 28 ++++++++++++++-------------- app/models/stream_entry.rb | 20 ++++++++++---------- app/models/user.rb | 4 ++-- 9 files changed, 52 insertions(+), 56 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 16db2d719..2fbd5a655 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -2,7 +2,7 @@ class Account < ApplicationRecord include Targetable MENTION_RE = /(?:^|\s|\.|>)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'] + IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze # Local users has_one :user, inverse_of: :account @@ -45,11 +45,11 @@ class Account < ApplicationRecord scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers } def follow!(other_account) - self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account) + active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account) end def unfollow!(other_account) - follow = self.active_relationships.find_by(target_account: other_account) + follow = active_relationships.find_by(target_account: other_account) follow.destroy unless follow.nil? end @@ -58,15 +58,15 @@ class Account < ApplicationRecord end def local? - self.domain.nil? + domain.nil? end def acct - local? ? self.username : "#{self.username}@#{self.domain}" + local? ? username : "#{username}@#{domain}" end def subscribed? - !self.subscription_expires_at.nil? + !subscription_expires_at.nil? end def favourited?(status) @@ -78,11 +78,11 @@ class Account < ApplicationRecord end def keypair - self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key) + private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key) end def subscription(webhook_url) - OStatus2::Subscription.new(self.remote_url, secret: self.secret, lease_seconds: 86400 * 30, webhook: webhook_url, hub: self.hub_url) + OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url) end def ping!(atom_url, hubs) @@ -91,10 +91,7 @@ class Account < ApplicationRecord end def avatar_remote_url=(url) - unless self[:avatar_remote_url] == url - self.avatar = URI.parse(url) - end - + self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url self[:avatar_remote_url] = url end @@ -103,26 +100,25 @@ class Account < ApplicationRecord end def to_param - self.username + username end def self.find_local!(username) - self.find_remote!(username, nil) + find_remote!(username, nil) end def self.find_remote!(username, domain) - table = self.arel_table - self.where(table[:username].matches(username)).where(domain: domain).take! + where(arel_table[:username].matches(username)).where(domain: domain).take! end def self.find_local(username) - self.find_local!(username) + find_local!(username) rescue ActiveRecord::RecordNotFound nil end def self.find_remote(username, domain) - self.find_remote!(username, domain) + find_remote!(username, domain) rescue ActiveRecord::RecordNotFound nil end diff --git a/app/models/concerns/streamable.rb b/app/models/concerns/streamable.rb index 8d754949e..b3354c819 100644 --- a/app/models/concerns/streamable.rb +++ b/app/models/concerns/streamable.rb @@ -25,7 +25,7 @@ module Streamable end after_create do - self.account.stream_entries.create!(activity: self) if self.account.local? + account.stream_entries.create!(activity: self) if account.local? end end end diff --git a/app/models/favourite.rb b/app/models/favourite.rb index e248ae561..3681181e5 100644 --- a/app/models/favourite.rb +++ b/app/models/favourite.rb @@ -11,7 +11,7 @@ class Favourite < ApplicationRecord end def title - "#{self.account.acct} favourited a status by #{self.status.account.acct}" + "#{account.acct} favourited a status by #{status.account.acct}" end def object_type @@ -19,7 +19,7 @@ class Favourite < ApplicationRecord end def thread - self.status + status end def target diff --git a/app/models/feed.rb b/app/models/feed.rb index bc3e960d4..5807ff176 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -7,11 +7,11 @@ class Feed def get(limit, max_id = nil) max_id = '+inf' if max_id.nil? unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, limit]) - status_map = Hash.new + status_map = {} # If we're after most recent items and none are there, we need to precompute the feed if unhydrated.empty? && max_id == '+inf' - PrecomputeFeedService.new.(@type, @account, limit) + PrecomputeFeedService.new.call(@type, @account, limit) else Status.where(id: unhydrated).with_includes.with_counters.each { |status| status_map[status.id.to_s] = status } unhydrated.map { |id| status_map[id] }.compact diff --git a/app/models/follow.rb b/app/models/follow.rb index 456e2c4f4..4f47580e1 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -8,11 +8,11 @@ class Follow < ApplicationRecord validates :account_id, uniqueness: { scope: :target_account_id } def verb - self.destroyed? ? :unfollow : :follow + destroyed? ? :unfollow : :follow end def target - self.target_account + target_account end def object_type @@ -20,6 +20,6 @@ class Follow < ApplicationRecord end def title - self.destroyed? ? "#{self.account.acct} is no longer following #{self.target_account.acct}" : "#{self.account.acct} started following #{self.target_account.acct}" + destroyed? ? "#{account.acct} is no longer following #{target_account.acct}" : "#{account.acct} started following #{target_account.acct}" end end diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 73fb967c4..e2cbe397e 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -1,18 +1,18 @@ class MediaAttachment < ApplicationRecord - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'] - VIDEO_MIME_TYPES = ['video/webm'] + IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze + VIDEO_MIME_TYPES = ['video/webm'].freeze belongs_to :account, inverse_of: :media_attachments belongs_to :status, inverse_of: :media_attachments - has_attached_file :file, styles: lambda { |f| f.instance.image? ? { small: '510x680>' } : { small: { convert_options: { output: { vf: 'scale="min(510\, iw):min(680\, ih)":force_original_aspect_ratio=decrease' } }, format: 'png', time: 1 } } }, processors: lambda { |f| f.video? ? [:transcoder] : [:thumbnail] } + has_attached_file :file, styles: -> (f) { f.instance.image? ? { small: '510x680>' } : { small: { convert_options: { output: { vf: 'scale="min(510\, iw):min(680\, ih)":force_original_aspect_ratio=decrease' } }, format: 'png', time: 1 } } }, processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] } validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES validates_attachment_size :file, less_than: 4.megabytes validates :account, presence: true def local? - self.remote_url.blank? + remote_url.blank? end def file_remote_url=(url) diff --git a/app/models/status.rb b/app/models/status.rb index cbcde69f4..1f1a70946 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -15,21 +15,21 @@ class Status < ApplicationRecord validates :account, presence: true validates :uri, uniqueness: true, unless: 'local?' - validates :text, presence: true, length: { maximum: 500 }, if: Proc.new { |s| s.local? && !s.reblog? } + validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? } scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') } scope :with_includes, -> { includes(:account, :media_attachments, :stream_entry, mentions: :account, reblog: [:account, mentions: :account], thread: :account) } def local? - self.uri.nil? + uri.nil? end def reblog? - !self.reblog_of_id.nil? + !reblog_of_id.nil? end def reply? - !self.in_reply_to_id.nil? + !in_reply_to_id.nil? end def verb @@ -41,11 +41,11 @@ class Status < ApplicationRecord end def content - reblog? ? self.reblog.text : self.text + reblog? ? reblog.text : text end def target - self.reblog + reblog end def title @@ -53,33 +53,33 @@ class Status < ApplicationRecord end def reblogs_count - self.attributes['reblogs_count'] || self.reblogs.count + attributes['reblogs_count'] || reblogs.count end def favourites_count - self.attributes['favourites_count'] || self.favourites.count + attributes['favourites_count'] || favourites.count end def ancestors - ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', self.id]) - [self]).pluck(:id) + ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id) ids.map { |id| statuses[id].first } end def descendants - ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', self.id]) - [self]).pluck(:id) + ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', id]) - [self]).pluck(:id) statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id) ids.map { |id| statuses[id].first } end def self.as_home_timeline(account) - self.where(account: [account] + account.following).with_includes.with_counters + where(account: [account] + account.following).with_includes.with_counters end def self.as_mentions_timeline(account) - self.where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters + where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters end def self.favourites_map(status_ids, account_id) @@ -87,10 +87,10 @@ class Status < ApplicationRecord end def self.reblogs_map(status_ids, account_id) - self.where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h + where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h end before_validation do - self.text.strip! + text.strip! end end diff --git a/app/models/stream_entry.rb b/app/models/stream_entry.rb index f33295796..261ecda53 100644 --- a/app/models/stream_entry.rb +++ b/app/models/stream_entry.rb @@ -10,16 +10,16 @@ class StreamEntry < ApplicationRecord validates :account, :activity, presence: true - STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, mentions: :account, reblog: [:stream_entry, :account, mentions: :account], thread: [:stream_entry, :account]] + STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, mentions: :account, reblog: [:stream_entry, :account, mentions: :account], thread: [:stream_entry, :account]].freeze 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 : self.activity.object_type) + orphaned? ? :activity : (targeted? ? :activity : activity.object_type) end def verb - orphaned? ? :delete : self.activity.verb + orphaned? ? :delete : activity.verb end def targeted? @@ -27,15 +27,15 @@ class StreamEntry < ApplicationRecord end def target - orphaned? ? nil : self.activity.target + orphaned? ? nil : activity.target end def title - orphaned? ? nil : self.activity.title + orphaned? ? nil : activity.title end def content - orphaned? ? nil : self.activity.content + orphaned? ? nil : activity.content end def threaded? @@ -43,20 +43,20 @@ class StreamEntry < ApplicationRecord end def thread - orphaned? ? nil : self.activity.thread + orphaned? ? nil : activity.thread end def mentions - self.activity.respond_to?(:mentions) ? self.activity.mentions.map { |x| x.account } : [] + activity.respond_to?(:mentions) ? activity.mentions.map { |x| x.account } : [] end def activity - self.send(self.activity_type.downcase.to_sym) + send(activity_type.downcase.to_sym) end private def orphaned? - self.activity.nil? + activity.nil? end end diff --git a/app/models/user.rb b/app/models/user.rb index d4361ffa1..9c0546c2f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,8 +11,8 @@ class User < ApplicationRecord scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') } scope :recent, -> { order('created_at desc') } scope :admins, -> { where(admin: true) } - + def admin? - self.admin + admin end end -- cgit