add posts and boards scaffolds

This commit is contained in:
0chan 2023-12-31 02:51:04 +06:00
parent dc1d284bab
commit 3382eb3635
35 changed files with 752 additions and 0 deletions

View file

@ -0,0 +1,70 @@
class BoardsController < ApplicationController
before_action :set_board, only: %i[ show edit update destroy ]
# GET /boards or /boards.json
def index
@boards = Board.all
end
# GET /boards/1 or /boards/1.json
def show
end
# GET /boards/new
def new
@board = Board.new
end
# GET /boards/1/edit
def edit
end
# POST /boards or /boards.json
def create
@board = Board.new(board_params)
respond_to do |format|
if @board.save
format.html { redirect_to board_url(@board), notice: "Board was successfully created." }
format.json { render :show, status: :created, location: @board }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /boards/1 or /boards/1.json
def update
respond_to do |format|
if @board.update(board_params)
format.html { redirect_to board_url(@board), notice: "Board was successfully updated." }
format.json { render :show, status: :ok, location: @board }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end
# DELETE /boards/1 or /boards/1.json
def destroy
@board.destroy!
respond_to do |format|
format.html { redirect_to boards_url, notice: "Board was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_board
@board = Board.find(params[:id])
end
# Only allow a list of trusted parameters through.
def board_params
params.require(:board).permit(:slug, :name)
end
end

View file

@ -0,0 +1,70 @@
class PostsController < ApplicationController
before_action :set_post, only: %i[ show edit update destroy ]
# GET /posts or /posts.json
def index
@posts = Post.all
end
# GET /posts/1 or /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts or /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to post_url(@post), notice: "Post was successfully created." }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1 or /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to post_url(@post), notice: "Post was successfully updated." }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1 or /posts/1.json
def destroy
@post.destroy!
respond_to do |format|
format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:board_id, :parent_id, :visible_id, :reply_id, :name, :title, :message, :message_formatted, :password, :created_at, :updated_at, :bumped_at, :deleted_at)
end
end

View file

@ -0,0 +1,2 @@
module BoardsHelper
end

View file

@ -0,0 +1,2 @@
module PostsHelper
end

2
app/models/board.rb Normal file
View file

@ -0,0 +1,2 @@
class Board < ApplicationRecord
end

2
app/models/post.rb Normal file
View file

@ -0,0 +1,2 @@
class Post < ApplicationRecord
end

View file

@ -0,0 +1,17 @@
<div id="<%= dom_id board %>">
<p class="my-5">
<strong class="block font-medium mb-1">Slug:</strong>
<%= board.slug %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= board.name %>
</p>
<% if action_name != "show" %>
<%= link_to "Show this board", board, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this board", edit_board_path(board), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>

View file

@ -0,0 +1,2 @@
json.extract! board, :id, :slug, :name, :created_at, :updated_at
json.url board_url(board, format: :json)

View file

@ -0,0 +1,27 @@
<%= form_with(model: board, class: "contents") do |form| %>
<% if board.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(board.errors.count, "error") %> prohibited this board from being saved:</h2>
<ul>
<% board.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :slug %>
<%= form.text_field :slug, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View file

@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing board</h1>
<%= render "form", board: @board %>
<%= link_to "Show this board", @board, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to boards", boards_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,14 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Boards</h1>
<%= link_to "New board", new_board_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="boards" class="min-w-full">
<%= render @boards %>
</div>
</div>

View file

@ -0,0 +1 @@
json.array! @boards, partial: "boards/board", as: :board

View file

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New board</h1>
<%= render "form", board: @board %>
<%= link_to "Back to boards", boards_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @board %>
<%= link_to "Edit this board", edit_board_path(@board), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this board", board_path(@board), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to "Back to boards", boards_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View file

@ -0,0 +1 @@
json.partial! "boards/board", board: @board

View file

@ -0,0 +1,82 @@
<%= form_with(model: post, class: "contents") do |form| %>
<% if post.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :board_id %>
<%= form.number_field :board_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :parent_id %>
<%= form.number_field :parent_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :visible_id %>
<%= form.number_field :visible_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :reply_id %>
<%= form.number_field :reply_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :title %>
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :message %>
<%= form.text_area :message, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :message_formatted %>
<%= form.text_area :message_formatted, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :password %>
<%= form.text_field :password, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :created_at %>
<%= form.datetime_field :created_at, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :updated_at %>
<%= form.datetime_field :updated_at, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :bumped_at %>
<%= form.datetime_field :bumped_at, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :deleted_at %>
<%= form.datetime_field :deleted_at, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View file

