From d4c94fa004117fdb7226b1b846a12d12dc0542d9 Mon Sep 17 00:00:00 2001 From: Joël Quenneville Date: Fri, 7 Apr 2017 14:18:30 -0400 Subject: DRY up reblog vs original status check Checking reblog vs original status was happening in multiple places across the app. For views, this logic was encapsulated in a helper method named `proper_status` but in the other layers of the app, the logic was duplicated. Because the logic is used at all layers of the app, we extracted it into a `Status#proper` method on the model and changed all uses of the logic to use this method. There is now a single source of truth for this condition. We added test coverage to untested methods that got refactored. --- app/models/account.rb | 4 ++-- app/models/status.rb | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 6968607a2..cbba8b5b6 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -125,11 +125,11 @@ class Account < ApplicationRecord end def favourited?(status) - (status.reblog? ? status.reblog : status).favourites.where(account: self).count.positive? + status.proper.favourites.where(account: self).count.positive? end def reblogged?(status) - (status.reblog? ? status.reblog : status).reblogs.where(account: self).count.positive? + status.proper.reblogs.where(account: self).count.positive? end def keypair diff --git a/app/models/status.rb b/app/models/status.rb index 6948ad77c..7e3dd3e28 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -62,8 +62,12 @@ class Status < ApplicationRecord reply? ? :comment : :note end + def proper + reblog? ? reblog : self + end + def content - reblog? ? reblog.text : text + proper.text end def target -- cgit