Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
torresed committed Feb 18, 2021
1 parent 35388e9 commit bcca1f4
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 46 deletions.
12 changes: 1 addition & 11 deletions app/controllers/friends_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def show

def create
@friendship = Friends.request(current_user, @friend)
if @friendship.nil? || @friendship.blocked?
if @friendship.nil?
render status: :bad_request, json: { error: 'Friend request could not be sent.' }
else
render status: :ok, json: { success: 'Friend request has been sent.' }
Expand All @@ -37,16 +37,6 @@ def destroy
end
end

def block
@friendship = Friends.find_by(sent_by: @friend, sent_to: current_user).presence || Friends.find_by(sent_by: current_user, sent_to: @friend).presence
if @friendship.nil?
render status: :bad_request, json: { error: 'User could not be blocked.' }
else
@friendship.blocked!
render status: :ok, json: { success: 'User has been blocked.' }
end
end

private

def users
Expand Down
53 changes: 53 additions & 0 deletions app/controllers/parties_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

class PartiesController < ApplicationController
before_action :check_token, :users
skip_before_action :users, only: %i[show]

def show
@user = current_user
end

def create
@member = Friends.find_by(sent_by: current_user, sent_to: @friend, status: :confirmed)
if @member.nil? || @member.invited? || current_user.party_members.count >= 2
render status: :bad_request, json: { error: 'Friend could not be invited.' }
else
@member.invited!
render status: :ok, json: { success: 'Friend has been invited.' }
end
end

def update
@member = Friends.find_by(sent_by: @friend, sent_to: current_user, status: :confirmed)
if @member.nil? || !@member.invited? || @member.party_members.count >= 2
render status: :bad_request, json: { error: 'Invitation could not be accepted.' }
else
@member.accepted!
render status: :ok, json: { success: 'Invitation accepted.' }
end
end

def kick
@member = Friends.find_by(sent_by: current_user, sent_to: @friend, status: :confirmed)
if @member.nil? || !@member.accepted?
render status: :bad_request, json: { error: 'Friend could not be kicked.' }
else
@member.unsolicited!
render status: :ok, json: { success: 'Friend has been kicked.' }
end
end

def destroy
current_user.party_members.destroy
current_user.invitations.destroy
render status: :ok, json: { success: 'Party has been disbanded.' }
end

private

def members
@user = current_user
head :bad_request unless (@friend = User.find_by(id: params[:id]))
end
end
2 changes: 1 addition & 1 deletion app/models/friends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class Friends < ApplicationRecord
def self.request(user, friend)
return if (user == friend) || find_by(sent_by: user, sent_to: friend).present?

user.friends_sent.create(sent_to: friend, status: :pending)
user.requests_sent.create(sent_to: friend, status: :pending)
end
end
24 changes: 14 additions & 10 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ class User < ApplicationRecord
validates :email_confirmation_token, uniqueness: true, allow_nil: true
validates :password_recovery_token, uniqueness: true, allow_nil: true

has_many :friends_sent, class_name: 'Friends', foreign_key: 'sent_by_id', inverse_of: 'sent_by', dependent: :destroy
has_many :friends_received, class_name: 'Friends', foreign_key: 'sent_to_id', inverse_of: 'sent_to', dependent: :destroy
has_many :friended, -> { merge(Friends.confirmed) }, through: :friends_sent, source: :sent_to
has_many :friendships, -> { merge(Friends.confirmed) }, through: :friends_received, source: :sent_by
has_many :pending_requests, -> { merge(Friends.pending) }, through: :friends_sent, source: :sent_to
has_many :received_requests, -> { merge(Friends.pending) }, through: :friends_received, source: :sent_by
# Request associations.
has_many :requests_sent, class_name: 'Friends', foreign_key: 'sent_by_id', inverse_of: 'sent_by', dependent: :destroy
has_many :requests_received, class_name: 'Friends', foreign_key: 'sent_to_id', inverse_of: 'sent_to', dependent: :destroy

has_many :pending_invitations, -> { merge(Friends.invited) }, through: :friends_sent, source: :sent_to
has_many :received_invitations, -> { merge(Friends.invited) }, through: :friends_received, source: :sent_by
has_many :party_members, -> { merge(Friends.accepted) }, through: :friends_sent, source: :sent_to
# Friendship associations.
has_many :friended, -> { merge(Friends.confirmed) }, through: :requests_sent, source: :sent_to
has_many :friended_by, -> { merge(Friends.confirmed) }, through: :requests_received, source: :sent_by
has_many :friends_pending, -> { merge(Friends.pending) }, through: :requests_sent, source: :sent_to
has_many :friends_awaiting, -> { merge(Friends.pending) }, through: :requests_received, source: :sent_by

# Party associations.
has_many :invitations_pending, -> { merge(Friends.invited) }, through: :requests_sent, source: :sent_to
has_many :invitations_received, -> { merge(Friends.invited) }, through: :requests_received, source: :sent_by
has_many :party_members, -> { merge(Friends.accepted) }, through: :requests_sent, source: :sent_to

def friends
friended + friendships
friended + friended_by
end

private
Expand Down
20 changes: 4 additions & 16 deletions app/views/friends/show.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@ json.friends do
json.array! @user.friends, :username
end

json.pending_requests do
json.array! @user.pending_requests, :username
json.friends_pending do
json.array! @user.friends_pending, :username
end

json.received_requests do
json.array! @user.received_requests, :username
end

json.pending_invitations do
json.array! @user.pending_invitations, :username
end

json.received_invitations do
json.array! @user.received_invitations, :username
end

json.party do
json.array! @user.party_members, :username
json.friends_awaiting do
json.array! @user.friends_awaiting, :username
end
16 changes: 16 additions & 0 deletions app/views/parties/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

json.id @user.id.to_s
json.extract! @user, :username

json.invitations_pending do
json.array! @user.invitations_pending, :username
end

json.invitations_received do
json.array! @user.invitations_received, :username
end

json.party do
json.array! @user.party_members, :username
end
7 changes: 2 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
post '/password/reset/:password_recovery_token', to: 'users#password_update', as: 'password_update'

resource :friends, only: %i[show create update destroy]
post '/friends/block', to: 'friends#block'

post '/friends/invite', to: 'friends#invite'
post '/friends/accept_invitation', to: 'friends#accept_invitation'
post '/friends/kick', to: 'friends#kick'
delete '/friends/disband', to: 'friends#disband'
resource :parties, only: %i[show create update destroy]
post '/parties/kick', to: 'parties#kick'
end
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@

it 'sends a friend request' do
described_class.request(requester, requestee)
expect(requester.pending_requests).to include(requestee)
expect(requester.friends_pending).to include(requestee)
end
end
4 changes: 2 additions & 2 deletions spec/requests/friends_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
it 'sends a friend request' do
post friends_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend request has been sent.'))
expect(user.pending_requests).to include(friend)
expect(user.friends_pending).to include(friend)
end
end

Expand Down Expand Up @@ -102,7 +102,7 @@
Friends.request(user, friend)
delete friends_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend has been removed.'))
expect(user.pending_requests).not_to include(friend)
expect(user.friends_pending).not_to include(friend)
end

it 'removes a friends' do
Expand Down

0 comments on commit bcca1f4

Please sign in to comment.