Skip to content
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

Confirmable #1

Open
wants to merge 17 commits into
base: friends
Choose a base branch
from
Prev Previous commit
Next Next commit
friend request feature added
  • Loading branch information
shubham committed Mar 14, 2019
commit 1db54a2234b59f4c05e9eba5b073be56e3bbab67
10 changes: 9 additions & 1 deletion app/controllers/friendships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class FriendshipsController < ApplicationController

def create
@friendship = current_user.friendships.build(friend_id: params[:friend_id])
flash[:notice] = 'Unable to add friend' unless @friendship.save
@friendship.status = 0
flash[:notice] = 'Unable to send request' unless @friendship.save
redirect_to root_url
end

Expand All @@ -12,4 +13,11 @@ def destroy
@friendship.destroy
redirect_to root_url
end

def update
by_user = User.find_by_id(params[:friend_id])
@friendship = by_user.friendships.find_by_id(params[:id])
flash[:notice] = 'Unable to add friend' unless @friendship.update_attribute(:status, 1)
redirect_to root_url
end
end
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def about

def friends
friends = @user.friends
@friends = friends + @user.inverse_friends
@friends = (friends + @user.inverse_friends).uniq
end

def photos
Expand Down
11 changes: 11 additions & 0 deletions app/views/home/_friendship.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% if (current_user.find_friendship(user) && (current_user.find_friendship(user).status == 1)) %>
<%= link_to ' Friend', friendship_path(id: current_user.find_friendship(user).id), method: :delete, class: "fa fa-check" %>
<% elsif (user.find_friendship(current_user) && (user.find_friendship(current_user).status == 1)) %>
<%= link_to ' Friend', friendship_path(id: user.find_friendship(current_user).id), method: :delete, class: "fa fa-check" %>
<% elsif current_user.find_friendship(user) %>
<%= link_to 'Requested', friendship_path(id: current_user.find_friendship(user).id), method: :delete %>
<% elsif user.find_friendship(current_user) %>
<%= link_to 'Confirm Request', friendship_path(friend_id: user.id, id: user.find_friendship(current_user).id), method: :patch %>
<% else %>
<%= link_to 'Add Friend', friendships_path(friend_id: user), method: :post %>
<% end %>
68 changes: 42 additions & 26 deletions app/views/home/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
<div class = "card" style = "width: 30rem;">
<div class = "card-body">
<p>
<%= link_to 'Delete', post_path(post), method: :delete %>
<%= post.timestamp %><br>
<%= post.content %><br>
<% if post.image.attached? %>
<%= image_tag post.image, style: "width:100%;" %>
<% end %>

<hr>
<span id = <%= "likes#{post.id}" %> >
<%= render(partial: 'home/like', locals: {post: post}) %>
<div class = "card-body">
<p>
<%= link_to "#{post.user.first_name} #{post.user.last_name}" %><br>
<%= time_ago_in_words(post.created_at) %><br>
<% if current_user==post.user %>
<span style="float:right">
<%= link_to 'Delete', post_path(post), method: :delete %>
<button type="button" class="pmenu btn btn-default dropdown-toggle"></button>
<ul class="dropdown dropdown-menu">
<li><%= link_to "Edit", "#" %></li>
<li><%= link_to "Delete", "#" %></li>
</ul>
</span>
<span><%= link_to ' Comments', post_comments_path(post), remote: true, class: "fa fa-comment btn" %></span>
<hr>

<div id= <%= "comments#{post.id}" %> ></div>

<div id= "add_comment" >
<%= form_with(model: [ post, post.comments.build ], remote: true ) do |f| %>
<%= f.text_field :content, placeholder: "Add a comment.." %>
<%= f.submit("Submit") %>
<% end %>
</div>
</p>
</div>
</div>
<% end %>
<%= post.content %><br>
<% if post.image.attached? %>
<%= image_tag post.image, style: "width:100%;" %>
<% end %>

