diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index 9cec999fa..596aa657f 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -3,14 +3,13 @@ class FollowsController < ApplicationController load_and_authorize_resource def create - @followable = find_followable - @follow = Follow.create(user: current_user, followable: @followable) + followable = find_followable + @follow = Follow.create(user: current_user, followable: followable) render :refresh_follow_button end def destroy @follow = Follow.find(params[:id]) - @followable = @follow.followable @follow.destroy render :refresh_follow_button end @@ -20,4 +19,5 @@ class FollowsController < ApplicationController def find_followable params[:followable_type].constantize.find(params[:followable_id]) end + end diff --git a/app/helpers/followables_helper.rb b/app/helpers/followables_helper.rb index b1e1e83d3..3d9743d06 100644 --- a/app/helpers/followables_helper.rb +++ b/app/helpers/followables_helper.rb @@ -1,11 +1,7 @@ module FollowablesHelper - def show_follow_action?(followable) - current_user && !followed?(followable) - end - - def show_unfollow_action?(followable) - current_user && followed?(followable) + def followed?(user, followable) + Follow.followed?(user, followable) end def followable_type_title(followable_type) @@ -31,10 +27,11 @@ module FollowablesHelper followable.class.to_s.parameterize.gsub('-', '_') end - private - - def followed?(followable) - Follow.followed?(current_user, followable) + 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) + end end diff --git a/app/views/budgets/investments/_investment_show.html.erb b/app/views/budgets/investments/_investment_show.html.erb index 950b93b6a..434934c7e 100644 --- a/app/views/budgets/investments/_investment_show.html.erb +++ b/app/views/budgets/investments/_investment_show.html.erb @@ -113,7 +113,9 @@ 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 %> diff --git a/app/views/follows/_follow_button.html.erb b/app/views/follows/_follow_button.html.erb new file mode 100644 index 000000000..dcec283d8 --- /dev/null +++ b/app/views/follows/_follow_button.html.erb @@ -0,0 +1,24 @@ + + + + <% 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 %> + + + \ No newline at end of file diff --git a/app/views/follows/_followable_button.html.erb b/app/views/follows/_followable_button.html.erb deleted file mode 100644 index e96f1e179..000000000 --- a/app/views/follows/_followable_button.html.erb +++ /dev/null @@ -1,27 +0,0 @@ - - - - <% 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 %> - - - \ No newline at end of file diff --git a/app/views/follows/refresh_follow_button.js.erb b/app/views/follows/refresh_follow_button.js.erb index 31e2627a3..1a5b58903 100644 --- a/app/views/follows/refresh_follow_button.js.erb +++ b/app/views/follows/refresh_follow_button.js.erb @@ -1,2 +1,2 @@ -App.Followable.update("<%= dom_id(@followable) %>", - "<%= j render('followable_button', followable: @followable) %>") +App.Followable.update("<%= dom_id(@follow.followable) %>", + "<%= j render('follow_button', follow: @follow) %>") diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index d5c83d9f5..6a8a56738 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -140,7 +140,9 @@ 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 %>