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

Creating Forms ๐Ÿ“œ As a Mode Of Input ๐Ÿ‘จโ€๐Ÿ’ป #7

Merged
merged 5 commits into from
Sep 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/assets/stylesheets/new_comment.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.new_comment_section {
margin-top: 40px;
border-top: 3px solid rgb(221, 226, 145);
box-shadow: 2px 5px 5px black;
padding-left: 10px;
}
42 changes: 42 additions & 0 deletions app/assets/stylesheets/new_post.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.new_post_section {
margin: 20px;
margin-left: 125px;
}

.new_post_section > a {
text-decoration: underline;
font-size: 1.5rem;
font-weight: bolder;
color: rgb(100, 120, 163);
}

.new_post_title {
margin-left: 35px;
color: rgb(100, 120, 163);
text-decoration: underline;
}

.back_btn_link {
margin-left: 35px;
}

.back_btn {
margin-left: 35px;
box-shadow: 2px 5px 5px black;
}

.create_post_header {
margin-left: 35px;
}

.Holder > form {
margin-left: 40px;
}

.field {
margin-bottom: 20px;
}

.actions {
margin-bottom: 10px;
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/post_index.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.comments_section {
width: 97%;
padding-left: 10px;
border: 3px solid black;
margin: auto;
margin-bottom: 50px;
Expand All @@ -10,6 +9,7 @@
.comments_line {
display: flex;
margin-bottom: 6px;
padding-left: 10px;
}

.comment {
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ApplicationController < ActionController::Base
def current_user
@user = User.first
end
end
34 changes: 34 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class CommentsController < ApplicationController
before_action :find_user
before_action :find_post

def new
@comment = Comment.new
end

def create
@comment = Comment.new(comment_params)
@comment.author = @user
@comment.post = @post
if @comment.save
flash[:notice] = 'Comment created successfully.'
redirect_to user_post_path(@user, @post)
else
render 'new'
end
end

private

def find_user
@user = ApplicationController.new.current_user
end

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

def comment_params
params.require(:comment).permit(:text)
end
end
64 changes: 64 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,75 @@ def index
@user = User.find(params[:user_id])
@posts = @user.posts
@recent_comments_by_post = @posts.to_h { |post| [post.id, post.recent_comments] }

@first_user = User.first
end

def show
@user = User.find(params[:user_id])
@post = Post.find(params[:id])
@recent_comments = @post.recent_comments
@new_comment = @post.comments.build
end

def new
@user = User.find(params[:user_id])
@post = @user.posts.build
@post.comments_counter = 0
@post.likes_counter = 0
end

def create
@user = User.find(params[:user_id])
@post = @user.posts.build(post_params)
@post.comments_counter = 0
@post.likes_counter = 0
if @post.save
redirect_to user_post_path(@user, @post), notice: 'Post was successfully created.'
else
render :new
end
end

def like
@user = User.find(params[:user_id])
@post = Post.find(params[:id])
if already_liked?(@user, @post)
redirect_to user_post_path(@user, @post)
else
@like = @post.likes.new(author_id: @user.id, post_id: @post.id)

if @like.save
redirect_to user_post_path(@user, @post)
else
redirect_to user_posts_path(@user)
end
end
end

def unlike
@user = User.find(params[:user_id])
@post = Post.find(params[:id])
@like = Like.find_by(author_id: @user.id, post_id: @post.id)

if @like
if @like.destroy
redirect_to user_post_path(@user, @post), notice: 'Post unliked successfully.'
else
redirect_to user_post_path(@user, @post), alert: 'Error occurred while unliking the post.'
end
else
redirect_to user_post_path(@user, @post), alert: 'You have not liked this post.'
end
end

private

def post_params
params.require(:post).permit(:title, :text)
end

def already_liked?(user, post)
Like.exists?(author_id: user.id, post_id: post.id)
end
end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ def index

def show
@user = User.find(params[:id])
@first_user = ApplicationController.new.current_user
end
end
5 changes: 3 additions & 2 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Like < ApplicationRecord
belongs_to :post
belongs_to :author, class_name: 'User'
belongs_to :post, class_name: 'Post'

after_save :update_likes_counter
after_destroy :update_likes_counter

def update_likes_counter
post.increment!(:likes_counter)
post.update(likes_counter: post.likes.count)
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class User < ApplicationRecord
validates :posts_counter, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

has_many :comments, foreign_key: :user_id
has_many :likes, foreign_key: :user_id
has_many :likes, foreign_key: :author_id
has_many :posts, foreign_key: :author_id

def recent_posts
Expand Down
7 changes: 7 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<section class= "new_comment_section">
<%= form_with model: [user, post, comment], scope: "comment" do |form|%>
<h1 class= "comment_title">Add comment</h1>
<%= form.text_area :text%>
<%= form.submit "Comment"%>
<% end %>
</section>
2 changes: 2 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#<h1 class= "new_comment_header">New comment</h1>
#<%= render partial: "form" locals: {comment: @comment}%>
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<%= stylesheet_link_tag 'user_show.css' %>
<%= stylesheet_link_tag 'post_index.css' %>
<%= stylesheet_link_tag 'post_show.css' %>
<%= stylesheet_link_tag 'new_post.css' %>
<%= stylesheet_link_tag 'new_comment.css' %>
<%= javascript_importmap_tags %>
</head>

Expand Down
19 changes: 19 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<a href="<%= user_path(@user)%>" class="back_btn_link">
<button id="back-btn">โฌ…</button>
</a>
<h1 class= "create_post_header">Create a new Post</h1>
<%= form_with model: [@user, @post], local: true do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :text %>
<%= form.text_area :text %>
</div>

<div class="actions">
<%= form.submit 'Create Post' %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<%= render partial: "shared/person_info" %>

<section class= "new_post_section">
<% if @user.id == @first_user.id %>
<%= link_to 'New Post', new_user_post_path(@user) %>
<% end %>
</section>

<section class="posts_section">
<% @posts.each_with_index do |post, i| %>
<a href="<%= user_post_path(@user, post) %>">
Expand Down
2 changes: 2 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1 class= "new_post_title">New Post</h1>
<%= render partial: "form", locals: {post: @post}%>
4 changes: 3 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<%= render partial: "shared/post_info_version_2", locals: {post: @post}%>
<%= render partial: "shared/post_info_version_2", locals: {post: @post,current_user: @user}%>


8 changes: 8 additions & 0 deletions app/views/shared/_post_info_version_2.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<span class="comments_count_v2">Comments: <%= post.comments_counter %></span>
<span class="likes_count_v2">Likes: <%= post.likes_counter %></span>
</span>

<% if current_user.present? %>
<div class="post-actions">
<%= button_to "Like", like_user_post_path(current_user, post), method: :post, remote: true %>
<%= button_to "Unlike", unlike_user_post_path(current_user, post), method: :delete, remote: true %>
</div>
<% end %>
</div>

<section class="comments_section">
Expand All @@ -15,4 +22,5 @@
<%= render partial: "shared/comments_info", locals: { comment: comment } %>
<% end %>
<% end %>
<%= render partial: "comments/form", locals: { user: @user, post: @post, comment: @new_comment } %>
</section>
6 changes: 6 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<%= @user.bio %>
</p>
</section>

<section class= "new_post_section">
<% if @user.id == @first_user.id %>
<%= link_to 'New Post', new_user_post_path(@user) %>
<% end %>
</section>

<section class="posts_section">
<% @user.recent_posts.each_with_index do |post, i| %>
Expand Down
10 changes: 8 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Rails.application.routes.draw do
root "users#index"
resources :users, only: [:index, :new, :create, :show, :update] do
resources :posts, only: [:index, :new, :create, :show, :update]
resources :users do
resources :posts do
member do
post 'like'
delete 'unlike'
end
resources :comments, only: [:create, :update, :destroy]
end
end
end
Loading