Microposts set up

Index, new, create all working. Styling is _okay_. Some issues with
styling inside trix-editor.
This commit is contained in:
Trevor Vallender 2023-09-17 14:59:06 +01:00
parent a257e98eb6
commit edef151006
22 changed files with 194 additions and 23 deletions

View File

@ -0,0 +1,37 @@
form#micropost_form {
display: flex;
flex-direction: column;
max-width: 55em;
> h2 {
width: 100%;
}
> trix-toolbar {
max-width: 95%;
}
> trix-editor {
width: 95%;
> ul {
list-style-type: disc;
}
}
> input[type=submit] {
width: 95%;
}
}
.micropost {
max-width: 55em;
margin: 1em auto;
padding: .5em;
background-color: var(--inset-background-color);
border: 1px solid var(--border-color);
> .created_at {
text-align: right;
font-size: .8em;
}
}

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show]
def index
@microposts = helpers.current_user.microposts
end
def new
@micropost = Micropost.new
end
def create
micropost = Micropost.new(micropost_params)
micropost.user = helpers.current_user
if micropost.save
redirect_to micropost, notice: t(".created")
else
render :new, status: :unprocessable_entity
end
end
def show; end
private
def micropost_params
params.require(:micropost).permit(
:content,
)
end
def set_micropost
@micropost = Micropost.find(params[:id])
end
end

View File

@ -5,5 +5,4 @@ class Micropost < ApplicationRecord
has_rich_text :content has_rich_text :content
validates :user, presence: true validates :user, presence: true
validates :content, presence: true
end end

View File

@ -4,6 +4,7 @@ require "securerandom"
class User < ApplicationRecord class User < ApplicationRecord
has_secure_password has_secure_password
has_many :microposts
validates :username, validates :username,
:first_name, :first_name,

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title><%= @title.present? ? "#{@title} | #{t('site_title')}" : t("site_title") %></title> <title><%= @title.present? ? "#{@title} | #{t('.site_title')}" : t(".site_title") %></title>
<meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
<%= csp_meta_tag %> <%= csp_meta_tag %>
@ -13,17 +13,18 @@
<body> <body>
<header> <header>
<%= link_to root_path do %> <%= link_to root_path do %>
<h1><%= t("site_title") %></h1> <h1><%= t(".site_title") %></h1>
<% end %> <% end %>
<nav> <nav>
<ul> <ul>
<% if logged_in? %> <% if logged_in? %>
<li><%= link_to t("home"), root_path %></li> <li><%= link_to t(".home"), root_path %></li>
<li><%= link_to t("profile"), user_path(current_user) %></li> <li><%= link_to t(".microposts"), microposts_path %></li>
<li><%= link_to t("log_out"), log_out_path, data: { turbo_method: :delete } %></li> <li><%= link_to t(".profile"), user_path(current_user) %></li>
<li><%= link_to t(".log_out"), log_out_path, data: { turbo_method: :delete } %></li>
<% else %> <% else %>
<li><%= link_to t("log_in"), new_session_path %></li> <li><%= link_to t(".log_in"), new_session_path %></li>
<li><%= link_to t("register"), new_user_path %></li> <li><%= link_to t(".register"), new_user_path %></li>
<% end %> <% end %>
</ul> </ul>
</nav> </nav>

View File

@ -0,0 +1,7 @@
<%= form_with model: @micropost, id: "micropost_form" do |f| %>
<h2><%= title %></h2>
<%= f.rich_text_area :content %>
<%= f.submit button_text %>
<% end %>

View File

@ -0,0 +1,6 @@
<div class="micropost">
<%= micropost.content %>
<div class="created_at">
<%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %>
</div>
</div>

View File

@ -0,0 +1,13 @@
<h2><%= t(".microposts") %></h2>
<%= link_to t(".new"), new_micropost_path %>
<% if @microposts.empty? %>
<p><%= t(".empty") %></p>
<% else %>
<ul id="microposts">
<% @microposts.each do |micropost| %>
<li><%= render micropost %></li>
<% end %>
</ul>
<% end %>

View File

@ -0,0 +1,9 @@
<% @title = t(".create") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".create"),
title: t(".create"),
} %>

View File

@ -0,0 +1 @@
<%= render @micropost %>

View File

