From edef1510060528415377e0aaf47f04c12a8af85b Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Sun, 17 Sep 2023 14:59:06 +0100 Subject: [PATCH] Microposts set up Index, new, create all working. Styling is _okay_. Some issues with styling inside trix-editor. --- app/assets/stylesheets/microposts.css | 37 +++++++++++++++++++ app/controllers/microposts_controller.rb | 37 +++++++++++++++++++ app/models/micropost.rb | 1 - app/models/user.rb | 1 + app/views/layouts/application.html.erb | 15 ++++---- app/views/microposts/_form.html.erb | 7 ++++ app/views/microposts/_micropost.html.erb | 6 +++ app/views/microposts/index.html.erb | 13 +++++++ app/views/microposts/new.html.erb | 9 +++++ app/views/microposts/show.html.erb | 1 + app/views/users/new.html.erb | 2 +- config/locales/controllers/microposts/en.yml | 3 ++ config/locales/views/layouts/application.yml | 15 +++++--- config/locales/views/microposts/en.yml | 9 +++++ config/routes.rb | 4 +- db/schema.rb | 10 ++++- .../controllers/microposts_controller_test.rb | 18 +++++++++ test/fixtures/action_text/rich_texts.yml | 2 +- test/fixtures/microposts.yml | 4 +- test/fixtures/users.yml | 6 +++ test/system/new_session_test.rb | 6 +-- test/test_helper.rb | 11 ++++++ 22 files changed, 194 insertions(+), 23 deletions(-) create mode 100644 app/assets/stylesheets/microposts.css create mode 100644 app/controllers/microposts_controller.rb create mode 100644 app/views/microposts/_form.html.erb create mode 100644 app/views/microposts/_micropost.html.erb create mode 100644 app/views/microposts/index.html.erb create mode 100644 app/views/microposts/new.html.erb create mode 100644 app/views/microposts/show.html.erb create mode 100644 config/locales/controllers/microposts/en.yml create mode 100644 config/locales/views/microposts/en.yml create mode 100644 test/controllers/microposts_controller_test.rb diff --git a/app/assets/stylesheets/microposts.css b/app/assets/stylesheets/microposts.css new file mode 100644 index 0000000..e85c297 --- /dev/null +++ b/app/assets/stylesheets/microposts.css @@ -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; + } +} diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb new file mode 100644 index 0000000..8db1a19 --- /dev/null +++ b/app/controllers/microposts_controller.rb @@ -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 diff --git a/app/models/micropost.rb b/app/models/micropost.rb index 788f81d..90e148a 100644 --- a/app/models/micropost.rb +++ b/app/models/micropost.rb @@ -5,5 +5,4 @@ class Micropost < ApplicationRecord has_rich_text :content validates :user, presence: true - validates :content, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 0bb7824..85de9a8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,7 @@ require "securerandom" class User < ApplicationRecord has_secure_password + has_many :microposts validates :username, :first_name, diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 52ec406..2459fcf 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - <%= @title.present? ? "#{@title} | #{t('site_title')}" : t("site_title") %> + <%= @title.present? ? "#{@title} | #{t('.site_title')}" : t(".site_title") %> <%= csrf_meta_tags %> <%= csp_meta_tag %> @@ -13,17 +13,18 @@
<%= link_to root_path do %> -

<%= t("site_title") %>

+

<%= t(".site_title") %>

<% end %> diff --git a/app/views/microposts/_form.html.erb b/app/views/microposts/_form.html.erb new file mode 100644 index 0000000..aad2958 --- /dev/null +++ b/app/views/microposts/_form.html.erb @@ -0,0 +1,7 @@ +<%= form_with model: @micropost, id: "micropost_form" do |f| %> +

<%= title %>

+ + <%= f.rich_text_area :content %> + + <%= f.submit button_text %> +<% end %> diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb new file mode 100644 index 0000000..3376cbf --- /dev/null +++ b/app/views/microposts/_micropost.html.erb @@ -0,0 +1,6 @@ +
+ <%= micropost.content %> +
+ <%= link_to micropost.created_at.strftime("%Y-%m-%d %H:%M"), micropost %> +
+
diff --git a/app/views/microposts/index.html.erb b/app/views/microposts/index.html.erb new file mode 100644 index 0000000..61078b3 --- /dev/null +++ b/app/views/microposts/index.html.erb @@ -0,0 +1,13 @@ +

<%= t(".microposts") %>

+ +<%= link_to t(".new"), new_micropost_path %> + +<% if @microposts.empty? %> +

<%= t(".empty") %>

