Skip to content

Commit 9e10fad

Browse files
committed
Add basic authentication to ApplicationController and implement CRUD actions for PostsController
1 parent b195de2 commit 9e10fad

File tree

11 files changed

+127
-23
lines changed

11 files changed

+127
-23
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
class ApplicationController < ActionController::Base
2+
before_action :authenticate
3+
4+
private
5+
6+
def authenticate
7+
authenticate_or_request_with_http_basic do |username, password|
8+
username == Rails.application.credentials.dig(:basic_auth, :admin_username) &&
9+
password == Rails.application.credentials.dig(:basic_auth, :admin_password)
10+
end
11+
end
212
end
Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,52 @@
11
class PostsController < ApplicationController
2+
before_action :set_post, only: %i[show edit update destroy]
3+
24
def index
35
@posts = Post.all
46
end
57

68
def show
7-
@post = Post.find_by_id(params[:id])
9+
@post = Post.friendly.find(params[:id])
810
end
911

1012
def new
1113
@post = Post.new
14+
@post.save
1215
end
1316

1417
def create
15-
post = Post.new(params[:post])
16-
17-
if post.save
18-
redirect_to posts_path
18+
@post = Post.new(post_params)
19+
if @post.save
20+
redirect_to posts_path, notice: 'Post was successfully created.'
1921
else
2022
render :new
2123
end
2224
end
2325

2426
def edit
25-
@post = Post.find_by_id(params[:id])
26-
27-
if @post.nil?
28-
redirect_to posts_path
29-
end
27+
@post = Post.friendly.find(params[:id])
3028
end
3129

3230
def update
33-
post = Post.find_by_id(params[:id])
34-
35-
if post.update_attributes(params[:post])
36-
redirect_to posts_path
31+
if @post.update(post_params)
32+
redirect_to posts_path, notice: 'Post was successfully updated.'
3733
else
3834
render :edit
3935
end
4036
end
4137

4238
def destroy
43-
post = Post.find_by_id(params[:id])
44-
45-
if post.destroy
46-
redirect_to posts_path, notice: "Post deleted"
47-
else
48-
redirect_to posts_path, notice: "Post could not be deleted"
49-
end
39+
@post.destroy
40+
redirect_to posts_path, notice: 'Post was successfully destroyed.'
5041
end
5142

5243
private
5344

45+
def set_post
46+
@post = Post.friendly.find(params[:id])
47+
end
48+
5449
def post_params
55-
params.require(:post).permit(:user_id, :title, :body, :status, :total_view)
50+
params.require(:post).permit(:title, :body, :user_id, :status)
5651
end
5752
end

app/models/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ class Post < ApplicationRecord
22
extend FriendlyId
33
friendly_id :title, use: :slugged
44

5+
enum status: { approved: 'approved', pending: 'pending', rejected: 'rejected' }
6+
57
validates :title, presence: true
68

79
belongs_to :user

app/views/layouts/application.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<title>CodeLearnApi</title>
55
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
67
<%= csrf_meta_tags %>
78
<%= csp_meta_tag %>
89

@@ -13,4 +14,6 @@
1314
<body>
1415
<%= yield %>
1516
</body>
17+
18+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
1619
</html>

