From bfda66aa16a9e0e4698c190df3d92f3d9c821ccb Mon Sep 17 00:00:00 2001 From: Yatish Mehta Date: Wed, 5 Jun 2024 11:29:52 -0700 Subject: [PATCH] Added a basic Post model (#18) --- Gemfile | 1 + Gemfile.lock | 3 +++ app/models/post.rb | 2 ++ db/migrate/20240605180804_create_posts.rb | 11 +++++++++++ db/schema.rb | 11 ++++++++++- db/structure.sql | 24 +++++++++++++++++++++++ test/factories/posts_factory.rb | 7 +++++++ test/models/post_test.rb | 8 ++++++++ 8 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/models/post.rb create mode 100644 db/migrate/20240605180804_create_posts.rb create mode 100644 test/factories/posts_factory.rb create mode 100644 test/models/post_test.rb diff --git a/Gemfile b/Gemfile index c600b38..2045266 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ group :development, :test do gem "debug", "~> 1.9", ">= 1.9.2", platforms: %i[mri windows] gem "dotenv", "~> 3.1", ">= 3.1.2" gem "factory_bot_rails", "~> 6.4", ">= 6.4.3" + gem "faker", "~> 3.4" gem "rubocop-rails-omakase", "~> 1.0", require: false, group: [:development] gem "minitest-reporters", "~> 1.6", ">= 1.6.1" end diff --git a/Gemfile.lock b/Gemfile.lock index 3ed9cb6..707ba5f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -124,6 +124,8 @@ GEM factory_bot_rails (6.4.3) factory_bot (~> 6.4) railties (>= 5.0.0) + faker (3.4.1) + i18n (>= 1.8.11, < 2) foreman (0.88.1) fugit (1.10.1) et-orbi (~> 1, >= 1.2.7) @@ -337,6 +339,7 @@ DEPENDENCIES dotenv (~> 3.1, >= 3.1.2) erb_lint (~> 0.5) factory_bot_rails (~> 6.4, >= 6.4.3) + faker (~> 3.4) foreman (~> 0.87, >= 0.87.2) jbuilder (~> 2.12) letter_opener (~> 1.10) diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..b2a8b46 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,2 @@ +class Post < ApplicationRecord +end diff --git a/db/migrate/20240605180804_create_posts.rb b/db/migrate/20240605180804_create_posts.rb new file mode 100644 index 0000000..fba683c --- /dev/null +++ b/db/migrate/20240605180804_create_posts.rb @@ -0,0 +1,11 @@ +class CreatePosts < ActiveRecord::Migration[7.1] + def change + create_table :posts, id: :uuid do |t| + t.string :title, null: false + t.string :summary + t.text :content + t.datetime :published_at + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 25d8c6b..cf43683 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,20 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_19_132661) do +ActiveRecord::Schema[7.1].define(version: 2024_06_05_180804) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" + create_table "posts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.string "title", null: false + t.string "summary" + t.text "content" + t.datetime "published_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "solid_queue_blocked_executions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "job_id", null: false t.string "queue_name", null: false diff --git a/db/structure.sql b/db/structure.sql index 7192932..aa4386c 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -39,6 +39,21 @@ CREATE TABLE public.ar_internal_metadata ( ); +-- +-- Name: posts; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.posts ( + id uuid DEFAULT gen_random_uuid() NOT NULL, + title character varying NOT NULL, + summary character varying, + content text, + published_at timestamp(6) without time zone, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL +); + + -- -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: - -- @@ -195,6 +210,14 @@ ALTER TABLE ONLY public.ar_internal_metadata ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key); +-- +-- Name: posts posts_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.posts + ADD CONSTRAINT posts_pkey PRIMARY KEY (id); + + -- -- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -506,6 +529,7 @@ ALTER TABLE ONLY public.solid_queue_scheduled_executions SET search_path TO "$user", public; INSERT INTO "schema_migrations" (version) VALUES +('20240605180804'), ('20240419132661'), ('20240419132660'), ('20240419132659'), diff --git a/test/factories/posts_factory.rb b/test/factories/posts_factory.rb new file mode 100644 index 0000000..37803b6 --- /dev/null +++ b/test/factories/posts_factory.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :post do + title { Faker::Book.title } + content { Faker::Markdown.sandwich(sentences: 5) } + published_at { Faker::Time.between(from: DateTime.now - 1, to: DateTime.now) } + end +end diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..e2ca425 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class PostTest < ActiveSupport::TestCase + test "valid" do + post = FactoryBot.create(:post) + assert post.valid? + end +end