@ -0,0 +1,72 @@
<div id="<%= dom_id post %>">
<p class="my-5">
<strong class="block font-medium mb-1">Board:</strong>
<%= post.board_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Parent:</strong>
<%= post.parent_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Visible:</strong>
<%= post.visible_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Reply:</strong>
<%= post.reply_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= post.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= post.title %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Message:</strong>
<%= post.message %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Message formatted:</strong>
<%= post.message_formatted %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Password:</strong>
<%= post.password %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Created at:</strong>
<%= post.created_at %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Updated at:</strong>
<%= post.updated_at %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Bumped at:</strong>
<%= post.bumped_at %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Deleted at:</strong>
<%= post.deleted_at %>
</p>
<% if action_name != "show" %>
<%= link_to "Show this post", post, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this post", edit_post_path(post), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>

View file

@ -0,0 +1,2 @@
json.extract! post, :id, :board_id, :parent_id, :visible_id, :reply_id, :name, :title, :message, :message_formatted, :password, :created_at, :updated_at, :bumped_at, :deleted_at, :created_at, :updated_at
json.url post_url(post, format: :json)

View file

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

View file

@ -0,0 +1,14 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Posts</h1>
<%= link_to "New post", new_post_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="posts" class="min-w-full">
<%= render @posts %>
</div>
</div>

View file

@ -0,0 +1 @@
json.array! @posts, partial: "posts/post", as: :post

View file

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

View file

@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @post %>
<%= 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" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this post", post_path(@post), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View file

@ -0,0 +1 @@
json.partial! "posts/post", post: @post

View file

@ -0,0 +1,10 @@
class CreateBoards < ActiveRecord::Migration[7.1]
def change
create_table :boards do |t|
t.string :slug
t.string :name
t.timestamps
end
end
end

View file

@ -0,0 +1,21 @@
class CreatePosts < ActiveRecord::Migration[7.1]
def change
create_table :posts do |t|
t.integer :board_id
t.integer :parent_id
t.integer :visible_id
t.integer :reply_id
t.string :name
t.string :title
t.text :message
t.text :message_formatted
t.string :password
t.datetime :created_at
t.datetime :updated_at
t.datetime :bumped_at
t.datetime :deleted_at
t.timestamps
end
end
end

21
db/schema.rb generated Normal file
View file

@ -0,0 +1,21 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2023_12_30_203445) do
create_table "boards", force: :cascade do |t|
t.string "slug"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class BoardsControllerTest < ActionDispatch::IntegrationTest
setup do
@board = boards(:one)
end
test "should get index" do
get boards_url
assert_response :success
end
test "should get new" do
get new_board_url
assert_response :success
end
test "should create board" do
assert_difference("Board.count") do
post boards_url, params: { board: { name: @board.name, slug: @board.slug } }
end
assert_redirected_to board_url(Board.last)
end
test "should show board" do
get board_url(@board)
assert_response :success
end
test "should get edit" do
get edit_board_url(@board)
assert_response :success
end
test "should update board" do
patch board_url(@board), params: { board: { name: @board.name, slug: @board.slug } }
assert_redirected_to board_url(@board)
end
test "should destroy board" do
assert_difference("Board.count", -1) do
delete board_url(@board)
end
assert_redirected_to boards_url
end
end

View file

@ -0,0 +1,48 @@
require "test_helper"
class PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@post = posts(:one)
end
test "should get index" do
get posts_url
assert_response :success
end
test "should get new" do
get new_post_url
assert_response :success
end
test "should create post" do
assert_difference("Post.count") do
post posts_url, params: { post: { board_id: @post.board_id, bumped_at: @post.bumped_at, created_at: @post.created_at, deleted_at: @post.deleted_at, message: @post.message, message_formatted: @post.message_formatted, name: @post.name, parent_id: @post.parent_id, password: @post.password, reply_id: @post.reply_id, title: @post.title, updated_at: @post.updated_at, visible_id: @post.visible_id } }
end
assert_redirected_to post_url(Post.last)
end
test "should show post" do
get post_url(@post)
assert_response :success
end
test "should get edit" do
get edit_post_url(@post)
assert_response :success
end
test "should update post" do
patch post_url(@post), params: { post: { board_id: @post.board_id, bumped_at: @post.bumped_at, created_at: @post.created_at, deleted_at: @post.deleted_at, message: @post.message, message_formatted: @post.message_formatted, name: @post.name, parent_id: @post.parent_id, password: @post.password, reply_id: @post.reply_id, title: @post.title, updated_at: @post.updated_at, visible_id: @post.visible_id } }
assert_redirected_to post_url(@post)
end
test "should destroy post" do
assert_difference("Post.count", -1) do
delete post_url(@post)
end
assert_redirected_to posts_url
end
end

