Commit fd19900b by Mai Hoang Thai Ha

Revert "Add user following"

This reverts commit 81b15553.
parent 253070d5
...@@ -153,44 +153,6 @@ aside { ...@@ -153,44 +153,6 @@ aside {
margin-top: 15px; margin-top: 15px;
} }
.stats {
overflow: auto;
margin-top: 0;
padding: 0;
a {
float: left;
padding: 0 10px;
border-left: 1px solid $gray-medium-light;
color: gray;
&:first-child {
padding-left: 0;
border: 0;
}
&:hover {
text-decoration: none;
color: black;
}
}
strong {
display: block;
}
}
.user_avatars {
overflow: auto;
margin-top: 10px;
.gravatar {
margin: 1px 1px;
}
a {
padding: 0;
}
}
.user.follow {
padding: 0;
}
// form // form
input, textarea, select, .uneditable_iput { input, textarea, select, .uneditable_iput {
border: 1px solid #bbb; border: 1px solid #bbb;
......
// Place all the styles related to the Relationships controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
@user = User.find(params[: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
class UsersController < ApplicationController class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy, before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
:following, :followers]
before_action :correct_user, only: [:edit, :update] before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy before_action :admin_user, only: :destroy
...@@ -48,20 +47,6 @@ class UsersController < ApplicationController ...@@ -48,20 +47,6 @@ class UsersController < ApplicationController
redirect_to users_url redirect_to users_url
end end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.following.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 private
def user_params def user_params
......
module RelationshipsHelper
end
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
class User < ApplicationRecord class User < ApplicationRecord
has_many :microposts, dependent: :destroy has_many :microposts, dependent: :destroy
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 attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email before_save :downcase_email
before_create :create_activation_digest before_create :create_activation_digest
...@@ -80,22 +72,7 @@ class User < ApplicationRecord ...@@ -80,22 +72,7 @@ class User < ApplicationRecord
# Defines a proto-feed # Defines a proto-feed
# See "Following users" for the full implementtation # See "Following users" for the full implementtation
def feed def feed
following_ids = "SELECT followed_id FROM relationships Micropost.where("user_id = ?", id)
WHERE follower_id = :user_id"
Micropost.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id)
end
def follow(other_user)
following << other_user
end
def unfollow(other_user)
following.delete(other_user)
end
def following?(other_user)
following.include?(other_user)
end end
private private
......
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= @user.followers.count %>');
\ No newline at end of file
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>");
$("#followers").html('<%= @user.followers.count %>');
\ No newline at end of file
<% @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>
\ No newline at end of file
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
<section class="user_info"> <section class="user_info">
<%= render 'shared/user_info' %> <%= render 'shared/user_info' %>
</section> </section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="micropost_form"> <section class="micropost_form">
<%= render 'shared/micropost_form' %> <%= render 'shared/micropost_form' %>
</section> </section>
......
<%= form_with(model: current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
\ No newline at end of file
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
\ No newline at end of file
<%= form_with(model: current_user.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
\ No newline at end of file
<li> <li>
<%= gravatar_for user, size: 50 %> <%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %> <%= link_to user.name, user %>
<%# <% if current_user.admin? && !current_user?(user) %> <% if current_user.admin? && !current_user?(user) %>
<% if current_user.try(:admin?) %>
| <%= link_to "delete", user, method: :delete, | <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %> data: { confirm: "You sure?" } %>
<% end %> <% end %>
......
...@@ -8,12 +8,8 @@ ...@@ -8,12 +8,8 @@
<%= @user.name %> <%= @user.name %>
</h1> </h1>
</section> </section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
</aside> </aside>
<div class="col-md-8"> <div class="col-md-8">
<%= render 'follow_form' if logged_in? %>
<% if @user.microposts.any? %> <% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3> <h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts"> <ol class="microposts">
......
<% provide(:title, @title) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= 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 class="stats">
<%= 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="col-md-8">
<h3><%= @title %></h3>
<% if @users.any? %>
<ul class="users follow">
<%= render @users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
...@@ -18,7 +18,5 @@ module SampleApp ...@@ -18,7 +18,5 @@ module SampleApp
# #
# config.time_zone = "Central Time (US & Canada)" # config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras") # config.eager_load_paths << Rails.root.join("extras")
# Include the authenticity token in remote forms.
config.action_view.embed_authenticity_token_in_remote_forms = true
end end
end end
...@@ -7,13 +7,8 @@ Rails.application.routes.draw do ...@@ -7,13 +7,8 @@ Rails.application.routes.draw do
get '/login', to: 'sessions#new' get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create' post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy' delete '/logout', to: 'sessions#destroy'
resources :users do resources :users
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit] resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update] resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy] resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
end end
class CreateRelationships < ActiveRecord::Migration[6.1]
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
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_06_22_074033) do ActiveRecord::Schema.define(version: 2021_06_22_061651) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
...@@ -49,16 +49,6 @@ ActiveRecord::Schema.define(version: 2021_06_22_074033) do ...@@ -49,16 +49,6 @@ ActiveRecord::Schema.define(version: 2021_06_22_074033) do
t.index ["user_id"], name: "index_microposts_on_user_id" t.index ["user_id"], name: "index_microposts_on_user_id"
end end
create_table "relationships", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["followed_id"], name: "index_relationships_on_followed_id"
t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
t.index ["follower_id"], name: "index_relationships_on_follower_id"
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
t.string "email" t.string "email"
......
...@@ -24,11 +24,3 @@ users = User.order(:created_at).take(6) ...@@ -24,11 +24,3 @@ users = User.order(:created_at).take(6)
content = Faker::Lorem.sentence(word_count: 5) content = Faker::Lorem.sentence(word_count: 5)
users.each { |user| user.microposts.create!(content: content) } users.each { |user| user.microposts.create!(content: content) }
end end
# Create 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) }
\ No newline at end of file
require "test_helper"
class RelationshipsControllerTest < ActionDispatch::IntegrationTest
test "create should require logged-in user" do
assert_no_difference 'Relationship.count' do
post relationships_path
end
assert_redirected_to login_url
end
test "destroy should require logged-in user" do
assert_no_difference 'Relationship.count' do
delete relationship_path(relationships(:one))
end
assert_redirected_to login_url
end
end
...@@ -58,13 +58,4 @@ class UsersControllerTest < ActionDispatch::IntegrationTest ...@@ -58,13 +58,4 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert flash.empty? assert flash.empty?
assert_redirected_to root_url assert_redirected_to root_url
end end
test "should redirect following when not logged in" do
get following_user_path(@user)
assert_redirected_to login_url
end
test "should redirect followers when not logged in" do
get followers_user_path(@user)
assert_redirected_to login_url
end
end end
\ No newline at end of file
one:
follower: michael
followed: lana
two:
follower: michael
followed: malory
three:
follower: lana
followed: michael
four:
follower: archer
followed: michael
\ No newline at end of file
require "test_helper"
class FollowingTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other = users(:archer)
log_in_as(@user)
end
test "following page" do
get following_user_path(@user)
assert_not @user.following.empty?
assert_match @user.following.count.to_s, response.body
@user.following.each do |user|
assert_select "a[href=?]", user_path(user)
end
end
test "followers page" do
get followers_user_path(@user)
assert_not @user.followers.empty?
assert_match @user.followers.count.to_s, response.body
@user.followers.each do |user|
assert_select "a[href=?]", user_path(user)
end
end
test "should follow a user the standard way" do
assert_difference '@user.following.count', 1 do
post relationships_path, params: { followed_id: @other.id }
end
end
test "should follow a user with Ajax" do
assert_difference '@user.following.count', 1 do
post relationships_path, xhr: true, params: { followed_id: @other.id }
end
end
test "should unfollow a user the standard way" do
@user.follow(@other)
relationship = @user.active_relationships.find_by(followed_id: @other.id)
assert_difference '@user.following.count', -1 do
delete relationship_path(relationship)
end
end
test "should unfollow a user with Ajax" do
@user.follow(@other)
relationship = @user.active_relationships.find_by(followed_id: @other.id)
assert_difference '@user.following.count', -1 do
delete relationship_path(relationship), xhr: true
end
end
end
require "test_helper"
class RelationshipTest < ActiveSupport::TestCase
def setup
@relationship = Relationship.new(follower_id: users(:michael).id,
followed_id: users(:archer).id)
end
test "should be valid" do
assert @relationship.valid?
end
test "should require a follower_id" do
@relationship.follower_id = nil
assert_not @relationship.valid?
end
test "should require a followed_id" do
@relationship.followed_id = nil
assert_not @relationship.valid?
end
end
...@@ -66,33 +66,4 @@ class UserTest < ActiveSupport::TestCase ...@@ -66,33 +66,4 @@ class UserTest < ActiveSupport::TestCase
@user.destroy @user.destroy
end end
end end
test "should follow and unfollow a user" do
michael = users(:michael)
archer = users(:archer)
assert_not michael.following?(archer)
michael.follow(archer)
assert michael.following?(archer)
assert archer.followers.include?(michael)
michael.unfollow(archer)
assert_not michael.following?(archer)
end
test "feed should have the right posts" do
michael = users(:michael)
archer = users(:archer)
lana = users(:lana)
# Posts from followed user
lana.microposts.each do |post_following|
assert michael.feed.include?(post_following)
end
# Posts from self
michael.microposts.each do |post_self|
assert michael.feed.include?(post_self)
end
# Posts from unfollowed user
archer.microposts.each do |post_unfollowed|
assert_not michael.feed.include?(post_unfollowed)
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment