Skip to content

Commit

Permalink
Initial interface for following/followers
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 31, 2010
1 parent 997c09e commit 9bc6628
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 33 deletions.
1 change: 1 addition & 0 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
Expand Down
21 changes: 21 additions & 0 deletions app/views/shared/_stats.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<% @user ||= current_user %>
<div class="stats">
<table summary="User stats">
<tr>
<td>
<a href="<%= following_user_path(@user) %>">
<span id="following" class="stat">
<%= @user.following.count %> following
</span>
</a>
</td>
<td>
<a href="<%= followers_user_path(@user) %>">
<span id="followers" class="stat">
<%= pluralize(@user.followers.count, "follower") %>
</span>
</a>
</td>
</tr>
</table>
</div>
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)) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% 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 %>
4 changes: 4 additions & 0 deletions app/views/users/_unfollow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= form_for(current_user.relationships.find_by_followed_id(@user),
:html => { :method => :delete }) do |f| %>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
<%= render 'follow_form' if signed_in? %>
<% if @user.microposts.any? %>
<table class="microposts" summary="User microposts">
<%= render @microposts %>
Expand All @@ -16,6 +17,7 @@
<strong>Name</strong> <%= @user.name %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %>
<strong>Microposts</strong> <%= @user.microposts.count %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
Expand Down
12 changes: 9 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
SampleApp::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 => "pages#home"

Expand Down
55 changes: 36 additions & 19 deletions lib/tasks/sample_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,42 @@ namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
admin = User.create!(:name => "Example User",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
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

User.all(:limit => 6).each do |user|
50.times do
user.microposts.create!(:content => Faker::Lorem.sentence(5))
end
make_users
make_microposts
make_relationships
end
end

def make_users
admin = User.create!(:name => "Example User",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
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
User.all(:limit => 6).each do |user|
50.times do
user.microposts.create!(:content => Faker::Lorem.sentence(5))
end
end
end

def make_relationships
users = User.all
user = users.first
following = users[1..50]
followers = users[3..40]
following.each { |followed| user.follow!(followed) }
followers.each { |follower| follower.follow!(user) }
end
42 changes: 31 additions & 11 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,41 @@
end

describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success

describe "when not signed in" do
it "should be successful" do
get 'home'
response.should be_success
end

it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "#{@base_title} | Home")
end

it "should have a non-blank body" do
get 'home'
response.body.should_not =~ /<body>\s*<\/body>/
end
end

it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "#{@base_title} | Home")
describe "when signed in" do
before(:each) do
@user = test_sign_in(Factory(:user))
other_user = Factory(:user, :email => Factory.next(:email))
other_user.follow!(@user)
end

it "should have the right follower/following counts" do
get :home
response.should have_selector('a', :href => following_user_path(@user),
:content => "0 following")
response.should have_selector('a', :href => followers_user_path(@user),
:content => "1 follower")
end
end

it "should have a non-blank body" do
get 'home'
response.body.should_not =~ /<body>\s*<\/body>/
end
end

describe "GET 'contact'" do
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@
response.should have_selector('td.sidebar',
:content => @user.microposts.count.to_s)
end

describe "when signed in as another user" do
it "should be successful" do
test_sign_in(Factory(:user, :email => Factory.next(:email)))
get :show, :id => @user
response.should be_success
end
end
end

describe "GET 'new'" do
Expand Down

0 comments on commit 9bc6628

Please sign in to comment.