-
Notifications
You must be signed in to change notification settings - Fork 0
Chap14 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ngango0205
wants to merge
2
commits into
develop
Choose a base branch
from
chap14
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chap14 #15
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
@@ -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 | ||
|
|
@@ -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) | ||
|
|
@@ -60,14 +68,33 @@ def password_reset_expired? | |
| reset_sent_at < Settings.expired_time | ||
| end | ||
|
|
||
| def feed | ||
| following_ids = "SELECT followed_id FROM relationships | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %>"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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