-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
353 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class PostsController < ApplicationController | ||
before_action :set_post, only: %i[ show edit update destroy ] | ||
|
||
# GET /posts | ||
def index | ||
@posts = Post.all | ||
end | ||
|
||
# GET /posts/1 | ||
def show | ||
end | ||
|
||
# GET /posts/new | ||
def new | ||
@post = Post.new | ||
end | ||
|
||
# GET /posts/1/edit | ||
def edit | ||
end | ||
|
||
# POST /posts | ||
def create | ||
@post = Post.new(post_params) | ||
|
||
if @post.save | ||
redirect_to @post, notice: "Post was successfully created." | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /posts/1 | ||
def update | ||
if @post.update(post_params) | ||
redirect_to @post, notice: "Post was successfully updated.", status: :see_other | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /posts/1 | ||
def destroy | ||
@post.destroy! | ||
redirect_to posts_url, notice: "Post was successfully destroyed.", status: :see_other | ||
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(:user_id, :title) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PostsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
def user | ||
User.find_by(id: ActionAuth::Current.user&.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Post < ApplicationRecord | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class User < ActionAuth::User | ||
has_many :posts, dependent: :destroy | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_with(model: post) do |form| %> | ||
<% if post.errors.any? %> | ||
<div style="color: red"> | ||
<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> | ||
<%= form.label :user_id, style: "display: block" %> | ||
<%= form.text_field :user_id %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :title, style: "display: block" %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div id="<%= dom_id post %>"> | ||
<p> | ||
<strong>User:</strong> | ||
<%= post.user_id %> | ||
</p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= post.title %> | ||
</p> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<h1>Editing post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Show this post", @post %> | | ||
<%= link_to "Back to posts", posts_path %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<h1>Posts</h1> | ||
|
||
<div id="posts"> | ||
<% @posts.each do |post| %> | ||
<%= render post %> | ||
<p> | ||
<%= link_to "Show this post", post %> | ||
</p> | ||
<% end %> | ||
</div> | ||
|
||
<%= link_to "New post", new_post_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>New post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Back to posts", posts_path %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render @post %> | ||
|
||
<div> | ||
<%= link_to "Edit this post", edit_post_path(@post) %> | | ||
<%= link_to "Back to posts", posts_path %> | ||
<%= button_to "Destroy this post", @post, method: :delete %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Rails.application.routes.draw do | ||
resources :posts | ||
mount ActionAuth::Engine => 'action_auth' | ||
root 'welcome#index' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreatePosts < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :posts do |t| | ||
t.belongs_to :user, null: false, foreign_key: { to_table: :action_auth_users } | ||
t.string :title | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: { title: @post.title, user_id: @post.user_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: { title: @post.title, user_id: @post.user_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
user: one | ||
title: MyString | ||
|
||
two: | ||
user: two | ||
title: MyString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class PostTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
Oops, something went wrong.