Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/relationships.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://coffeescript.org/
39 changes: 39 additions & 0 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,45 @@ aside {
.gravatar_edit {
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;
}
a {
padding: 0;
}
}

.users{
.follow {
padding: 0;
}
}

input, textarea, select, .uneditable-input {
border: 1px solid $colour6;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/relationships.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/
23 changes: 23 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class RelationshipsController < ApplicationController
before_action :logged_in_user

def create
user = User.find_by id: params[:followed_id]
current_user.follow user
respond_to do |format|
format.html { redirect_to @user }
format.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái này để làm gì thế em :v

end
redirect_to user
end

def destroy
user = Relationship.find_by(id: params[:id]).followed
current_user.unfollow user
respond_to do |format|
format.html { redirect_to @user }
format.js
end
redirect_to user
end
end
2 changes: 1 addition & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class StaticPagesController < ApplicationController
def home
return unless logged_in?
@micropost = current_user.microposts.build
@feed_items = current_user.microposts.micropost_desc.page(params[:page]).per Settings.page.per_page
@feed_items = current_user.feed.page(params[:page]).per Settings.page.per_page
end

def help
Expand Down
23 changes: 16 additions & 7 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class UsersController < ApplicationController
before_action :find_user, only: [:show, :edit, :update]
before_action :find_user, only: [:show, :edit, :update, :following]
before_action :logged_in_user, only: [:index, :edit, :update, :show, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy

def index
@users = User.where(activated: true).page(params[:page])
.per(Settings.page.per_page)
.per Settings.page.per_page
end

def new
Expand Down Expand Up @@ -55,6 +55,16 @@ def destroy
end
end

def following
@users = @user.following.page(params[:page])
render :show_follow
end

def followers
@users = @user.followers.page(params[:page])
render :show_follow
end

private

def user_params
Expand All @@ -69,10 +79,10 @@ def find_user
end

def logged_in_user
unless logged_in?
flash[:danger] = t "title2"
redirect_to login_url
end
unless logged_in?
flash[:danger] = t "title2"
redirect_to login_url
end
end

def correct_user
Expand All @@ -84,4 +94,3 @@ def admin_user
redirect_to root_url unless current_user.admin?
end
end

4 changes: 4 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Relationship < ApplicationRecord
belongs_to :follower, class_name: User.name
belongs_to :followed, class_name: User.name
end
53 changes: 40 additions & 13 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
class User < ApplicationRecord
has_many :microposts, 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
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: Settings.maximum.length_name }
validates :name, presence: true, length: {maximum: Settings.maximum.length_name}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: Settings.maximum.length_email },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :email, presence: true, length: {maximum: Settings.maximum.length_email},
format: {with: VALID_EMAIL_REGEX},
uniqueness: {case_sensitive: false}
has_secure_password
validates :password, presence: true, length: { minimum: Settings.minimum.length_pass }
validates :password, presence: true, length: {minimum: Settings.minimum.length_pass}

class << self

def digest string
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end

def new_token
Expand All @@ -31,7 +39,7 @@ def remember
def authenticated? attribute, token
digest = send "#{attribute}_digest"
return false if digest.nil?
BCrypt::Password.new(digest).is_password? token
BCrypt::Password.new(digest).is_password? token
end

def forget
Expand All @@ -46,7 +54,7 @@ def send_activation_email
UserMailer.account_activation(self).deliver_now
end

def create_reset_digest
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
Expand All @@ -60,14 +68,33 @@ def password_reset_expired?
reset_sent_at < Settings.expired_time
end

def feed
following_ids = "SELECT followed_id FROM relationships

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

có thể đổi sang query của active record ko em

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

private

def downcase_email
email.downcase!
end

def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest activation_token
end
self.activation_token = User.new_token
self.activation_digest = User.digest activation_token
end
end
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("<%= j 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("<%= j render "users/unfollow" %>");
$("#followers").html("<%= @user.followers.count %>");
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>
3 changes: 3 additions & 0 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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
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| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% 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 %>
5 changes: 5 additions & 0 deletions app/views/users/_unfollow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= form_for current_user.active_relationships.find_by(followed_id: @user.id),
html: {method: :delete},
remote: true 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><%= t(".microposts") %><%= @user.microposts.count %></h3>
<ol class="microposts" >
Expand Down
30 changes: 30 additions & 0 deletions app/views/users/show_follow.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<% provide :title, t(".follow") %>
<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>
<%= paginate @users, theme: "twitter-bootstrap-3" %>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Application < Rails::Application
config.load_defaults 5.1
config.i18n.load_path += Dir[Rails.root.join("my", "locales", "*.{rb,yml}").to_s]
config.i18n.available_locales = [:en, :vi, :jp]
config.action_view.embed_authenticity_token_in_remote_forms = true

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
Expand Down
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ en:
title: "About"
title1: "Contact"
title2: "News"

show_follow:
follow: "Following"
follower: "Follower"
sessions:
new:
title: "Log In"
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
resources :account_activations, only: :edit
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
end
13 changes: 13 additions & 0 deletions db/migrate/20180718013926_create_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateRelationships < ActiveRecord::Migration[5.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
Loading