Skip to content

Commit

Permalink
Merge branch 'new_features_1'
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungHypo committed Nov 22, 2024
2 parents cfb5e79 + fbcf089 commit cf94e84
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 54 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ GEM
nio4r (2.7.4)
nokogiri (1.16.7-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
pg (1.3.5)
Expand Down Expand Up @@ -233,6 +235,7 @@ GEM

PLATFORMS
aarch64-linux
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :require_login
helper_method :current_user

private

def current_user
@current_user ||= User.find_by(id: session[:user_id])
end

def require_login
unless current_user
redirect_to login_path
end
end
end
36 changes: 33 additions & 3 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
class CommentsController < ApplicationController
before_action :set_comment, only: [:edit, :update, :destroy]
before_action :check_ownership, only: [:edit, :update, :destroy]

def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user = current_user
@comment.user = current_user if current_user # Associate with user if authenticated

if @comment.save
redirect_to post_path(@post), notice: 'Comment was successfully added.'
else
redirect_to post_path(@post), alert: @comment.errors.full_messages.join(', ')
end
end


def edit
end

def update
if @comment.update(comment_params)
redirect_to post_path(@comment.post), notice: 'Comment was successfully updated.'
else
render :edit
end
end

def destroy
post = @comment.post
@comment.destroy
redirect_to post_path(post), notice: 'Comment was successfully deleted.'
end

private


def set_comment
@comment = Comment.find(params[:id])
end

def comment_params
params.require(:comment).permit(:content)
end

def check_ownership
unless @comment.user == current_user
redirect_to root_path, alert: 'You can only modify your own comments.'
end
end
end
32 changes: 17 additions & 15 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,61 @@ class PostsController < ApplicationController
before_action :check_ownership, only: [:edit, :update, :destroy]

def index
@posts = if params[:username].present?
User.find_by(username: params[:username])&.posts || Post.none
if current_user
# Show only user's posts when authenticated
@posts = current_user.posts.order(created_at: :desc)
else
Post.all
# Show all posts when unauthenticated
@posts = Post.all.order(created_at: :desc)
end
@posts = @posts.order(created_at: :desc)
end

def show
@comment = Comment.new
end

def new
@post = Post.new
end

def create
@post = current_user.posts.build(post_params)
@post = Post.new(post_params)
@post.user = current_user if current_user # Associate with user if authenticated

if @post.save
redirect_to root_path, notice: 'Post was successfully created.'
else
render :new
end
end

def edit
end

def update
if @post.update(post_params)
redirect_to @post, notice: 'Post was successfully updated.'
else
render :edit
end
end

def destroy
@post.destroy
redirect_to posts_url, notice: 'Post was successfully deleted.'
end

private

def set_post
@post = Post.find_by(id: params[:id])
render file: 'public/404.html', status: :not_found unless @post
end

def post_params
params.require(:post).permit(:content)
end

def check_ownership
unless @post.user == current_user
redirect_to root_path, alert: 'You can only modify your own posts.'
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class SessionsController < ApplicationController
skip_before_action :require_login, only: [:new, :create]

def new
end

Expand All @@ -12,6 +10,6 @@ def create

def destroy
session[:user_id] = nil
redirect_to login_path
redirect_to root_path
end
end
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Comment < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true # Added optional
belongs_to :post

validates :content, presence: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Post < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true # Added optional
has_many :comments, dependent: :destroy

validates :content, presence: true
Expand Down
24 changes: 24 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="comment-form">
<h1>Edit Comment</h1>

<%= form_for [@comment.post, @comment] do |f| %>
<% if @comment.errors.any? %>
<div class="alert alert-danger">
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= f.text_area :content, class: 'form-control', rows: 3 %>
</div>

<div class="form-actions">
<%= f.submit "Update Comment", class: 'btn btn-primary' %>
<%= link_to 'Back', post_path(@comment.post), class: 'btn btn-secondary' %>
</div>
<% end %>
</div>
29 changes: 23 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<%= javascript_include_tag 'application' %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<style>
/* Add some basic styling */
.container { max-width: 800px; margin: 0 auto; padding: 20px; }
.post-card { border: 1px solid #ddd; padding: 15px; margin-bottom: 15px; }
.comment { border-left: 3px solid #ddd; padding-left: 10px; margin: 10px 0; }
Expand Down Expand Up @@ -80,17 +79,35 @@
form {
display: inline-block;
}

.navbar {
background-color: #f8f9fa;
padding: 10px 20px;
margin-bottom: 20px;
}

.nav-links {
display: flex;
gap: 15px;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<% if current_user %>
<div class="navbar">
<span>Logged in as: <%= current_user.username %></span>
<div class="navbar">
<div class="nav-links">
<%= link_to 'Home', root_path, class: 'btn btn-secondary' %>
<%= link_to 'Logout', logout_path, class: 'btn btn-secondary' %>
<% if current_user %>
<span>Logged in as: <%= current_user.username %></span>
<%= link_to 'My Posts', root_path, class: 'btn btn-secondary' %>
<%= link_to 'Logout', logout_path, class: 'btn btn-secondary' %>
<% else %>
<%= link_to 'Login', login_path, class: 'btn btn-primary' %>
<%= link_to 'All Posts', root_path, class: 'btn btn-secondary' %>
<% end %>
</div>
<% end %>
</div>

<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == 'notice' ? 'success' : 'danger' %>">
Expand Down
10 changes: 5 additions & 5 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
<%= post.content %>
</div>
<div class="post-meta">
<span>By: <%= post.user.username %></span>
<span>By: <%= post.user ? post.user.username : "Anonymous" %></span>
<span><%= link_to "Comments (#{post.comments.count})", post_path(post) %></span>
<span>Last updated: <%= time_ago_in_words(post.updated_at) %> ago</span>
</div>
<% if post.user == current_user %>
<% if current_user && post.user == current_user %>
<div class="post-actions">
<%= link_to 'Edit', edit_post_path(post), class: 'btn btn-sm btn-secondary' %>
<%= button_to 'Delete', post_path(post),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-sm btn-danger' %>
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-sm btn-danger' %>
</div>
<% end %>
</div>
Expand Down
21 changes: 15 additions & 6 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<%= @post.content %>
</div>
<div class="post-meta">
<span>By: <%= @post.user.username %></span>
<span>By: <%= @post.user ? @post.user.username : "Anonymous" %></span>
<span>Last updated: <%= time_ago_in_words(@post.updated_at) %> ago</span>
</div>
<% if @post.user == current_user %>
<% if current_user && @post.user == current_user %>
<div class="post-actions">
<%= link_to 'Edit', edit_post_path(@post), class: 'btn btn-secondary' %>
<%= button_to 'Delete', post_path(@post),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-danger' %>
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-danger' %>
</div>
<% end %>
</div>
Expand All @@ -25,7 +25,16 @@
<% @post.comments.each do |comment| %>
<div class="comment">
<p><%= comment.content %></p>
<small>By: <%= comment.user.username %></small>
<small>By: <%= comment.user ? comment.user.username : "Anonymous" %></small>
<% if current_user && comment.user == current_user %>
<div class="comment-actions">
<%= link_to 'Edit', edit_post_comment_path(@post, comment), class: 'btn btn-sm btn-secondary' %>
<%= button_to 'Delete', post_comment_path(@post, comment),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'btn btn-sm btn-danger' %>
</div>
<% end %>
</div>
<% end %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Rails.application.routes.draw do
root 'posts#index'

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'

resources :posts do
resources :comments, only: [:create]
resources :comments, only: [:create, :edit, :update, :destroy]
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241122001837_allow_null_user_in_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AllowNullUserInPosts < ActiveRecord::Migration[7.1]
def change
change_column_null :posts, :user_id, true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241122001838_allow_null_user_in_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AllowNullUserInComments < ActiveRecord::Migration[7.1]
def change
change_column_null :comments, :user_id, true
end
end
6 changes: 3 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf94e84

Please sign in to comment.