app/views/posts/_form.html.erb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<div class="container">
2+
<%= form_with(model: post, local: true, url: post.persisted? ? post_path(post) : posts_path, method: post.persisted? ? :patch : :post) do |form| %>
3+
<div class="row justify-content-center">
4+
<div class="col-md-6">
5+
<% if post.errors.any? %>
6+
<div class="alert alert-danger" role="alert">
7+
<h2><%= pluralize(post.errors.count, 'error') %> prohibited this post from being saved:</h2>
8+
9+
<ul>
10+
<% post.errors.full_messages.each do |message| %>
11+
<li><%= message %></li>
12+
<% end %>
13+
</ul>
14+
</div>
15+
<% end %>
16+
17+
<div class="mb-3">
18+
<%= form.label :user_id, class: 'form-label' %>
19+
<%= form.collection_select :user_id, User.all, :id, :email, { include_blank: true }, class: 'form-control' %>
20+
</div>
21+
22+
<div class="mb-3">
23+
<%= form.label :title, class: 'form-label' %>
24+
<%= form.text_field :title, class: 'form-control' %>
25+
</div>
26+
27+
<div class="mb-3">
28+
<%= form.label :body, class: 'form-label' %>
29+
<%= form.text_area :body, class: 'form-control' %>
30+
</div>
31+
32+
<div class="mb-3">
33+
<%= form.label :status, class: 'form-label' %>
34+
<%= form.select :status, Post.statuses.keys, class: 'form-control' %>
35+
</div>
36+
37+
<div class="mb-3">
38+
<%= form.submit post.persisted? ? 'Update' : 'Create', class: 'btn btn-primary' %>
39+
</div>
40+
</div>
41+
</div>
42+
<% end %>
43+
</div>

app/views/posts/edit.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="container mt-5">
2+
<h1>New Post</h1>
3+
4+
<%= render 'form', post: @post %>
5+
6+
<%= link_to 'Back', posts_path, class: 'btn btn-secondary mt-3' %>
7+
</div>

app/views/posts/index.html.erb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<div class="container mt-5">
2+
<h1>Posts</h1>
3+
4+
<%= link_to 'New Post', new_post_path, class: 'btn btn-primary mb-3' %>
5+
6+
<table class="table">
7+
<thead>
8+
<tr>
9+
<th>Title</th>
10+
<th>Body</th>
11+
<th>Action</th>
12+
</tr>
13+
</thead>
14+
15+
<tbody>
16+
<% @posts.each do |post| %>
17+
<tr>
18+
<td><%= post.title %></td>
19+
<td><%= post.body %></td>
20+
<td>
21+
<%= link_to 'Edit', edit_post_path(post), class: 'btn btn-warning btn-sm mr-1' %>
22+
<%= form_with(model: post, local: false, method: :delete, data: { confirm: 'Are you sure?' }) do |form| %>
23+
<%= form.submit 'Destroy', class: 'btn btn-danger btn-sm' %>
24+
<% end %>
25+
</td>
26+
</tr>
27+
<% end %>
28+
</tbody>
29+
</table>
30+
</div>

app/views/posts/new.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="container mt-5">
2+
<h1>New Post</h1>
3+
4+
<%= render 'form', post: @post %>
5+
6+
<%= link_to 'Back', posts_path, class: 'btn btn-secondary mt-3' %>
7+
</div>

app/views/posts/show.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="container mt-5">
2+
<h1><%= @post.title %></h1>
3+
<p><%= @post.body %></p>
4+
<%= link_to 'Back', posts_path, class: 'btn btn-secondary' %>
5+
</div>

config/credentials.yml.enc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/5yz8VLM7DrJNIrILtZxu+A7/+223BmNBsZB1KfSeqbQsEMkmSEWLF+YOzG2ETtbj41pqGupTCf0akiy3n9dX1mnVFAA+/C3OjvGZVjxhTdR9Rt/Co+vahMoCP1mZRf59l0eV3yAvuNjjFpqleuNVeCbxuK8xHz1ENvCUg8qK0bqRfTx/2nkDidz2eeFj8MNh8kIZqpfiuZRo6RIfePQOc6XBjefeua5mXMecv0MxnU/wjBYZLPO70EO6mwGDFLybrFzqG1pB7GBzLWgqIHiEg4G8KKbAgVRss4HQI6gyboU46B56aZOO1lMQKR5LGCOxT5Ihs9PyLb1kSXbz81QBIUul1oBV3f5CbQo39l0d83hwFLgVN/je4PrcYOiOPsPqghQ/kygbT6/VD3s6KaIiI3fHbVMbWv/O1VL4j9CJq5T+rgA1fp8l16kW8xFyZr4sgBhSy3qvaKsZkAqyMl3lkjUmqQvcURrynvirIEPExiKYs4HEgksiNHwvAQpRJDK4vlkfgSvWZD8CJybtq/WlpA4mJpgejNJ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