Remove where clause from followable button partial and rename followable_button partial to follow_button.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-19 17:48:57 +02:00
parent c9a6630c94
commit 119a6b3397
7 changed files with 42 additions and 44 deletions

View File

@@ -3,14 +3,13 @@ class FollowsController < ApplicationController
load_and_authorize_resource load_and_authorize_resource
def create def create
@followable = find_followable followable = find_followable
@follow = Follow.create(user: current_user, followable: @followable) @follow = Follow.create(user: current_user, followable: followable)
render :refresh_follow_button render :refresh_follow_button
end end
def destroy def destroy
@follow = Follow.find(params[:id]) @follow = Follow.find(params[:id])
@followable = @follow.followable
@follow.destroy @follow.destroy
render :refresh_follow_button render :refresh_follow_button
end end
@@ -20,4 +19,5 @@ class FollowsController < ApplicationController
def find_followable def find_followable
params[:followable_type].constantize.find(params[:followable_id]) params[:followable_type].constantize.find(params[:followable_id])
end end
end end

View File

@@ -1,11 +1,7 @@
module FollowablesHelper module FollowablesHelper
def show_follow_action?(followable) def followed?(user, followable)
current_user && !followed?(followable) Follow.followed?(user, followable)
end
def show_unfollow_action?(followable)
current_user && followed?(followable)
end end
def followable_type_title(followable_type) def followable_type_title(followable_type)
@@ -31,10 +27,11 @@ module FollowablesHelper
followable.class.to_s.parameterize.gsub('-', '_') followable.class.to_s.parameterize.gsub('-', '_')
end end
private def find_or_build_follow(user, followable)
if followed?(user, followable)
def followed?(followable) return Follow.find_by(user: user, followable: followable)
Follow.followed?(current_user, followable)
end end
Follow.new(user: user, followable: followable)
end
end end

View File

@@ -113,7 +113,9 @@
url: budget_investment_url(budget_id: investment.budget_id, id: investment.id) url: budget_investment_url(budget_id: investment.budget_id, id: investment.id)
} %> } %>
<%= render 'follows/followable_button', followable: investment if current_user %> <% if current_user %>
<%= render 'follows/follow_button', follow: find_or_build_follow(current_user, investment) %>
<% end %>
</aside> </aside>
</div> </div>

View File

@@ -0,0 +1,24 @@
<span class="js-follow">
<span class="followable-content">
<% unless followed?(current_user, follow.followable) %>
<%= link_to t('shared.follow'),
follows_path(followable_id: follow.followable.id,
followable_type: follow.followable.class.name),
method: :post, remote: true,
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>
</span>

View File

@@ -1,27 +0,0 @@
<span class="js-follow">
<span class="followable-content">
<% if show_follow_action? followable %>
<%= link_to t('shared.follow'),
follows_path(followable_id: followable.id,
followable_type: followable.class.name),
method: :post, remote: true,
title: follow_text(followable),
class: 'button hollow' %>
<% end %>
<% if show_unfollow_action? followable %>
<% follow = followable.follows.where(user: current_user).first %>
<%= link_to t('shared.unfollow'),
follow_path(follow),
method: :delete, remote: true,
title: unfollow_text(followable),
class: 'button hollow' %>
<% end %>
</span>
</span>

View File

@@ -1,2 +1,2 @@
App.Followable.update("<%= dom_id(@followable) %>", App.Followable.update("<%= dom_id(@follow.followable) %>",
"<%= j render('followable_button', followable: @followable) %>") "<%= j render('follow_button', follow: @follow) %>")

View File

@@ -140,7 +140,9 @@
url: proposal_url(@proposal) url: proposal_url(@proposal)
} %> } %>
<%= render 'follows/followable_button', followable: @proposal if current_user %> <% if current_user %>
<%= render 'follows/follow_button', follow: find_or_build_follow(current_user, @proposal) %>
<% end %>
</aside> </aside>
</div> </div>