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

@@ -3,8 +3,7 @@ class FollowsController < ApplicationController
load_and_authorize_resource load_and_authorize_resource
def create def create
followable = find_followable @follow = Follow.create(user: current_user, followable: find_followable)
@follow = Follow.create(user: current_user, followable: followable)
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice_html") flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice_html")
render :refresh_follow_button render :refresh_follow_button
end end
@@ -23,7 +22,7 @@ class FollowsController < ApplicationController
end end
def followable_translation_key(followable) def followable_translation_key(followable)
followable.class.name.parameterize.gsub("-", "_") followable.class.name.parameterize("_")
end end
end end

View File

@@ -1,9 +1,5 @@
module FollowablesHelper module FollowablesHelper
def followed?(user, followable)
Follow.followed?(user, followable)
end
def followable_type_title(followable_type) def followable_type_title(followable_type)
t("activerecord.models.#{followable_type.underscore}.other") t("activerecord.models.#{followable_type.underscore}.other")
end end
@@ -28,10 +24,7 @@ module FollowablesHelper
end end
def find_or_build_follow(user, followable) def find_or_build_follow(user, followable)
if followed?(user, followable) Follow.find_or_initialize_by(user: user, followable: followable)
return Follow.find_by(user: user, followable: followable)
end
Follow.new(user: user, followable: followable)
end end
end end

View File

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

View File

@@ -6,15 +6,4 @@ class Follow < ActiveRecord::Base
validates :followable_id, presence: true validates :followable_id, presence: true
validates :followable_type, 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 end

View File

@@ -1,7 +1,15 @@
<span class="js-follow"> <span class="js-follow">
<span class="followable-content"> <span class="followable-content">
<% 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'), <%= link_to t('shared.follow'),
follows_path(followable_id: follow.followable.id, follows_path(followable_id: follow.followable.id,
@@ -10,14 +18,6 @@
title: follow_text(follow.followable), title: follow_text(follow.followable),
class: 'button hollow' %> 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 %> <% end %>
</span> </span>