Skip to content

Commit

Permalink
finish tests for follow and unfollow
Browse files Browse the repository at this point in the history
  • Loading branch information
lalithr95 committed Jul 28, 2016
1 parent a55ee0c commit 4ab7cbd
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/relationships.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/relationships.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Relationships controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
13 changes: 13 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RelationshipsController < ApplicationController
def create
user = User.find(params[:followed_id])
current_user.follow(user)
redirect_to "/profile/#{user.username}"
end

def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
redirect_to "/profile/#{user.username}"
end
end
14 changes: 14 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ def privacy

end

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

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

def map
# @title = "Maps"
# valid_tags = ["skill", "role", "gear", "tool"]
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/relationships_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RelationshipsHelper
end
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, @profile_user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% 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(@profile_user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
18 changes: 15 additions & 3 deletions app/views/users/profile.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
<% end %>

<!--<a href="#" class="btn btn-default btn-block"><i class="fa fa-envelope"></i> Send message</a>-->
<a href="#" class="btn btn-default btn-block disabled"><i class="fa fa-eye"></i> Follow <%= @user.name %></a>
<% if current_user %>
<hr />
<% unless current_user == @profile_user %>
<div id="follow_form" class="col-md-offset-4">
<% if current_user.following?(@profile_user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
<% end %>
<% if @user.user.nil? && current_user && current_user.role == "admin" %>
<a href="/admin/migrate/<%= @user.uid %>" class="btn btn-block"><i class="fa fa-bolt"></i> Migrate to new site</a>
<% end %>
Expand Down Expand Up @@ -83,10 +95,10 @@

<div class="row">
<div class="col-md-3 col-md-offset-2">
<h5><b><%= @profile_user.followers.count %></b></h5>
<h5><b><%= link_to @profile_user.followers.count, followers_path(id: @profile_user.username) %></b></h5>
</div>
<div class="col-md-3 col-md-offset-2">
<h5><b><%= @profile_user.following_users.count %></b></h5>
<h5><b><%= link_to @profile_user.following_users.count, following_path(id: @profile_user.username) %></b></h5>
</div>
</div>
<hr />
Expand Down
14 changes: 14 additions & 0 deletions app/views/users/show_follow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="container">
<% @users.each do |user| %>
<div class="col-md-3">
<div style="text-align:center;">
<% if user.photo_file_name %>
<img class="img-circle" id="profile-photo" style="width:50%;margin-bottom:20px;" src="<%= user.photo_path(:thumb) %>" />
<% else %>
<img class="img-circle" id="profile-photo" style="width:50%;margin-bottom:20px;" src="https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(user.email}">
<% end %>
<h3><%= user.username %></h3>
</div>
</div>
<% end %>
</div>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
get 'people' => 'users#list'
get 'users/role/:id' => 'users#list'
match 'users/update' => 'users#update'
match 'users/:id/following' => 'users#following', as: :following
match 'users/:id/followers' => 'users#followers', as: :followers
match 'signup' => 'users#new'
match 'home' => 'home#front'
resources :relationships, only: [:create, :destroy]

#resources :users

Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb.example
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 => 20160720235923) do
ActiveRecord::Schema.define(:version => 20160722022014) do

create_table "answer_selections", :force => true do |t|
t.integer "user_id"
Expand Down
16 changes: 12 additions & 4 deletions test/fixtures/relationships.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
follower_id: 1
followed_id: 4
follower: jeff
followed: bob

two:
follower_id: 1
followed_id: 3
follower: bob
followed: jeff

three:
follower: jeff
followed: admin

four:
follower: jeff
followed: moderator
34 changes: 34 additions & 0 deletions test/functional/relationships_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'

class RelationshipsControllerTest < ActionController::TestCase

def setup
activate_authlogic
end

test "create will follow other user with followed_id param" do
user = rusers(:admin)
followed_user = rusers(:jeff)
UserSession.create(user)
assert_difference 'Relationship.count', 1 do
post :create, followed_id: followed_user.id
end
assert_response :redirect
assert_redirected_to '/profile/' + followed_user.username
end

test "destroy will remove follow relationship" do
user = rusers(:jeff)
UserSession.create(user)
followed_user = rusers(:bob)
post :create, followed_id: followed_user.id


assert_difference 'Relationship.count', -1 do
delete :destroy, id: Relationship.last.id
end

assert_response :redirect
assert_redirected_to '/profile/' + followed_user.username
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/relationships_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class RelationshipsHelperTest < ActionView::TestCase
end

0 comments on commit 4ab7cbd

Please sign in to comment.