From 8a0d78fbf448ed607e0a48973d936ceed79ad43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 1 Mar 2021 22:02:18 +0100 Subject: Add ActiveRecord models and migrations --- db/comment.rb | 20 ++++++++++++++++++++ db/config.rb | 17 +++++++++++++++++ db/init.rb | 11 +++++++++++ db/post.rb | 18 ++++++++++++++++++ db/tag.rb | 26 ++++++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 db/comment.rb create mode 100644 db/config.rb create mode 100644 db/init.rb create mode 100644 db/post.rb create mode 100644 db/tag.rb diff --git a/db/comment.rb b/db/comment.rb new file mode 100644 index 0000000..b3977f6 --- /dev/null +++ b/db/comment.rb @@ -0,0 +1,20 @@ +class Comment < ActiveRecord::Base + belongs_to :post + validates_presence_of :date + validates_presence_of :author + validates_presence_of :email + validates_format_of :email, with: /@/ + validates_presence_of :body +end + +class CreateCommentTable < ActiveRecord::Migration[6.0] + def change + create_table :comments do |t| + t.references :post, foreign_key: true, index: true + t.string :author, null: false + t.string :email, null: false + t.string :body, null: false + t.timestamps + end + end +end diff --git a/db/config.rb b/db/config.rb new file mode 100644 index 0000000..55b4106 --- /dev/null +++ b/db/config.rb @@ -0,0 +1,17 @@ +class Config < ActiveRecord::Base + self.table_name = 'config' + validates_presence_of :title + validates_presence_of :timezone + validates_presence_of :postsperpage +end + +class CreateConfigTable < ActiveRecord::Migration[6.0] + def change + create_table :config do |t| + t.string :title, null: false + t.string :subtitle + t.string :timezone, null: false + t.integer :postsperpage, null: false + end + end +end diff --git a/db/init.rb b/db/init.rb new file mode 100644 index 0000000..7c08faf --- /dev/null +++ b/db/init.rb @@ -0,0 +1,11 @@ +require 'active_record' + +ActiveRecord::Base.establish_connection( + adapter: 'sqlite3', + database: 'db.sqlite3' +) + +require_relative 'config' +require_relative 'comment' +require_relative 'post' +require_relative 'tag' diff --git a/db/post.rb b/db/post.rb new file mode 100644 index 0000000..1d44051 --- /dev/null +++ b/db/post.rb @@ -0,0 +1,18 @@ +class Post < ActiveRecord::Base + has_many :comments + has_many :tags, through: :post_tag_links + + validates_presence_of :title + validates_presence_of :body +end + +class CreatePostTable < ActiveRecord::Migration[6.0] + def change + create_table :posts do |t| + t.string :title, null: false + t.string :body, null: false + t.boolean :locked, default: false + t.timestamps + end + end +end diff --git a/db/tag.rb b/db/tag.rb new file mode 100644 index 0000000..7f3446c --- /dev/null +++ b/db/tag.rb @@ -0,0 +1,26 @@ +class PostTagLink < Active::Base + belongs_to :post + belongs_to :tag +end + +class CreatePostTagLinkTable < ActiveRecord::Migration[6.0] + def change + create_table :post_tag_links do |t| + t.references :post, foreign_key: true, index: true + t.references :tag, foreign_key: true, index: true + end + end +end + +class Tag < ActiveRecord::Base + belongs_to :post + validates_presence_of :name +end + +class CreateTagTable < ActiveRecord::Migration[6.0] + def change + create_table :tags do |t| + t.string :name, null: false + end + end +end -- cgit v1.2.3