Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f6f528
commit 42433f1
Showing
10 changed files
with
160 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class RelationshipsController < ApplicationController | ||
before_filter :signed_in_user | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %>') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %>') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<%= form_for(current_user.relationships.build(followed_id: @user.id)) do |f| %> | ||
<%= form_for(current_user.relationships.build(followed_id: @user.id), remote: true) do |f| %> | ||
<div><%= f.hidden_field :followed_id %></div> | ||
<%= f.submit "Follow", class: "btn btn-large btn-primary" %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<%= form_for(current_user.relationships.find_by_followed_id(@user), html: { method: :delete}) do |f| %> | ||
<%= form_for(current_user.relationships.find_by_followed_id(@user), html: { method: :delete}, remote: true) do |f| %> | ||
<%= f.submit "Unfollow", class: "btn btn-large" %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'spec_helper' | ||
|
||
describe RelationshipsController do | ||
|
||
let(:user) { FactoryGirl.create(:user) } | ||
let(:other_user) { FactoryGirl.create(:user) } | ||
|
||
before { sign_in user } | ||
|
||
describe "creating a relationship with Ajax" do | ||
|
||
it "should increment the Relationship count" do | ||
expect do | ||
xhr :post, :create, relationship: { followed_id: other_user.id } | ||
end.should change(Relationship, :count).by(1) | ||
end | ||
|
||
it "should respond with success" do | ||
xhr :post, :create, relationship: { followed_id: other_user.id } | ||
response.should be_success | ||
end | ||
end | ||
|
||
describe "destroying a relationship with Ajax" do | ||
|
||
before { user.follow!(other_user) } | ||
let(:relationship) { user.relationships.find_by_followed_id(other_user) } | ||
|
||
it "should decrement the Relationship count" do | ||
expect do | ||
xhr :delete, :destroy, id: relationship.id | ||
end.should change(Relationship, :count).by(-1) | ||
end | ||
|
||
it "should respond with success" do | ||
xhr :delete, :destroy, id: relationship.id | ||
response.should be_success | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters