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

Users can like posts feature (wip) #1

Merged
merged 10 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AllCops:
Exclude:
- 'db/schema.rb'
- 'db/seeds.rb'
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.2"
ruby "3.3.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.8"
Expand Down Expand Up @@ -63,6 +63,7 @@ group :development, :test do
gem "rspec-rails"
gem "pry-rails"
gem "shoulda-matchers"
gem 'solargraph', group: :development
end

group :development do
Expand Down
60 changes: 58 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
backport (1.2.0)
bcrypt (3.1.19)
benchmark (0.3.0)
bindex (0.8.1)
bootsnap (1.16.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -98,6 +101,7 @@ GEM
responders
warden (~> 1.2.3)
diff-lcs (1.5.0)
e2mmap (0.1.0)
erubi (1.12.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
Expand Down Expand Up @@ -134,11 +138,18 @@ GEM
io-console (0.6.0)
irb (1.7.4)
reline (>= 0.3.6)
jaro_winkler (1.5.6)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jsbundling-rails (1.2.1)
railties (>= 6.0.0)
json (2.7.1)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
language_server-protocol (3.17.0.3)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -169,12 +180,18 @@ GEM
net-smtp (0.4.0)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4-x86_64-linux)
nokogiri (1.16.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.2-x86_64-linux)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
parallel (1.24.0)
parser (3.3.0.4)
ast (~> 2.4.1)
racc
pg (1.5.4)
pry (0.14.2)
coderay (~> 1.1)
Expand Down Expand Up @@ -216,17 +233,21 @@ GEM
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbs (2.8.4)
redis (4.8.1)
regexp_parser (2.8.1)
reline (0.3.9)
io-console (~> 0.5)
responders (3.1.0)
actionpack (>= 5.2)
railties (>= 5.2)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand All @@ -249,6 +270,20 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.60.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby-vips (2.2.0)
ffi (~> 1.12)
rubyzip (2.3.2)
Expand All @@ -262,6 +297,22 @@ GEM
simple_form (5.2.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
solargraph (0.50.0)
backport (~> 1.2)
benchmark
bundler (~> 2.0)
diff-lcs (~> 1.4)
e2mmap
jaro_winkler (~> 1.5)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.1)
parser (~> 3.0)
rbs (~> 2.0)
reverse_markdown (~> 2.0)
rubocop (~> 1.38)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand All @@ -272,13 +323,15 @@ GEM
stimulus-rails (1.2.2)
railties (>= 6.0.0)
thor (1.2.2)
tilt (2.3.0)
timeout (0.4.0)
turbo-rails (1.4.0)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
Expand All @@ -292,9 +345,11 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.36)
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down Expand Up @@ -322,14 +377,15 @@ DEPENDENCIES
selenium-webdriver
shoulda-matchers
simple_form
solargraph
sprockets-rails
stimulus-rails
turbo-rails
tzinfo-data
web-console

RUBY VERSION
ruby 3.1.2p20
ruby 3.3.0p0

BUNDLED WITH
2.4.19
10 changes: 8 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require "application_responder"
require 'application_responder'

class ApplicationController < ActionController::Base
self.responder = ApplicationResponder
respond_to :html

def after_sign_in_path_for(resource)
def after_sign_in_path_for(_resource)
posts_path
end

private

def load_friend_requests
@friend_requests = FriendRequest.where(receiver_id: current_user, status: 'pending')
end
end
39 changes: 39 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class LikesController < ApplicationController
before_action :set_post, only: %i[create destroy]

def create
@like = current_user.likes.find_or_initialize_by(post_id: params[:post_id])
if @like.new_record?
if @like.save
flash[:notice] = 'Post liked.'

else
flash[:alert] = 'Unable to like the post.'
end
else
flash[:alert] = 'You have already liked this post.'
end
redirect_to posts_path
end

def destroy
@like = current_user.likes.find_by(post: @post)
if @like
@like.destroy
flash[:notice] = 'Like removed.'
else
flash[:alert] = 'Unable to remove the like.'
end
redirect_to posts_path
end

private

def set_post
@post = Post.find(params[:post_id])
end

def post_params
params.require(:post).permit(:title, :content)
end
end
14 changes: 8 additions & 6 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
class PostsController < ApplicationController
before_action :authenticate_user!
include PostsHelper

def new
@friend_requests = FriendRequest.where(receiver_id: current_user, status: 'pending')
@post = Post.new
end
end

def index
@friend_requests = FriendRequest.where(receiver_id: current_user, status: 'pending')
@posts = PostsService.new(current_user).friends_and_own_posts
@likes = current_user.likes.where(post_id: @posts.pluck(:id))
end

def create
@friend_requests = FriendRequest.where(receiver_id: current_user, status: 'pending')
@post = current_user.posts.build(post_params)
if @post.save
flash[:notice] = "Post created successfully"
flash[:notice] = 'Post created successfully'
redirect_to posts_path
else
flash.now[:alert] = 'Post creation failed.'
Expand All @@ -24,15 +26,15 @@ def create

def edit
@post = Post.find(params[:id])
end
end

def destroy
@post = current_user.posts.find_by(id: params[:id])
if @post
@post.destroy
flash[:notice] = "Post deleted successfully"
flash[:notice] = 'Post deleted successfully'
else
flash[:alert] = "Post not found"
flash[:alert] = 'Post not found'
end
redirect_to posts_path
end
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
module PostsHelper
def liked_by?(post, _user_id)
post.likes.exists?(user: current_user)
end

def current_user_like(post)
post.likes.find_by(user: current_user)
end
end
15 changes: 15 additions & 0 deletions app/views/posts/_like_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% if liked_by?(post, current_user) %>
<%= form_with model: current_user_like(post),
url: post_like_path(post, current_user_like(post)),
method: :delete, class: 'like-form' do |f| %>
<%= f.submit 'Unlike', class: 'btn btn-danger unlike-button' %>
<% end %>
<% else %>
<%= form_with model: Like.new,
url: post_likes_path(post),
method: :post,
class: 'like-form' do |f| %>
<%= f.hidden_field :post_id, value: post.id %>
<%= f.submit 'Like', class: 'btn btn-primary like-button' %>
<% end %>
<% end %>
25 changes: 10 additions & 15 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<div class="trix-content">
<h1>Timeline</h1>
<%= link_to "New Post", new_post_path, class: "btn btn-primary" %>
<% if @posts.any? %>
<% @posts.each do |post| %>
<div class="post">
<h2><%= post.title %></h2>
<p><%= post.content %></p>
<p> This post has <%= post.likes.count %>
<% if %>
<% post.likes.count == 1 %>
<p>like!<p/>
<% else %>
<p> likes! </p>
<% end %>
<p>Posted by: <%= post.user.name %></p>
</div>
<% end %>
<% else %>
<p>No posts to display.</p>
<% end %>
<%= link_to "New Post", new_post_path, class: "btn btn-primary" %>
</div>
<p>This post has <%= pluralize(post.likes.count, "like") %>! </p>
<%= render "like_button", post: post %>
<p>Posted by: <%= post.user.name %></p>
</div>
<% end %>
<% else %>
<p>No posts to display.</p>
<% end %>
</div>
11 changes: 3 additions & 8 deletions app/views/shared/_flash.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<% if flash[:notice] %>
<div class="alert alert-info mt-4" role="alert">
<%= flash[:notice] %>
</div>
<% end %>
<% if flash[:alert] %>
<div class="alert alert-info mt-4" role="alert">
<%= flash[:alert] %>
<% flash.each do |flash_type, message| %>
<div class="alert alert-info mt-4">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div class="alert alert-info mt-4">
<div class="alert alert-info <%= flash_type %> mt-4">

<%= message %>
</div>
<% end %>
14 changes: 8 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Rails.application.routes.draw do
get 'friend_requests/create'
get 'friends/index'
root "pages#home"
root 'pages#home'
devise_for :users
resources :posts
resources :posts do
resources :likes, only: %i[create destroy]
end
resources :notifications, only: [:index]
resources :friendship_acknowledgements, only: [:create, :destroy]
resources :friends, only: [:index, :destroy]
resources :friendships, only: [:create, :destroy]
resources :friend_requests, only: [:create, :destroy]
resources :friendship_acknowledgements, only: %i[create destroy]
resources :friends, only: %i[index destroy]
resources :friendships, only: %i[create destroy]
resources :friend_requests, only: %i[create destroy]
end