Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User page: new following section #1750

Merged
merged 17 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove complex methods from follow model. Create instance method foll…
…owed_by? on followable model concern. Some code improvements.
  • Loading branch information
Senen committed Jul 19, 2017
commit b7bce2e69948f8dfad4f74133fd374c1cf271dea
5 changes: 2 additions & 3 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +22,7 @@ def find_followable
end

def followable_translation_key(followable)
followable.class.name.parameterize.gsub("-", "_")
followable.class.name.parameterize("_")
end

end
9 changes: 1 addition & 8 deletions app/helpers/followables_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,10 +24,7 @@ def followable_class_name(followable)
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
4 changes: 4 additions & 0 deletions app/models/concerns/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ module Followable
has_many :followers, through: :follows, source: :user
end

def followed_by?(user)
followers.include?(user)
end

end
11 changes: 0 additions & 11 deletions app/models/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions app/views/follows/_follow_button.html.erb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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>
Expand Down