9
test/fixtures/boards.yml vendored Normal file
View file

@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
slug: MyString
name: MyString
two:
slug: MyString
name: MyString

31
test/fixtures/posts.yml vendored Normal file
View file

@ -0,0 +1,31 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
board_id: 1
parent_id: 1
visible_id: 1
reply_id: 1
name: MyString
title: MyString
message: MyText
message_formatted: MyText
password: MyString
created_at: 2023-12-31 02:50:17
updated_at: 2023-12-31 02:50:17
bumped_at: 2023-12-31 02:50:17
deleted_at: 2023-12-31 02:50:17
two:
board_id: 1
parent_id: 1
visible_id: 1
reply_id: 1
name: MyString
title: MyString
message: MyText
message_formatted: MyText
password: MyString
created_at: 2023-12-31 02:50:17
updated_at: 2023-12-31 02:50:17
bumped_at: 2023-12-31 02:50:17
deleted_at: 2023-12-31 02:50:17

View file

@ -0,0 +1,7 @@
require "test_helper"
class BoardTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/models/post_test.rb Normal file
View file

@ -0,0 +1,7 @@
require "test_helper"
class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,43 @@
require "application_system_test_case"
class BoardsTest < ApplicationSystemTestCase
setup do
@board = boards(:one)
end
test "visiting the index" do
visit boards_url
assert_selector "h1", text: "Boards"
end
test "should create board" do
visit boards_url
click_on "New board"
fill_in "Name", with: @board.name
fill_in "Slug", with: @board.slug
click_on "Create Board"
assert_text "Board was successfully created"
click_on "Back"
end
test "should update Board" do
visit board_url(@board)
click_on "Edit this board", match: :first
fill_in "Name", with: @board.name
fill_in "Slug", with: @board.slug
click_on "Update Board"
assert_text "Board was successfully updated"
click_on "Back"
end
test "should destroy Board" do
visit board_url(@board)
click_on "Destroy this board", match: :first
assert_text "Board was successfully destroyed"
end
end

65
test/system/posts_test.rb Normal file
View file

@ -0,0 +1,65 @@
require "application_system_test_case"
class PostsTest < ApplicationSystemTestCase
setup do
@post = posts(:one)
end
test "visiting the index" do
visit posts_url
assert_selector "h1", text: "Posts"
end
test "should create post" do
visit posts_url
click_on "New post"
fill_in "Board", with: @post.board_id
fill_in "Bumped at", with: @post.bumped_at
fill_in "Created at", with: @post.created_at
fill_in "Deleted at", with: @post.deleted_at
fill_in "Message", with: @post.message
fill_in "Message formatted", with: @post.message_formatted
fill_in "Name", with: @post.name
fill_in "Parent", with: @post.parent_id
fill_in "Password", with: @post.password
fill_in "Reply", with: @post.reply_id
fill_in "Title", with: @post.title
fill_in "Updated at", with: @post.updated_at
fill_in "Visible", with: @post.visible_id
click_on "Create Post"
assert_text "Post was successfully created"
click_on "Back"
end
test "should update Post" do
visit post_url(@post)
click_on "Edit this post", match: :first
fill_in "Board", with: @post.board_id
fill_in "Bumped at", with: @post.bumped_at
fill_in "Created at", with: @post.created_at
fill_in "Deleted at", with: @post.deleted_at
fill_in "Message", with: @post.message
fill_in "Message formatted", with: @post.message_formatted
fill_in "Name", with: @post.name
fill_in "Parent", with: @post.parent_id
fill_in "Password", with: @post.password
fill_in "Reply", with: @post.reply_id
fill_in "Title", with: @post.title
fill_in "Updated at", with: @post.updated_at
fill_in "Visible", with: @post.visible_id
click_on "Update Post"
assert_text "Post was successfully updated"
click_on "Back"
end
test "should destroy Post" do
visit post_url(@post)
click_on "Destroy this post", match: :first
assert_text "Post was successfully destroyed"
end
end