+<% else %> + +<% end %> diff --git a/app/views/microposts/new.html.erb b/app/views/microposts/new.html.erb new file mode 100644 index 0000000..2912e92 --- /dev/null +++ b/app/views/microposts/new.html.erb @@ -0,0 +1,9 @@ +<% @title = t(".create") %> + +<%= render partial: "form", + locals: { + user: @user, + button_text: t(".create"), + title: t(".create"), + } %> + diff --git a/app/views/microposts/show.html.erb b/app/views/microposts/show.html.erb new file mode 100644 index 0000000..ebdd181 --- /dev/null +++ b/app/views/microposts/show.html.erb @@ -0,0 +1 @@ +<%= render @micropost %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index d4731a3..a389f74 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,4 +1,4 @@ -<% @title = "Register" %> +<% @title = t(".register") %> <%= render partial: "form", locals: { user: @user, diff --git a/config/locales/controllers/microposts/en.yml b/config/locales/controllers/microposts/en.yml new file mode 100644 index 0000000..985d074 --- /dev/null +++ b/config/locales/controllers/microposts/en.yml @@ -0,0 +1,3 @@ +en: + microposts: + created: Successfully created μpost diff --git a/config/locales/views/layouts/application.yml b/config/locales/views/layouts/application.yml index b7d3ca8..9fc589d 100644 --- a/config/locales/views/layouts/application.yml +++ b/config/locales/views/layouts/application.yml @@ -1,7 +1,10 @@ en: - site_title: T S Vallender - home: Home - log_in: Log in - log_out: Log out - profile: Profile - register: Register + layouts: + application: + site_title: T S Vallender + home: Home + log_in: Log in + log_out: Log out + profile: Profile + register: Register + microposts: μposts diff --git a/config/locales/views/microposts/en.yml b/config/locales/views/microposts/en.yml new file mode 100644 index 0000000..d32f8ea --- /dev/null +++ b/config/locales/views/microposts/en.yml @@ -0,0 +1,9 @@ +en: + microposts: + index: + microposts: μposts + empty: You have no μposts yet. + new: New μpost + new: + create: Create μpost + diff --git a/config/routes.rb b/config/routes.rb index 0327847..9cd5981 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,9 @@ Rails.application.routes.draw do # Users and sessions 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" get "confirm_email", to: "email_confirmations#confirm" + + resources :microposts, only: [:index, :new, :create, :show] end diff --git a/db/schema.rb b/db/schema.rb index 3206355..76ead3d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 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 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| t.string "username", 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_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "microposts", "users" end diff --git a/test/controllers/microposts_controller_test.rb b/test/controllers/microposts_controller_test.rb new file mode 100644 index 0000000..b2f0e96 --- /dev/null +++ b/test/controllers/microposts_controller_test.rb @@ -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 = "

Just some text

" + sign_in(users(:user)) + post microposts_url, params: { content: post_content } + assert_response :redirect + follow_redirect! + assert_includes @response.body, post_content + end +end diff --git a/test/fixtures/action_text/rich_texts.yml b/test/fixtures/action_text/rich_texts.yml index a751005..3952ef5 100644 --- a/test/fixtures/action_text/rich_texts.yml +++ b/test/fixtures/action_text/rich_texts.yml @@ -1,4 +1,4 @@ one: - record: some_text (Micropost) + record: one (Micropost) name: content body:

Just some text

diff --git a/test/fixtures/microposts.yml b/test/fixtures/microposts.yml index 26c2649..4453ccd 100644 --- a/test/fixtures/microposts.yml +++ b/test/fixtures/microposts.yml @@ -1,2 +1,2 @@ -some_text: - user: gimli +one: + user: user diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 3352c8a..2329f39 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -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: username: gimli password_digest: <%= BCrypt::Password.create('tolkien-abercrombie-hobb-barker', cost: 5) %> diff --git a/test/system/new_session_test.rb b/test/system/new_session_test.rb index 5b8ccaa..0c8c7cf 100644 --- a/test/system/new_session_test.rb +++ b/test/system/new_session_test.rb @@ -8,9 +8,9 @@ class SessionsTest < ApplicationSystemTestCase visit new_session_path fill_in "username", with: user.username 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") - click_link I18n.t("log_out") + click_link I18n.t("layouts.application.log_out") assert_text I18n.t("sessions.logged_out") end @@ -19,7 +19,7 @@ class SessionsTest < ApplicationSystemTestCase visit new_session_path fill_in "username", with: user.username 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") end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 38efdc9..3d2476e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,3 +9,14 @@ class ActiveSupport::TestCase fixtures :all 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