Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add user following
- Loading branch information
Showing
20 changed files
with
260 additions
and
27 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
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_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 |
---|---|---|
@@ -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
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,5 @@ | ||
<%= 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
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 | ||
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,26 +1,42 @@ | ||
namespace :db do | ||
|
||
desc "Fill database with sample data" | ||
task populate: :environment do | ||
|
||
User.create!(name: "Example User", | ||
email: "example@railstutorial.org", | ||
password: "foobar", | ||
password_confirmation: "foobar") | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
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 | ||
end | ||
make_users | ||
make_microposts | ||
make_relationships | ||
end | ||
end | ||
|
||
def make_users | ||
admin = User.create!(name: "Example User", | ||
email: "example@railstutorial.org", | ||
password: "foobar", | ||
password_confirmation: "foobar", | ||
admin: true) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
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,5 @@ | ||
require 'spec_helper' | ||
|
||
describe Relationship do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |