Skip to content

Commit

Permalink
Follow and Unfollow buttons added as well as stats on how many follow…
Browse files Browse the repository at this point in the history
…ers and following a user has.
  • Loading branch information
JessSumner committed Sep 5, 2015
1 parent 2f8e925 commit e7e03ac
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 2 deletions.
38 changes: 38 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,44 @@ aside {
margin-top: 15px;
}

.stats {
overflow: auto;
margin-top: 0;
padding: 0;
a {
float: left;
padding: 0 10px;
border-left: 1px solid $gray-lighter;
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;
}
a {
padding: 0;
}
}

.users.follow {
padding: 0;
}

/* forms */

input, textarea, select, .uneditable-input {
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class User < ActiveRecord::Base
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_homepage.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="micropost_form">
<%= render 'shared/micropost_form' %>
</section>
Expand Down
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.following.count %>
</strong>
following
</a>
<a href="<%= followers_user_path(@user) %>">
<strong id="followers" class="stat">
<%= @user.followers.count %>
</strong>
followers
</a>
</div>
4 changes: 4 additions & 0 deletions app/views/users/_follow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/users/_follow_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% 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.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% 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 class="stats">
<%= render 'shared/stats' %>
</section>
</aside>
<div class="col-md-8">
<%= render 'follow_form' if logged_in? %>
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
end
10 changes: 9 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
end

# Following relationships
users = User.all
user = users.first
following = users[2..50]
followers = users[3..40]
following.each { |followed| user.follow(followed) }
followers.each { |follower| follower.follow(user) }
1 change: 1 addition & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def setup
assert_not testuser.following?(archer)
testuser.follow(archer)
assert testuser.following?(archer)
assert archer.followers.include?(testuser)
testuser.unfollow(archer)
assert_not testuser.following?(archer)
end
Expand Down

0 comments on commit e7e03ac

Please sign in to comment.