diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index 3c3300507..05f0e8f96 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -3,8 +3,7 @@ class FollowsController < ApplicationController load_and_authorize_resource def create - followable = find_followable - @follow = Follow.create(user: current_user, followable: followable) + @follow = Follow.create(user: current_user, followable: find_followable) flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice_html") render :refresh_follow_button end @@ -23,7 +22,7 @@ class FollowsController < ApplicationController end def followable_translation_key(followable) - followable.class.name.parameterize.gsub("-", "_") + followable.class.name.parameterize("_") end end diff --git a/app/helpers/followables_helper.rb b/app/helpers/followables_helper.rb index 3d9743d06..b7fe1f199 100644 --- a/app/helpers/followables_helper.rb +++ b/app/helpers/followables_helper.rb @@ -1,9 +1,5 @@ module FollowablesHelper - def followed?(user, followable) - Follow.followed?(user, followable) - end - def followable_type_title(followable_type) t("activerecord.models.#{followable_type.underscore}.other") end @@ -28,10 +24,7 @@ module FollowablesHelper end def find_or_build_follow(user, followable) - if followed?(user, followable) - return Follow.find_by(user: user, followable: followable) - end - Follow.new(user: user, followable: followable) + Follow.find_or_initialize_by(user: user, followable: followable) end end diff --git a/app/models/concerns/followable.rb b/app/models/concerns/followable.rb index 51469698e..6231a24f4 100644 --- a/app/models/concerns/followable.rb +++ b/app/models/concerns/followable.rb @@ -6,4 +6,8 @@ module Followable has_many :followers, through: :follows, source: :user end + def followed_by?(user) + followers.include?(user) + end + end diff --git a/app/models/follow.rb b/app/models/follow.rb index 6c2c62e99..2da52127f 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -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 diff --git a/app/views/follows/_follow_button.html.erb b/app/views/follows/_follow_button.html.erb index dcec283d8..547e880b9 100644 --- a/app/views/follows/_follow_button.html.erb +++ b/app/views/follows/_follow_button.html.erb @@ -1,7 +1,15 @@ - <% unless followed?(current_user, follow.followable) %> + <% if follow.followable.followed_by?(current_user) %> + + <%= link_to t('shared.unfollow'), + follow_path(follow), + method: :delete, remote: true, + title: unfollow_text(follow.followable), + class: 'button hollow' %> + + <% else %> <%= link_to t('shared.follow'), follows_path(followable_id: follow.followable.id, @@ -10,14 +18,6 @@ title: follow_text(follow.followable), class: 'button hollow' %> - <% else %> - - <%= link_to t('shared.unfollow'), - follow_path(follow), - method: :delete, remote: true, - title: unfollow_text(follow.followable), - class: 'button hollow' %> - <% end %>