-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc939ba
first commit
fedesapuppo c854f1f
add partials for like and dislike buttons
fedesapuppo ab3622d
wip, cant access @post in partial
fedesapuppo 0a735b4
Update app/views/posts/index.html.erb
fedesapuppo 98d2685
Update app/views/posts/index.html.erb
fedesapuppo ae2f524
complete like and unlike post feature
fedesapuppo e83838f
Update app/helpers/posts_helper.rb
fedesapuppo 9251518
Update app/views/posts/index.html.erb
fedesapuppo 0312054
implement users can like posts feature
fedesapuppo fd2cb67
update flash messages
fedesapuppo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
AllCops: | ||
Exclude: | ||
- 'db/schema.rb' | ||
- 'db/seeds.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
<%= message %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.