@ -1,4 +1,4 @@
<% @title = "Register" %> <% @title = t(".register") %>
<%= render partial: "form", <%= render partial: "form",
locals: { locals: {
user: @user, user: @user,

View File

@ -0,0 +1,3 @@
en:
microposts:
created: Successfully created μpost

View File

@ -1,7 +1,10 @@
en: en:
layouts:
application:
site_title: T S Vallender site_title: T S Vallender
home: Home home: Home
log_in: Log in log_in: Log in
log_out: Log out log_out: Log out
profile: Profile profile: Profile
register: Register register: Register
microposts: μposts

View File

@ -0,0 +1,9 @@
en:
microposts:
index:
microposts: μposts
empty: You have no μposts yet.
new: New μpost
new:
create: Create μpost

View File

@ -7,7 +7,9 @@ Rails.application.routes.draw do
# Users and sessions # Users and sessions
resources :users, only: [:new, :create, :show, :edit, :update] resources :users, only: [:new, :create, :show, :edit, :update]
resources :sessions, only: [:new, :create] resources :sessions, only: [:index, :new, :create]
delete "log_out", to: "sessions#destroy_session" delete "log_out", to: "sessions#destroy_session"
get "confirm_email", to: "email_confirmations#confirm" get "confirm_email", to: "email_confirmations#confirm"
resources :microposts, only: [:index, :new, :create, :show]
end end

10
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_09_17_112623) do ActiveRecord::Schema[7.0].define(version: 2023_09_17_114037) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -52,6 +52,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_17_112623) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end end
create_table "microposts", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_microposts_on_user_id"
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "username", null: false t.string "username", null: false
t.string "password_digest", null: false t.string "password_digest", null: false
@ -67,4 +74,5 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_17_112623) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "microposts", "users"
end end

View File

@ -0,0 +1,18 @@
require "test_helper"
class MicropostsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
sign_in(users(:user))
get microposts_new_url
assert_response :success
end
test "should create" do
post_content = "<p>Just some text</p>"
sign_in(users(:user))
post microposts_url, params: { content: post_content }
assert_response :redirect
follow_redirect!
assert_includes @response.body, post_content
end
end

View File

@ -1,4 +1,4 @@
one: one:
record: some_text (Micropost) record: one (Micropost)
name: content name: content
body: <p>Just some text</p> body: <p>Just some text</p>

View File

@ -1,2 +1,2 @@
some_text: one:
user: gimli user: user

View File

@ -1,3 +1,9 @@
trevor:
username: tsvallender
password_digest: <%= BCrypt::Password.create('tolkien-abercrombie-hobb-barker', cost: 5) %>
first_name: Trevor
last_names: Vallender
email: t@tsvallender.co.uk
user: user:
username: gimli username: gimli
password_digest: <%= BCrypt::Password.create('tolkien-abercrombie-hobb-barker', cost: 5) %> password_digest: <%= BCrypt::Password.create('tolkien-abercrombie-hobb-barker', cost: 5) %>

View File

@ -8,9 +8,9 @@ class SessionsTest < ApplicationSystemTestCase
visit new_session_path visit new_session_path
fill_in "username", with: user.username fill_in "username", with: user.username
fill_in "password", with: "tolkien-abercrombie-hobb-barker" fill_in "password", with: "tolkien-abercrombie-hobb-barker"
click_button I18n.t("log_in") click_button I18n.t("layouts.application.log_in")
assert_text I18n.t("sessions.logged_in") assert_text I18n.t("sessions.logged_in")
click_link I18n.t("log_out") click_link I18n.t("layouts.application.log_out")
assert_text I18n.t("sessions.logged_out") assert_text I18n.t("sessions.logged_out")
end end
@ -19,7 +19,7 @@ class SessionsTest < ApplicationSystemTestCase
visit new_session_path visit new_session_path
fill_in "username", with: user.username fill_in "username", with: user.username
fill_in "password", with: "tolkien-abercrombie-hobb-barker" fill_in "password", with: "tolkien-abercrombie-hobb-barker"
click_button I18n.t("log_in") click_button I18n.t("layouts.application.log_in")
assert_text I18n.t("sessions.account_not_confirmed") assert_text I18n.t("sessions.account_not_confirmed")
end end
end end

View File

@ -9,3 +9,14 @@ class ActiveSupport::TestCase
fixtures :all fixtures :all
end end
class ActionDispatch::IntegrationTest
def sign_in(user)
post sessions_path, params: {
username: user.username,
password: 'tolkien-abercrombie-hobb-barker',
}
follow_redirect!
assert_includes @response.body, I18n.t("sessions.logged_in")
end
end