-
Notifications
You must be signed in to change notification settings - Fork 0
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
thirotan
committed
Dec 19, 2015
1 parent
ae87b48
commit 224c0b7
Showing
25 changed files
with
447 additions
and
25 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
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_action :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Relationship < ActiveRecord::Base | ||
belongs_to :follower, class_name: "User" | ||
belongs_to :followed, class_name: "User" | ||
validates :follower_id, presence: true | ||
validates :followed_id, presence: true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$("#follow_form").html("<%= escape_jaevscript(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_jaevscript(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
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,15 @@ | ||
<% @user ||= current_user %> | ||
<div class="stats"> | ||
<a href="<%= following_user_path(@user) %>"> | ||
<strong id="following" class="stat"> | ||
<%= @user.followed_users.count %> | ||
</strong> | ||
following | ||
</a> | ||
<a href="<%= followers_user_path(@user) %>"> | ||
<strong id="followers" class="stat"> | ||
<%= @user.followers.count %> | ||
</strong> | ||
followers | ||
</a> | ||
</div> |
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,5 @@ | ||
<%= 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% unless current_user?(@user) %> | ||
<div id="follow_form"> | ||
<% if current_user.following?(@user) %> | ||
<%= render 'unfollow' %> | ||
<% else %> | ||
<%= render 'follow' %> | ||
<% end %> | ||
</div> | ||
<% 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,4 @@ | ||
<%= form_for(current_user.relationships.find_by(followed_id: @user.id), | ||
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
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,30 @@ | ||
<% provide(:title, @title) %> | ||
<div class="row"> | ||
<aside class="span4"> | ||
<section> | ||
<%= gravatar_for @user %> | ||
<h1><%= @user.name %></h1> | ||
<span><%= link_to "view my profile", @user %></span> | ||
<span><b>Microposts:</b> <%= @user.microposts.count %></span> | ||
</section> | ||
<section> | ||
<%= render 'shared/stats' %> | ||
<% if @users.any? %> | ||
<div class="user_avatars"> | ||
<% @users.each do |user| %> | ||
<%= link_to gravatar_for(user, size: 30), user %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</section> | ||
</aside> | ||
<div class="span8"> | ||
<h3><%= @title %></h3> | ||
<% if @users.any? %> | ||
<ul class="users"> | ||
<%= render @users %> | ||
</ul> | ||
<%= will_paginate %> | ||
<% end %> | ||
</div> | ||
</div> |
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,13 @@ | ||
class CreateRelationships < ActiveRecord::Migration | ||
def change | ||
create_table :relationships do |t| | ||
t.integer :follower_id | ||
t.integer :followed_id | ||
|
||
t.timestamps null: false | ||
end | ||
add_index :relationships, :follower_id | ||
add_index :relationships, :followed_id | ||
add_index :relationships, [:follower_id, :followed_id], unique: true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
namespace :db do | ||
desc "Fill database with sample data" | ||
task populate: :environment do | ||
User.create!(name: "Example User", | ||
email: "example@railstutorial.jp", | ||
password: "foobar", | ||
password_confirmation: "foobar", | ||
admin: true) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.jp" | ||
password = "password" | ||
User.create!(name: name, | ||
email: email, | ||
password: password, | ||
password_confirmation: password) | ||
end | ||
users = User.all.limit(6) | ||
50.times do | ||
content = Faker::Lorem.sentence(5) | ||
users.each { |user| user.microposts.create!(content: content) } | ||
end | ||
make_users | ||
make_microposts | ||
make_relationships | ||
end | ||
end | ||
|
||
def make_users | ||
admin = User.create!(name: "Example User", | ||
email: "example@railstutorial.jp", | ||
password: "foobar", | ||
password_confirmation: "foobar", | ||
admin: true) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.jp" | ||
password = "password" | ||
User.create!(name: name, | ||
email: email, | ||
password: password, | ||
password_confirmation: password) | ||
end | ||
end | ||
|
||
def make_microposts | ||
users = User.all.limit(6) | ||
50.times do | ||
content = Faker::Lorem.sentence(5) | ||
users.each { |user| user.microposts.create!(content: content) } | ||
end | ||
end | ||
|
||
def make_relationships | ||
users = User.all | ||
user = users.first | ||
followed_users = users[2..50] | ||
followers = users[3..40] | ||
followed_users.each { |followed| user.follow!(followed) } | ||
followers.each { |follower| follower.follow!(user) } | ||
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,40 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe RelationshipsController do | ||
let(:user) { FactoryGirl.create(:user) } | ||
let(:other_user) { FactoryGirl.create(:user) } | ||
|
||
before { sign_in user, no_capybara: true } | ||
|
||
describe "creating a relationship with Ajac" do | ||
it "should increment the Relationship count" do | ||
expect do | ||
xhr :post, :create, relationship: { followed_id: other_user.id } | ||
end.to change(Relationship, :count).by(1) | ||
end | ||
|
||
it "should respond with cuccess" do | ||
xhr :post, :create, relationship: { followed_id: other_user.id } | ||
expect(response).to be_success | ||
end | ||
end | ||
|
||
describe "destroying a relationship with Ajax" do | ||
before { user.follow!(other_user) } | ||
|
||
let(:relationship) do | ||
user.relationships.find_by(followed_id: other_user.id) | ||
end | ||
|
||
it "should decrement the Relationship count" do | ||
expect do | ||
xhr :delete, :destroy, id: relationship.id | ||
end.to change(Relationship, :count).by(-1) | ||
end | ||
|
||
it "shoud respond with success" do | ||
xhr :delete, :destroy, id: relationship.id | ||
expect(response).to be_success | ||
end | ||
end | ||
end |
Oops, something went wrong.