Remove complex methods from follow model. Create instance method followed_by? on followable model concern. Some code improvements.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,4 +6,8 @@ module Followable
|
||||
has_many :followers, through: :follows, source: :user
|
||||
end
|
||||
|
||||
def followed_by?(user)
|
||||
followers.include?(user)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
<span class="js-follow">
|
||||
<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'),
|
||||
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 %>
|
||||
|
||||
</span>
|
||||
|
||||
Reference in New Issue
Block a user