-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add posts resources and install ActionText
- Loading branch information
1 parent
045437d
commit 183a900
Showing
29 changed files
with
939 additions
and
13 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,53 @@ | ||
class PostsController < ApplicationController | ||
before_action :set_post, only: %i[ show edit update destroy ] | ||
|
||
# GET /posts | ||
def index | ||
@posts = Post.all | ||
end | ||
|
||
def show | ||
end | ||
|
||
def new | ||
@post = Post.new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
def destroy | ||
@post.destroy! | ||
redirect_to posts_path, notice: "Post was successfully destroyed.", status: :see_other | ||
end | ||
|
||
private | ||
|
||
def set_post | ||
@post = Post.find(params.expect(:id)) | ||
end | ||
|
||
|
||
def post_params | ||
# TODO: merge user_id from current user once authentication is built. | ||
params.expect(post: [ :title, :message, :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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
import "@hotwired/turbo-rails" | ||
import "controllers" | ||
|
||
import "trix" | ||
import "@rails/actiontext" |
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 @@ | ||
class Post < ApplicationRecord | ||
#----------------------------------------------------------------------------- | ||
# Associations | ||
#----------------------------------------------------------------------------- | ||
belongs_to :user | ||
has_rich_text :message | ||
|
||
#----------------------------------------------------------------------------- | ||
# Validations | ||
#----------------------------------------------------------------------------- | ||
validates :title, presence: true | ||
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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
class User < ApplicationRecord | ||
#----------------------------------------------------------------------------- | ||
# Associations | ||
#----------------------------------------------------------------------------- | ||
|
||
has_many :posts, dependent: :destroy | ||
|
||
#----------------------------------------------------------------------------- | ||
# Validations | ||
#----------------------------------------------------------------------------- | ||
|
||
validates :name, presence: true | ||
validates :email, presence: true, uniqueness: true | ||
|
||
#----------------------------------------------------------------------------- | ||
# Authentication | ||
#----------------------------------------------------------------------------- | ||
|
||
has_secure_password | ||
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,14 @@ | ||
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>"> | ||
<% if blob.representable? %> | ||
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> | ||
<% end %> | ||
|
||
<figcaption class="attachment__caption"> | ||
<% if caption = blob.try(:caption) %> | ||
<%= caption %> | ||
<% else %> | ||
<span class="attachment__name"><%= blob.filename %></span> | ||
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> | ||
<% end %> | ||
</figcaption> | ||
</figure> |
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 @@ | ||
<div class="trix-content"> | ||
<%= yield -%> | ||
</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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<%= 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 :title %> | ||
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> | ||
</div> | ||
|
||
<div class="my-5"> | ||
<%= form.label :message %> | ||
<%= form.rich_textarea :message, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> | ||
</div> | ||
|
||
<%# TODO: Remove once user authentication is added %> | ||
<%= form.hidden_field :user_id, value: User.first.id %> | ||
|
||
<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 %> |
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 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">User:</strong> | ||
<%= post.user_id %> | ||
</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,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> |
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,21 @@ | ||
<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 %> | ||
|
||
<% content_for :title, "Posts" %> | ||
|
||
<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"> | ||
<% @posts.each do |post| %> | ||
<%= render post %> | ||
<p> | ||
<%= link_to "Show this post", post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</p> | ||
<% end %> | ||
</div> | ||
</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,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> |
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,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" %> | ||
<%= link_to "Back to posts", posts_path, class: "ml-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, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> | ||
</div> | ||
</div> | ||
</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
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
57 changes: 57 additions & 0 deletions
57
db/migrate/20241003035050_create_active_storage_tables.active_storage.rb
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,57 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < ActiveRecord::Migration[7.0] | ||
def change | ||
# Use Active Record's configured type for primary and foreign keys | ||
primary_key_type, foreign_key_type = primary_and_foreign_key_types | ||
|
||
create_table :active_storage_blobs, id: primary_key_type do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
t.string :service_name, null: false | ||
t.bigint :byte_size, null: false | ||
t.string :checksum | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :key ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments, id: primary_key_type do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type | ||
t.references :blob, null: false, type: foreign_key_type | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
|
||
create_table :active_storage_variant_records, id: primary_key_type do |t| | ||
t.belongs_to :blob, null: false, index: false, type: foreign_key_type | ||
t.string :variation_digest, null: false | ||
|
||
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
|
||
private | ||
def primary_and_foreign_key_types | ||
config = Rails.configuration.generators | ||
setting = config.options[config.orm][:primary_key_type] | ||
primary_key_type = setting || :primary_key | ||
foreign_key_type = setting || :bigint | ||
[ primary_key_type, foreign_key_type ] | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
db/migrate/20241003035051_create_action_text_tables.action_text.rb
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,26 @@ | ||
# This migration comes from action_text (originally 20180528164100) | ||
class CreateActionTextTables < ActiveRecord::Migration[6.0] | ||
def change | ||
# Use Active Record's configured type for primary and foreign keys | ||
primary_key_type, foreign_key_type = primary_and_foreign_key_types | ||
|
||
create_table :action_text_rich_texts, id: primary_key_type do |t| | ||
t.string :name, null: false | ||
t.text :body, size: :long | ||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type | ||
|
||
t.timestamps | ||
|
||
t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true | ||
end | ||
end | ||
|
||
private | ||
def primary_and_foreign_key_types | ||
config = Rails.configuration.generators | ||
setting = config.options[config.orm][:primary_key_type] | ||
primary_key_type = setting || :primary_key | ||
foreign_key_type = setting || :bigint | ||
[ primary_key_type, foreign_key_type ] | ||
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,10 @@ | ||
class CreatePosts < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :posts do |t| | ||
t.string :title | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.