Skip to content

Commit

Permalink
Merge branch 'following-users'
Browse files Browse the repository at this point in the history
  • Loading branch information
zyuanhong committed Jul 5, 2013
2 parents 81bb49e + a3636a3 commit 7b3f5ee
Show file tree
Hide file tree
Showing 27 changed files with 516 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0
2.0.0-p247
29 changes: 29 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ aside {
margin-right: 10px;
}

.stats {
overflow: auto;
a {
float: left;
padding: 0 10px;
border-left: 1px solid $grayLighter;
color: gray;
&:first-child {
padding-left: 0;
border: 0;
}
&:hover {
text-decoration: none;
color: $blue;
}
}
strong {
display: block;
}
}

.user_avatars {
overflow: auto;
margin-top: 10px;
.gravatar {
margin: 1px 1px;
}
}

/* forms */

input, textarea, select, .uneditable-input {
Expand Down
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 :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
17 changes: 16 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class UsersController < ApplicationController
before_filter :signed_in_user, :only => [:index, :edit, :update, :destroy]
before_filter :signed_in_user,
:only => [:index, :edit, :update, :destroy, :following, :followers]
before_filter :correct_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy
before_filter :already_login, :only => [:new, :create]
Expand Down Expand Up @@ -52,6 +53,20 @@ def destroy
redirect_to users_url
end

def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(:page => params[:page])
render 'show_follow'
end

def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(:page => params[:page])
render 'show_follow'
end

private

def correct_user
Expand Down
9 changes: 8 additions & 1 deletion app/models/micropost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ class Micropost < ActiveRecord::Base

belongs_to :user

validates :content, :presence => true, :length => { :maximum => 140 }
validates :user_id, :presence => true
validates :content, :presence => true, :length => { :maximum => 140 }

default_scope :order => 'microposts.created_at DESC'

def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
:user_id => user.id)
end
end
20 changes: 20 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# == Schema Information
#
# Table name: relationships
#
# id :integer not null, primary key
# follower_id :integer
# followed_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Relationship < ActiveRecord::Base
attr_accessible :followed_id

belongs_to :follower, :class_name => "User"
belongs_to :followed, :class_name => "User"

validates :follower_id, :presence => true
validates :followed_id, :presence => true
end
29 changes: 24 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@

class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, :dependent => :destroy

has_many :microposts, :dependent => :destroy
has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
has_many :followed_users, :through => :relationships, :source => :followed
has_many :reverse_relationships, :foreign_key => "followed_id",
:class_name => "Relationship",
:dependent => :destroy
has_many :followers, :through => :reverse_relationships, :source => :follower

before_save { self.email.downcase! }
before_save :create_remember_token
Expand All @@ -25,12 +31,25 @@ class User < ActiveRecord::Base
validates :email, :presence => true,
:format => { :with => VALID_EMAIL_REGEX },
:uniqueness => { :case_sensitive => false }
validates :password, :length => { :minimum => 6 }
has_secure_password
validates :password, :presence => true, :length => { :minimum => 6 }
validates :password_confirmation, :presence => true
after_validation { self.errors.messages.delete(:password_digest) }

def feed
# This is preliminary. See "Following users" for the full implementation.
Micropost.where("user_id = ?", id)
Micropost.from_users_followed_by(self)
end

def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end

def follow!(other_user)
relationships.create!(:followed_id => other_user.id)
end

def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end

private
Expand Down
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").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')
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").html("<%= escape_javascript(render('users/follow')) %>")
$("#followers").html('<%= @user.followers.count %>')
15 changes: 15 additions & 0 deletions app/views/shared/_stats.html.erb
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>
5 changes: 4 additions & 1 deletion app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/stats' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
Expand All @@ -15,7 +18,7 @@
</div>
<% else %>
<div class="center hero-unit">
<h1>Weclome to the Sample App</h1>
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
Expand Down
5 changes: 5 additions & 0 deletions app/views/users/_follow.html.erb
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 %>
9 changes: 9 additions & 0 deletions app/views/users/_follow_form.html.erb
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 %>
5 changes: 5 additions & 0 deletions app/views/users/_unfollow.html.erb
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 %>
4 changes: 4 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
<%= @user.name %>
</h1>
</section>
<section>
<%= render 'shared/stats' %>
</section>
</aside>
<div class="span8">
<%= render 'follow_form' if signed_in? %>
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
Expand Down
30 changes: 30 additions & 0 deletions app/views/users/show_follow.html.erb
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>
5 changes: 1 addition & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
activerecord:
attributes:
user:
password_digest: "Password"
hello: "Hello world"
13 changes: 10 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
SampleApp2ndEd::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]

resources :users do
member do
get :following, :followers
end
end

resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]

root :to => 'static_pages#home'

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20130605125024_create_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130521055340) do
ActiveRecord::Schema.define(:version => 20130605125024) do

create_table "microposts", :force => true do |t|
t.string "content"
Expand All @@ -22,6 +22,17 @@

add_index "microposts", ["user_id", "created_at"], :name => "index_microposts_on_user_id_and_created_at"

create_table "relationships", :force => true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "relationships", ["followed_id"], :name => "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], :name => "index_relationships_on_follower_id_and_followed_id", :unique => true
add_index "relationships", ["follower_id"], :name => "index_relationships_on_follower_id"

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
Expand Down
Loading

0 comments on commit 7b3f5ee

Please sign in to comment.