<hr>
<span id = <%= "likes#{post.id}" %> >
<%= render(partial: 'home/like', locals: {post: post}) %>
</span>
<span><%= link_to ' Comments', post_comments_path(post), remote: true, class: "fa fa-comment btn" %></span>
<hr>

<div id= <%= "comments#{post.id}" %> ></div>

<div id= "add_comment" >
<%= form_with(model: [ post, post.comments.build ], remote: true ) do |f| %>
<%= f.text_field :content, placeholder: "Add a comment.." %>
<%= f.submit("Submit") %>
<% end %>
</div>
</p>
</div>
</div>

<script>
$(".pmenu").click(function(){
$(".dropdown").slideToggle();
});
</script>
16 changes: 16 additions & 0 deletions app/views/home/_navbar.html.erb → app/views/home/_top.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<div class="row">
<span class="col-sm-2"></span>
<div class="col-sm-3">
<b><%= "#{user.first_name} #{user.last_name}" %></b>
</div>
<div class="col-sm-4">
<% if current_user==user %>
<button class="btn btn-primary">Update Profile</button>
<% else %>
<%= render(partial: 'friendship', locals:{user: user}) %>
<button class="btn btn-primary">Message</button>
<% end %>
</div>
</div>
<br>

<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="collapse navbar-collapse">
Expand Down
8 changes: 0 additions & 8 deletions app/views/home/_user.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/home/about.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render(partial: 'home/navbar', locals: {user: @user}) %>
<%= render(partial: 'home/top', locals: {user: @user}) %>
6 changes: 4 additions & 2 deletions app/views/home/friends.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<%= render(partial: 'home/navbar', locals: {user: @user}) %>
<%= render(partial: 'home/top', locals: {user: @user}) %>

<div class="row">
<span class="col-md-1"></span>
<div id="main" class="col-md-4">
<ul id="body" type="none">
<% @friends.each do |f| %>
<div class="row">
<div class="col-md-5"><li><%= "#{f.first_name} #{f.last_name}\t" %></div>
<div class="col-md-5">
<%= link_to "#{f.first_name} #{f.last_name}", home_show_path(user_id: f.id) %>
</div>
<div class="col-md-4"><i class="fa fa-check"></i> Friend</div></li>
</div>
<hr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/photos.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render(partial: 'home/navbar', locals: {user: @user}) %>
<%= render(partial: 'home/top', locals: {user: @user}) %>
18 changes: 18 additions & 0 deletions app/views/home/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="row">
<span class="col-md-1"></span>
<div id="main" class="col-md-4">
<ul id="body" type="none">
<% @users.each do |user| %>
<div class="row">
<div class="col-md-5">
<%= link_to "#{user.first_name} #{user.last_name}", home_show_path(user_id: user.id) %>
</div>
<div class="col-md-4">
<%= render(partial: 'friendship', locals:{user: user}) %>
</div></li>
</div>
<hr>
<% end %>
</ul>
</div>
</div>
1 change: 0 additions & 1 deletion app/views/home/search.js.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/home/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render(partial: 'home/navbar', locals: {user: @user}) %>
<%= render(partial: 'home/top', locals: {user: @user}) %>

<div id="main">
<ul id="body" type="none">
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div class="container-fluid">
<div class="collapse navbar-collapse">
<h2 class = "nav navbar-nav navbar-left">Welcome, <%= link_to "#{current_user.first_name}", home_show_path(user_id: current_user.id) %></h2>
<%= form_with url: home_search_path, method: :get do |form| %>
<%= form_with url: home_search_path, method: :get, local:true do |form| %>
<%= form.text_field :friend %>
<% end %>
<ul class="nav navbar-nav navbar-right" style="flex-direction: row;">
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190314142721_add_status_to_friendships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToFriendships < ActiveRecord::Migration[5.2]
def change
add_column :friendships, :status, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_03_13_115322) do
ActiveRecord::Schema.define(version: 2019_03_14_142721) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -51,6 +51,7 @@
t.integer "friend_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status"
end

create_table "likes", force: :cascade do |t|
Expand Down