Skip to content

Commit 38bde05

Browse files
committed
Add post scaffold and fix the failure of the installation script :(
1 parent d1cb139 commit 38bde05

21 files changed

+323
-4
lines changed

app/assets/stylesheets/application.tailwind.css

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
1-
@import "@afomera/richer-text/dist/css/richer-text.css";
2-
@import "highlight.js/styles/github-dark.css";
3-
@import "richer-text.css";
1+
/* @import "@afomera/richer-text/dist/css/richer-text.css"; */
2+
/* @import "highlight.js/styles/github-dark"; */
3+
44
@tailwind base;
55
@tailwind components;
66
@tailwind utilities;
77

8+
.richer-text table {
9+
border-collapse: collapse;
10+
margin: 0;
11+
overflow: hidden;
12+
table-layout: fixed;
13+
width: 100%;
14+
}
15+
16+
.richer-text table td,
17+
.richer-text table th {
18+
border: 2px solid #ced4da;
19+
box-sizing: border-box;
20+
min-width: 1em;
21+
padding: 3px 5px;
22+
position: relative;
23+
vertical-align: top;
24+
}
25+
26+
.richer-text table td>*,
27+
.richer-text table th>* {
28+
margin-top: 0;
29+
margin-bottom: 0;
30+
}
31+
32+
.richer-text table th {
33+
background-color: #f1f3f5;
34+
font-weight: bold;
35+
text-align: left;
36+
}
37+
38+
.richer-text p {
39+
margin: 0;
40+
}
41+
42+
.richer-text img {
43+
width: auto;
44+
}
45+
46+
.richer-text blockquote {
47+
border-left: 2px solid #ced4da;
48+
margin: 1rem 0.5rem;
49+
padding: 0 0 0 10px;
50+
}
51+
852
/*
953
1054
@layer components {

app/controllers/posts_controller.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class PostsController < ApplicationController
2+
before_action :set_post, only: %i[ show edit update destroy ]
3+
4+
# GET /posts or /posts.json
5+
def index
6+
@posts = Post.all
7+
end
8+
9+
# GET /posts/1 or /posts/1.json
10+
def show
11+
end
12+
13+
# GET /posts/new
14+
def new
15+
@post = Post.new
16+
end
17+
18+
# GET /posts/1/edit
19+
def edit
20+
end
21+
22+
# POST /posts or /posts.json
23+
def create
24+
@post = Post.new(post_params)
25+
26+
respond_to do |format|
27+
if @post.save
28+
format.html { redirect_to @post, notice: "Post was successfully created." }
29+
format.json { render :show, status: :created, location: @post }
30+
else
31+
format.html { render :new, status: :unprocessable_entity }
32+
format.json { render json: @post.errors, status: :unprocessable_entity }
33+
end
34+
end
35+
end
36+
37+
# PATCH/PUT /posts/1 or /posts/1.json
38+
def update
39+
respond_to do |format|
40+
if @post.update(post_params)
41+
format.html { redirect_to @post, notice: "Post was successfully updated." }
42+
format.json { render :show, status: :ok, location: @post }
43+
else
44+
format.html { render :edit, status: :unprocessable_entity }
45+
format.json { render json: @post.errors, status: :unprocessable_entity }
46+
end
47+
end
48+
end
49+
50+
# DELETE /posts/1 or /posts/1.json
51+
def destroy
52+
@post.destroy!
53+
54+
respond_to do |format|
55+
format.html { redirect_to posts_path, status: :see_other, notice: "Post was successfully destroyed." }
56+
format.json { head :no_content }
57+
end
58+
end
59+
60+
private
61+
# Use callbacks to share common setup or constraints between actions.
62+
def set_post
63+
@post = Post.find(params.expect(:id))
64+
end
65+
66+
# Only allow a list of trusted parameters through.
67+
def post_params
68+
params.expect(post: [ :content ])
69+
end
70+
end

app/helpers/posts_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PostsHelper
2+
end

app/models/post.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Post < ApplicationRecord
2+
has_richer_text :content, store_as: :json
3+
end

app/views/posts/_form.html.erb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%= form_with(model: post, class: "contents") do |form| %>
2+
<% if post.errors.any? %>
3+
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
4+
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
5+
6+
<ul>
7+
<% post.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="my-5 w-[500px]">
15+
<%= form.label :content %>
16+
<%= form.richer_text_area :content, class: "w-full" %>
17+
</div>
18+
19+
<div class="inline">
20+
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
21+
</div>
22+
<% end %>

app/views/posts/_post.html.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div id="<%= dom_id post %>">
2+
<p class="my-5">
3+
<strong class="block font-medium mb-1">Content:</strong>
4+
<%= post.content %>
5+
</p>
6+
7+
</div>

app/views/posts/_post.json.jbuilder

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
json.extract! post, :id, :content, :created_at, :updated_at
2+
json.url post_url(post, format: :json)
3+
json.content post.content.to_s

app/views/posts/edit.html.erb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="mx-auto md:w-2/3 w-full">
2+
<h1 class="font-bold text-4xl">Editing post</h1>
3+
4+
<%= render "form", post: @post %>
5+
6+
<%= link_to "Show this post", @post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
7+
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
8+
</div>

app/views/posts/index.html.erb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="w-full">
2+
<% if notice.present? %>
3+
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
4+
<% end %>
5+
6+
<% content_for :title, "Posts" %>
7+
8+
<div class="flex justify-between items-center">
9+
<h1 class="font-bold text-4xl">Posts</h1>
10+
<%= link_to "New post", new_post_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
11+
</div>
12+
13+
<div id="posts" class="min-w-full">
14+
<% @posts.each do |post| %>
15+
<%= render post %>
16+
<p>
17+
<%= link_to "Show this post", post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
18+
</p>
19+
<% end %>
20+
</div>
21+
</div>

app/views/posts/index.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.array! @posts, partial: "posts/post", as: :post

app/views/posts/new.html.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="mx-auto md:w-2/3 w-full">
2+
<h1 class="font-bold text-4xl">New post</h1>
3+
4+
<%= render "form", post: @post %>
5+
6+
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
7+
</div>

app/views/posts/show.html.erb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="mx-auto md:w-2/3 w-full flex">
2+
<div class="mx-auto">
3+
<% if notice.present? %>
4+
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
5+
<% end %>
6+
7+
<%= render @post %>
8+
9+
<%= link_to "Edit this post", edit_post_path(@post), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
10+
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
11+
<div class="inline-block ml-2">
12+
<%= button_to "Destroy this post", @post, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
13+
</div>
14+
</div>
15+
</div>

app/views/posts/show.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.partial! "posts/post", post: @post

config/initializers/assets.rb

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55

66
# Add additional assets to the asset load path.
77
# Rails.application.config.assets.paths << Emoji.images_path
8+
9+
# Rails.application.config.assets.paths << ""

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :posts
23
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
34

45
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreatePosts < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :posts do |t|
4+
5+
t.timestamps
6+
end
7+
end
8+
end

db/schema.rb

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "test_helper"
2+
3+
class PostsControllerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
@post = posts(:one)
6+
end
7+
8+
test "should get index" do
9+
get posts_url
10+
assert_response :success
11+
end
12+
13+
test "should get new" do
14+
get new_post_url
15+
assert_response :success
16+
end
17+
18+
test "should create post" do
19+
assert_difference("Post.count") do
20+
post posts_url, params: { post: {} }
21+
end
22+
23+
assert_redirected_to post_url(Post.last)
24+
end
25+
26+
test "should show post" do
27+
get post_url(@post)
28+
assert_response :success
29+
end
30+
31+
test "should get edit" do
32+
get edit_post_url(@post)
33+
assert_response :success
34+
end
35+
36+
test "should update post" do
37+
patch post_url(@post), params: { post: {} }
38+
assert_redirected_to post_url(@post)
39+
end
40+
41+
test "should destroy post" do
42+
assert_difference("Post.count", -1) do
43+
delete post_url(@post)
44+
end
45+
46+
assert_redirected_to posts_url
47+
end
48+
end

test/fixtures/posts.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
5+
two:

test/models/post_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class PostTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

test/system/posts_test.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "application_system_test_case"
2+
3+
class PostsTest < ApplicationSystemTestCase
4+
setup do
5+
@post = posts(:one)
6+
end
7+
8+
test "visiting the index" do
9+
visit posts_url
10+
assert_selector "h1", text: "Posts"
11+
end
12+
13+
test "should create post" do
14+
visit posts_url
15+
click_on "New post"
16+
17+
click_on "Create Post"
18+
19+
assert_text "Post was successfully created"
20+
click_on "Back"
21+
end
22+
23+
test "should update Post" do
24+
visit post_url(@post)
25+
click_on "Edit this post", match: :first
26+
27+
click_on "Update Post"
28+
29+
assert_text "Post was successfully updated"
30+
click_on "Back"
31+
end
32+
33+
test "should destroy Post" do
34+
visit post_url(@post)
35+
click_on "Destroy this post", match: :first
36+
37+
assert_text "Post was successfully destroyed"
38+
end
39+
end

0 commit comments

Comments
 (0)