Skip to content

Commit

Permalink
Merge pull request #22 from dinhthai2206/follow-user
Browse files Browse the repository at this point in the history
Add follow-user
  • Loading branch information
chintk authored Nov 9, 2018
2 parents 47015e4 + 39c4cc4 commit 8d52917
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/controllers/followers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class FollowersController < ApplicationController
def index
@title = t ".followers"
@user = User.friendly.find params[:user_id]
@pagy, @users = pagy @user.followers, items: 10
render "users/show_follow"
end
end
8 changes: 8 additions & 0 deletions app/controllers/following_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class FollowingController < ApplicationController
def index
@title = t ".following"
@user = User.friendly.find params[:user_id]
@pagy, @users = pagy @user.following, items: 10
render "users/show_follow"
end
end
34 changes: 34 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class RelationshipsController < ApplicationController
before_action :find_user, only: %w(create destroy)

def create
current_user.follow @user
respond_to do |format|
format.html{redirect_to @user}
format.js
end
end

def destroy
current_user.unfollow @user
respond_to do |format|
format.html{redirect_to @user}
format.js
end
end

private

def find_user
@user = if params[:action] == "create"
User.friendly.find params[:followed_id]
elsif params[:action] == "destroy"
Relationship.find_by(id: params[:id])&.followed
end

return if @user

flash[:info] = t ".info"
redirect_to root_url
end
end
6 changes: 6 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Relationship < ApplicationRecord
belongs_to :follower, class_name: User.name
belongs_to :followed, class_name: User.name
validates :follower_id, presence: true
validates :followed_id, presence: true
end
18 changes: 18 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class User < ApplicationRecord

has_many :speeches
has_many :services, dependent: :destroy
has_many :active_relationships, class_name: Relationship.name,
foreign_key: "follower_id", dependent: :destroy
has_many :passive_relationships, class_name: Relationship.name,
foreign_key: "followed_id", dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
mount_uploader :avatar, AvatarUploader
validates :slug, presence: true

Expand All @@ -18,4 +24,16 @@ class User < ApplicationRecord
def should_generate_new_friendly_id?
name_changed? || super
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
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
</li>
<%= form_with url: search_path, local: true, method: :get, class: "form-inline" do |form| %>
<div class="form-group">
<%= form.text_field :q, placeholder: "Search", data: {behavior: "autocomplete"}, class: "form-control" %>
<%= form.text_field :q, placeholder: t(".search"), data: {behavior: "autocomplete"}, class: "form-control" %>
</div>
<%= form.button "Search", class: "btn btn-outline-success my-2 my-sm-0" %>
<%= form.button t(".search"), class: "btn btn-outline-success my-2 my-sm-0" %>
<% end %>
<% else %>
<li class="dropdown nav-item">
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 %>');
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, remote: true do |f| %>
<%= hidden_field_tag :followed_id, @user.id %>
<%= f.submit t(".follow"), class: "btn follow" %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/users/_follow_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% unless current_user == user %>
<% if current_user.following? user %>
<%= render "users/unfollow" %>
<% else %>
<%= render "users/follow" %>
<% end %>
<% 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}, remote: true do |f| %>
<%= f.submit t(".unfollow"), class: "btn follow" %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<li id="user-<%= user.id %>">
<div>
<%= link_to user.name, user %>
</div>
</li>
9 changes: 7 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<button class="btn btn-fab btn-primary btn-round" rel="tooltip" title="Follow this user">
<i class="material-icons">add</i>
</button>
<span id="follow_form">
<%= render "follow_form", user: @user %>
</span>
</div>
</div>
</div>
Expand Down Expand Up @@ -65,8 +68,10 @@
<ul class="list-unstyled">
<li>
<b><%= t ".x" %></b> <%= t ".speeches" %></li>
<li>
<b><%= t ".x" %></b> <%= t ".followers" %></li>
<li><a href="<%= user_followers_path @user %>">
<b id="followers"><%= @user.followers.count %></b> <%= t ".followers" %></a></li>
<li><a href="<%= user_following_index_path @user %>">
<b id="following"><%= @user.following.count %></b> <%= t ".following" %></a></li>
<li>
<b><%= t ".x" %></b> <%= t ".likes" %></li>
</ul>
Expand Down
10 changes: 10 additions & 0 deletions app/views/users/show_follow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% provide :title, @title %>

<h1><%= @title %></h1>
<% if @users.present? %>
<ul class="users follow">
<%= render @users %>
</ul>
<%== pagy_nav_bootstrap @pagy %>
<% end %>
<%= link_to t(".back"), user_path(@user) %>
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ class Application < Rails::Application
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
config.action_view.embed_authenticity_token_in_remote_forms = true
end
end
12 changes: 11 additions & 1 deletion config/locales/controller_en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
users:
find_user:
show:
info: That user doesn't exist!

speeches:
Expand All @@ -10,3 +10,13 @@ en:
success: Speech updated!
destroy:
success: Delete success!

following:
following: Following

followers:
followers: Followers

relationship:
create:
info: That user doesn't exist!
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ en:
account: Account
profile: Profile
sign_out: Sign Out
search: Search
sign_in: Sign In
sign_up: Sign Up

Expand All @@ -60,6 +61,12 @@ en:
x: 69
trainee: Trainee
user_name: Bùi Mạnh Cường
follow:
follow: follow
unfollow:
unfollow: unfollow
show_follow:
back: Back to user

themes:
index:
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

root "static_pages#home"
devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"}
resources :users, only: :show
resources :users, only: :show do
resources :following, only: :index
resources :followers, only: :index
end
resources :themes, only: :index
resources :speeches, except: :index
resources :relationships, only: [:create, :destroy]
get :search, controller: :main
end
13 changes: 13 additions & 0 deletions db/migrate/20181106142346_create_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateRelationships < ActiveRecord::Migration[5.2]
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
9 changes: 8 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
location_id: rand(Location.first.id..Location.last.id))
end

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)}

12.times do
title = Faker::Book.genre
title = Faker::Book.genre
Theme.create! title: title
end

Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/followers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe FollowersController, type: :controller do

end
5 changes: 5 additions & 0 deletions spec/controllers/following_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe FollowingController, type: :controller do

end
5 changes: 5 additions & 0 deletions spec/controllers/relationships_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe RelationshipsController, type: :controller do

end
6 changes: 6 additions & 0 deletions spec/factories/relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :relationship do
follower_id { 1 }
followed_id { 1 }
end
end
5 changes: 5 additions & 0 deletions spec/models/relationship_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Relationship, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 8d52917

Please sign in to comment.