Skip to content

Commit

Permalink
Prevent user from sending a friend request to themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
kwizl committed May 22, 2020
1 parent f12a7be commit 9172dfd
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
45 changes: 45 additions & 0 deletions :w
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

validates :name, presence: true, length: { maximum: 20 }

has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id"
has_many :posts
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy

def pending
self.status = 'p'
end

def friends
friends_array = friendships.map{ |friendship| friendship.friend if friendship.status == 'a' }
friends_array.compact
end

def confirm_friend(user)
friendship = inverse_friendships.find{ |friendship| friendship.user == user }
friendship.status = 'a'
friendship.save
end

def friend?(user)
friends.include?(user)
end

def pending_friends
friends_array = friendships.map{ |friendship| friendship.friend if !friendship.status = 'p' }
friends_array.compact
end

def reject_friend(user)
friendship = inverse_friendships.find{ |friendship| friendship.user == user }
friendship.status = 'r'
friendship.save
end
end
13 changes: 11 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ class UsersController < ApplicationController
before_action :authenticate_user!

def index
@users = User.all
@users = User.find_by_sql(["SELECT * FROM users WHERE id != ? ", current_user.id])
end

def show
@user = User.find(params[:id])
@posts = @user.posts.ordered_by_most_recent
end


def follow_user
user_id = params[:user_id]
if Friendship.create(user_id: current_user.id, friend_id: user_id)
flash[:success] = "Request Sent"
else
flash[:danger] = "Failure to send request"
end
redirect_to users_path
end
end
2 changes: 2 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Friendship < ApplicationRecord
belongs_to :user
belongs_to :friend, class_name: "User"

validates_uniqueness_of :user_id, scope: :friend_id
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class User < ApplicationRecord
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy

def pending
self.status = 'p'
end

def friends
friends_array = friendships.map{ |friendship| friendship.friend if friendship.status == 'a' }
friends_array.compact
Expand Down
4 changes: 4 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<%= link_to 'See Profile', user_path(user), class: 'profile-link' %>
</span>
<%= send_button(user.id) %>
<%= form_tag follow_user_path, method: :post do %>
<%= hidden_field_tag :user_id, user.id %>
<%= submit_tag "Request", class: 'btn btn-primary btn-sm' %>
<% end %>
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
resources :comments, only: [:create]
resources :likes, only: [:create, :destroy]
end

post "follow/user" => "users#follow_user", as: :follow_user
post 'send_invitation' => 'users#send_invitation'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

0 comments on commit 9172dfd

Please sign in to comment.