Skip to content

Commit

Permalink
Working follow & unfollow buttons (with Ajax)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 31, 2010
1 parent 135e233 commit 7a4092f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class RelationshipsController < ApplicationController
before_filter :authenticate

def create
@user = User.find(params[:relationship][: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)
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").update("<%= escape_javascript(render('users/unfollow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
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").update("<%= escape_javascript(render('users/follow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
3 changes: 2 additions & 1 deletion app/views/users/_follow.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= form_for(current_user.relationships.
build(:followed_id => @user.id)) do |f| %>
build(:followed_id => @user.id),
:remote => true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% 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.relationships.find_by_followed_id(@user),
:html => { :method => :delete }) do |f| %>
:html => { :method => :delete },
:remote => true) do |f| %>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
62 changes: 62 additions & 0 deletions spec/controllers/relationships_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

describe RelationshipsController do

describe "access control" do
it "should require signin for create" do
post :create
response.should redirect_to(signin_path)
end

it "should require signin for destroy" do
delete :destroy, :id => 1
response.should redirect_to(signin_path)
end
end

describe "POST 'create'" do

before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
end

it "should create a relationship" do
lambda do
post :create, :relationship => { :followed_id => @followed }
response.should redirect_to(user_path(@followed))
end.should change(Relationship, :count).by(1)
end

it "should create a relationship with Ajax" do
lambda do
xhr :post, :create, :relationship => { :followed_id => @followed }
response.should be_success
end.should change(Relationship, :count).by(1)
end
end

describe "DELETE 'destroy'" do

before(:each) do
@user = test_sign_in(Factory(:user))
@followed = Factory(:user, :email => Factory.next(:email))
@user.follow!(@followed)
@relationship = @user.relationships.find_by_followed_id(@followed)
end

it "should destroy a relationship" do
lambda do
delete :destroy, :id => @relationship
response.should redirect_to(user_path(@followed))
end.should change(Relationship, :count).by(-1)
end

it "should destroy a relationship with Ajax" do
lambda do
xhr :delete, :destroy, :id => @relationship
response.should be_success
end.should change(Relationship, :count).by(-1)
end
end
end

0 comments on commit 7a4092f

Please sign in to comment.