diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb new file mode 100644 index 0000000..c7e69c8 --- /dev/null +++ b/app/controllers/relationships_controller.rb @@ -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 \ No newline at end of file diff --git a/app/views/relationships/create.js.erb b/app/views/relationships/create.js.erb new file mode 100644 index 0000000..a3e2b09 --- /dev/null +++ b/app/views/relationships/create.js.erb @@ -0,0 +1,2 @@ +$("follow_form").update("<%= escape_javascript(render('users/unfollow')) %>") +$("followers").update('<%= "#{@user.followers.count} followers" %>') \ No newline at end of file diff --git a/app/views/relationships/destroy.js.erb b/app/views/relationships/destroy.js.erb new file mode 100644 index 0000000..32cd47c --- /dev/null +++ b/app/views/relationships/destroy.js.erb @@ -0,0 +1,2 @@ +$("follow_form").update("<%= escape_javascript(render('users/follow')) %>") +$("followers").update('<%= "#{@user.followers.count} followers" %>') \ No newline at end of file diff --git a/app/views/users/_follow.html.erb b/app/views/users/_follow.html.erb index 23c9484..8fd790d 100644 --- a/app/views/users/_follow.html.erb +++ b/app/views/users/_follow.html.erb @@ -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| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow" %>
<% end %> \ No newline at end of file diff --git a/app/views/users/_unfollow.html.erb b/app/views/users/_unfollow.html.erb index 7aa0e67..e34c12e 100644 --- a/app/views/users/_unfollow.html.erb +++ b/app/views/users/_unfollow.html.erb @@ -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| %>
<%= f.submit "Unfollow" %>
<% end %> \ No newline at end of file diff --git a/spec/controllers/relationships_controller_spec.rb b/spec/controllers/relationships_controller_spec.rb new file mode 100644 index 0000000..34fe0a8 --- /dev/null +++ b/spec/controllers/relationships_controller_spec.rb @@ -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 \ No newline at end of file