Skip to content

Commit

Permalink
Revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienNeibaf committed Sep 14, 2019
1 parent 5e40293 commit 59d76dc
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 54 deletions.
3 changes: 1 addition & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

def home
@posts = current_user.posts + current_user.friend_posts
render 'users/home'
@posts = current_user.posts.latest.includes(:likes, :comments) + current_user.friend_posts
end

protected
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/friendships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

class FriendshipsController < ApplicationController
def index
if id = params[:u]
if (id = params[:u])
@user = User.find(id)
@users = @user.friends
elsif q = params[:s]
elsif (q = params[:s])
if q == 's'
@users = current_user.friend_requests[:sent]
render 'sent'
Expand Down
22 changes: 12 additions & 10 deletions app/helpers/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

module NotificationsHelper
def send_notification(receiver, action, subject)
actions = {
'like' => "liked your #{subject.subject.class.name.downcase!}",
'comment' => subject.subject.class.name == 'Post' ? 'commented on your post' : 'replied on your comment',
'request' => 'sent you a friend request',
'accept' => 'accepted your friend request'
}

return nil if receiver == current_user

content = "#{current_user.username} #{actions[action]}"
content = "#{current_user.username} " +
case action
when 'like'
"liked your #{subject.subject.class.name.downcase!}"
when 'comment'
subject.subject.class.name == 'Post' ? 'commented on your post' : 'replied on your comment'
when 'request'
'sent you a friend request'
when 'accept'
'accepted your friend request'
else ''
end
Notification.create(sender: current_user, receiver: receiver, content: content, subject: subject)
end
end
13 changes: 13 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ class Friendship < ApplicationRecord
enum status: %i[pending accepted]

validates :status, presence: true

def self.acceptors(user)
where(requestor_id: user.id).includes(:acceptor).map(&:acceptor)
end

def self.requestors(user)
where(acceptor_id: user.id).includes(:requestor).map(&:requestor)
end

def self.between(user1, user2)
where('requestor_id = ? AND acceptor_id = ?', user1.id, user2.id)
.or(where('requestor_id = ? AND acceptor_id = ?', user2.id, user1.id)).first
end
end
4 changes: 2 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Post < ApplicationRecord
has_many :likes, as: :subject, dependent: :destroy
has_many :comments, as: :subject, dependent: :destroy

scope :latest, -> { order(created_at: :desc) }

validates :user, :title, :content, presence: true

scope :latest, -> { order(created_at: :desc) }
end
35 changes: 11 additions & 24 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,32 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable
devise :omniauthable, omniauth_providers: %i[facebook]

has_many :comments

has_many :requested_friendships, class_name: 'Friendship',
foreign_key: 'requestor_id', dependent: :destroy
has_many :received_friendships, class_name: 'Friendship',
foreign_key: 'acceptor_id', dependent: :destroy

has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :notifications, dependent: :destroy, foreign_key: 'receiver_id'

validates :username, presence: true

def friends
Friendship.accepted.acceptors(self) + Friendship.accepted.requestors(self)
end

def friend_requests
{
sent: requested_friendships.pending.includes(:acceptor).map(&:acceptor),
received: received_friendships.pending.includes(:requestor).map(&:requestor)
sent: Friendship.pending.acceptors(self),
received: Friendship.pending.requestors(self)
}
end

def friends
requested_friendships.accepted.includes(:acceptor).map(&:acceptor) +
received_friendships.accepted.includes(:requestor).map(&:requestor)
end

def friend_posts
requested_friendships.accepted.includes(acceptor: [posts: %i[likes comments]])
.map(&:acceptor).map(&:posts).flatten +
received_friendships.accepted.includes(requestor: [posts: %i[likes comments]])
.map(&:requestor).map(&:posts).flatten
end

def friendship(user)
Friendship.where("requestor_id = #{id} AND acceptor_id = #{user.id} OR "\
"requestor_id = #{user.id} AND acceptor_id = #{id}").first
(Friendship.accepted.acceptors(self) + Friendship.accepted.requestors(self))
.map { |user| user.posts.latest.includes(:likes, :comments) }.flatten
end

def self.new_with_session(param, session)
super.tap do |user|
if data == session['devise.facebook_data'] && session['devise.facebook_data']['extra']['raw_info']
if data == session['devise.facebook_data'] &&
session['devise.facebook_data']['extra']['raw_info']
user['email'] = data['email'] if user.email.blank?
end
end
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
<%= f.label :gender %>
<div class="gender">
<span class="option">
<%= f.radio_button :gender, 'Homme' %>Homme
<%= f.radio_button :gender, 'Male' %>Male
</span>
<span class="option">
<%= f.radio_button :gender, 'Femme' %>Femme
<%= f.radio_button :gender, 'Female' %>Female
</span>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<%= f.label :gender %>
<div class="gender">
<span class="option">
<%= f.radio_button :gender, 'Homme' %>Homme
<%= f.radio_button :gender, 'Male' %>Male
</span>
<span class="option">
<%= f.radio_button :gender, 'Femme' %>Femme
<%= f.radio_button :gender, 'Female' %>Female
</span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/friendships/_friendship.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% if users.any? %>
<ul>
<% users.each do |user| %>
<% friendship = current_user.friendship(user) %>
<% friendship = Friendship.between(current_user, user) %>
<li>
<%= link_to user, class: 'photo' do %>
<%= image_tag(photo_path(user)) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/likes/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<%= link_to like.user, class: 'user-link' do %>
<%= like.user.name %>
<% end %>
<% if current_user != like.user && current_user.friendship(like.user).nil? %>
<% if current_user != like.user && Friendship.between(current_user, like.user).nil? %>
<%= link_to friendships_path(friendship: {requestor_id: current_user.id,
acceptor_id: like.user.id, status: 'pending'}),
method: :post do %>
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# frozen_string_literal: true

Rails.application.routes.draw do
root to: 'application#home'
root 'users#home'

resources :notifications, only: [:index]

resources :users, only: [:index, :show]

resources :likes, only: [:create, :update, :destroy]

resources :posts, only: [:show, :create, :edit, :update, :destroy] do
Expand All @@ -23,5 +21,7 @@

devise_for :users, controllers: {omniauth_callbacks: 'omniauth_callbacks'}

resources :users, only: [:index, :show]

get '*path', to: redirect('/')
end
2 changes: 1 addition & 1 deletion spec/features/comment_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
visit user_path(@user)

page.find("li[data-post-id='#{@post.id}'] > footer > .comment-form textarea")
.set('I comment by test through RSPEC\n')
.set('I comment by test through RSPEC').send_keys(:return)

expect(page).to have_content 'I comment by test through RSPEC'
end
Expand Down
5 changes: 1 addition & 4 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,4 @@
config.javascript_driver = :webkit
end

Capybara::Webkit.configure do |config|
config.allow_unknown_urls
config.timeout = 5
end
Capybara::Webkit.configure(&:allow_unknown_urls)

0 comments on commit 59d76dc

Please sign in to comment.