Remove complex methods from follow model. Create instance method followed_by? on followable model concern. Some code improvements.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-19 21:50:37 +02:00
parent dec7d2d2b5
commit b7bce2e699
5 changed files with 16 additions and 31 deletions

View File

@@ -6,4 +6,8 @@ module Followable
has_many :followers, through: :follows, source: :user
end
def followed_by?(user)
followers.include?(user)
end
end

View File

@@ -6,15 +6,4 @@ class Follow < ActiveRecord::Base
validates :followable_id, presence: true
validates :followable_type, presence: true
scope(:by_user_and_followable, lambda do |user, followable|
where(user_id: user.id,
followable_type: followable.class.to_s,
followable_id: followable.id)
end)
def self.followed?(user, followable)
return false unless user
!! by_user_and_followable(user, followable).try(:first)
end
end