From 775e3e014a4f6f69a4d1da0d1a6b3420cfd4ed62 Mon Sep 17 00:00:00 2001 From: Dilraj Somel Date: Sat, 3 Jun 2023 14:37:20 -0400 Subject: [PATCH 1/4] Created the migration for `kathas`, `gianis` and `chapter_kathas` --- app/models/giani.rb | 2 + ...te_kathas_and_gianis_and_chapter_kathas.rb | 44 +++++++++++++++++++ db/schema.rb | 34 +++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 app/models/giani.rb create mode 100644 db/migrate/20230603170655_create_kathas_and_gianis_and_chapter_kathas.rb diff --git a/app/models/giani.rb b/app/models/giani.rb new file mode 100644 index 0000000..00e047a --- /dev/null +++ b/app/models/giani.rb @@ -0,0 +1,2 @@ +class Giani < ApplicationRecord +end diff --git a/db/migrate/20230603170655_create_kathas_and_gianis_and_chapter_kathas.rb b/db/migrate/20230603170655_create_kathas_and_gianis_and_chapter_kathas.rb new file mode 100644 index 0000000..ed8204b --- /dev/null +++ b/db/migrate/20230603170655_create_kathas_and_gianis_and_chapter_kathas.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +class CreateKathasAndGianisAndChapterKathas < ActiveRecord::Migration[7.0] + def up + create_table :gianis do |t| + t.string :name, :null => false + t.string :artwork_url + + t.timestamps + end + + create_table :kathas do |t| + t.references :giani, :null => false, :foreign_key => true + t.string :title + t.string :public_url + t.string :soundcloud_url + t.integer :year + + t.timestamps + end + + create_table :chapter_kathas do |t| + t.references :chapter, :null => false, :foreign_key => true + t.references :katha, :null => false, :foreign_key => true + + t.timestamps + end + + # Seeding Gianis + Giani.create!([ + { :id => 1, :name => 'Giani Harbhajan Singh Dhudike' }, + { :id => 2, :name => 'Nihang Giani Sher Singh Ambala' }, + { :id => 3, :name => 'Sant Giani Inderjit Singh Raqbewale' }, + { :id => 4, :name => 'Bhai Sukha Singh UK' }, + { :id => 5, :name => 'The Suraj Podcast' } + ]) + end + + def down + drop_table :gianis + drop_table :kathas + drop_table :chapter_kathas + end +end diff --git a/db/schema.rb b/db/schema.rb index 2ce993a..fdac006 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_05_08_124727) do +ActiveRecord::Schema[7.0].define(version: 2023_06_03_170655) do create_table "books", force: :cascade do |t| t.integer "sequence", null: false t.string "title", null: false @@ -22,6 +22,15 @@ t.datetime "updated_at", null: false end + create_table "chapter_kathas", force: :cascade do |t| + t.integer "chapter_id", null: false + t.integer "katha_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["chapter_id"], name: "index_chapter_kathas_on_chapter_id" + t.index ["katha_id"], name: "index_chapter_kathas_on_katha_id" + end + create_table "chapters", force: :cascade do |t| t.integer "book_id", null: false t.integer "number", null: false @@ -68,6 +77,24 @@ t.index ["pauri_id"], name: "index_external_pauris_on_pauri_id" end + create_table "gianis", force: :cascade do |t| + t.string "name", null: false + t.string "artwork_url" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "kathas", force: :cascade do |t| + t.integer "giani_id", null: false + t.string "title" + t.string "public_url" + t.string "soundcloud_url" + t.integer "year" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["giani_id"], name: "index_kathas_on_giani_id" + end + create_table "pauri_footnotes", force: :cascade do |t| t.integer "pauri_id", null: false t.text "bhai_vir_singh_footnote" @@ -88,7 +115,7 @@ end create_table "pauris", force: :cascade do |t| - t.integer "number", null: false + t.integer "number" t.integer "chapter_id", null: false t.integer "chhand_id", null: false t.datetime "created_at", null: false @@ -130,10 +157,13 @@ t.index ["sequence", "pauri_id"], name: "index_tuks_on_sequence_and_pauri_id", unique: true end + add_foreign_key "chapter_kathas", "chapters" + add_foreign_key "chapter_kathas", "kathas" add_foreign_key "chapters", "books" add_foreign_key "chhands", "chapters" add_foreign_key "chhands", "chhand_types" add_foreign_key "external_pauris", "pauris" + add_foreign_key "kathas", "gianis" add_foreign_key "pauri_footnotes", "pauris" add_foreign_key "pauri_translations", "pauris" add_foreign_key "pauris", "chapters" From 41e5830647d737d8377fc23a6ad0805767b9e997 Mon Sep 17 00:00:00 2001 From: Dilraj Somel Date: Sat, 3 Jun 2023 16:10:34 -0400 Subject: [PATCH 2/4] Models --- app/models/chapter.rb | 3 +++ app/models/chapter_katha.rb | 6 ++++++ app/models/giani.rb | 3 +++ app/models/katha.rb | 7 +++++++ 4 files changed, 19 insertions(+) create mode 100644 app/models/chapter_katha.rb create mode 100644 app/models/katha.rb diff --git a/app/models/chapter.rb b/app/models/chapter.rb index 382912c..00279b2 100644 --- a/app/models/chapter.rb +++ b/app/models/chapter.rb @@ -4,4 +4,7 @@ class Chapter < ApplicationRecord belongs_to :book has_many :chhands, :dependent => :destroy has_many :pauris, :dependent => :destroy + has_many :tuks, :through => :pauris + has_many :chapter_kathas + has_many :kathas, :through => :chapter_kathas end diff --git a/app/models/chapter_katha.rb b/app/models/chapter_katha.rb new file mode 100644 index 0000000..ff98243 --- /dev/null +++ b/app/models/chapter_katha.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class ChapterKatha < ApplicationRecord + belongs_to :katha + belongs_to :chapter +end diff --git a/app/models/giani.rb b/app/models/giani.rb index 00e047a..9534913 100644 --- a/app/models/giani.rb +++ b/app/models/giani.rb @@ -1,2 +1,5 @@ +# frozen_string_literal: true + class Giani < ApplicationRecord + has_many :kathas end diff --git a/app/models/katha.rb b/app/models/katha.rb new file mode 100644 index 0000000..56bc813 --- /dev/null +++ b/app/models/katha.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Katha < ApplicationRecord + belongs_to :giani + has_many :chapter_kathas + has_many :chapters, :through => :chapter_kathas +end From 75620e1eaafa3215ed176e648d9e68da45ce957a Mon Sep 17 00:00:00 2001 From: Dilraj Somel Date: Sat, 3 Jun 2023 16:26:36 -0400 Subject: [PATCH 3/4] Add `:dependent => :destroy` to `has_many` --- app/models/giani.rb | 2 +- app/models/katha.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/giani.rb b/app/models/giani.rb index 9534913..8e6c343 100644 --- a/app/models/giani.rb +++ b/app/models/giani.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Giani < ApplicationRecord - has_many :kathas + has_many :kathas, :dependent => :destroy end diff --git a/app/models/katha.rb b/app/models/katha.rb index 56bc813..eba0724 100644 --- a/app/models/katha.rb +++ b/app/models/katha.rb @@ -2,6 +2,6 @@ class Katha < ApplicationRecord belongs_to :giani - has_many :chapter_kathas - has_many :chapters, :through => :chapter_kathas + has_many :chapter_kathas, :dependent => :destroy + has_many :chapters, :through => :chapter_kathas, :dependent => :destroy end From 77a228462ca1ce7a17aac5b2ae0ca035e0bda9d0 Mon Sep 17 00:00:00 2001 From: Dilraj Somel Date: Sun, 4 Jun 2023 17:44:24 -0400 Subject: [PATCH 4/4] Linting --- app/models/chapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/chapter.rb b/app/models/chapter.rb index 00279b2..35fff97 100644 --- a/app/models/chapter.rb +++ b/app/models/chapter.rb @@ -5,6 +5,6 @@ class Chapter < ApplicationRecord has_many :chhands, :dependent => :destroy has_many :pauris, :dependent => :destroy has_many :tuks, :through => :pauris - has_many :chapter_kathas + has_many :chapter_kathas, :dependent => :destroy has_many :kathas, :through => :chapter_kathas end