Skip to content

Commit

Permalink
added ajax functionality to following/unfollowing, fixed typo in rela…
Browse files Browse the repository at this point in the history
…tionships controller
  • Loading branch information
scarvill91 committed Apr 29, 2015
1 parent 2e3f9c9 commit bcdbb56
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
18 changes: 12 additions & 6 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ class RelationshipsController < ApplicationController
before_action :logged_in_user

def create
user = User.find(params[:followed_id])
current_user.follow(user)
redirect_to user
@user = User.find(params[:followed_id])
current_user.follow(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end

def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
redirect_to user
@user = Relationship.find(params[:id]).followed
current_user.unfollow(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
2 changes: 2 additions & 0 deletions app/views/relationships/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')
2 changes: 2 additions & 0 deletions app/views/relationships/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>")
$("#followers").html('<%= @user.followers.count %>')
2 changes: 1 addition & 1 deletion app/views/users/_follow.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_for(current_user.active_relationships.build) do |f| %>
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/users/_unfollow.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }) do |f| %>
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ class Application < Rails::Application

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.action_view.embed_authenticity_token_in_remote_forms = true
end
end
31 changes: 30 additions & 1 deletion test/integration/following_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class FollowingTest < ActionDispatch::IntegrationTest

def setup
@user = users(:michael)
@user = users(:michael)
@other = users(:archer)
log_in_as(@user)
end

Expand All @@ -24,4 +25,32 @@ def setup
assert_select "a[href=?]", user_path(user)
end
end

test "should follow a user the standard way" do
assert_difference '@user.following.count', 1 do
post relationships_path, followed_id: @other.id
end
end

test "should follow a user with Ajax" do
assert_difference '@user.following.count', 1 do
xhr :post, relationships_path, followed_id: @other.id
end
end

test "should unfollow a user the standard way" do
@user.follow(@other)
relationship = @user.active_relationships.find_by(followed_id: @other.id)
assert_difference '@user.following.count', -1 do
delete relationship_path(relationship)
end
end

test "should unfollow a user with Ajax" do
@user.follow(@other)
relationship = @user.active_relationships.find_by(followed_id: @other.id)
assert_difference '@user.following.count', -1 do
xhr :delete, relationship_path(relationship)
end
end
end

0 comments on commit bcdbb56

Please sign in to comment.