From 49b11f2864f75bcfb8d0d01439939ed68aa90b8f Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 2 Jul 2018 18:03:37 -0400 Subject: Collapsed Entry -> Blog single-table inheritance to Blog --- app/controllers/blogs_controller.rb | 7 ++++ app/controllers/entries_controller.rb | 7 ---- app/models/blog.rb | 12 +++++- app/models/entry.rb | 11 ------ app/views/blogs/show.html.haml | 2 + app/views/entries/show.html.haml | 2 - app/views/records/index.html.haml | 2 +- config/routes.rb | 2 +- .../20180702214240_rename_entries_to_blogs.rb | 24 ++++++++++++ db/schema.rb | 45 +++++++++++----------- 10 files changed, 67 insertions(+), 47 deletions(-) create mode 100644 app/controllers/blogs_controller.rb delete mode 100644 app/controllers/entries_controller.rb delete mode 100644 app/models/entry.rb create mode 100644 app/views/blogs/show.html.haml delete mode 100644 app/views/entries/show.html.haml create mode 100644 db/migrate/20180702214240_rename_entries_to_blogs.rb diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb new file mode 100644 index 0000000..5e72601 --- /dev/null +++ b/app/controllers/blogs_controller.rb @@ -0,0 +1,7 @@ +class BlogsController < ApplicationController + + def show + @blog = Blog.find_by_slug(params[:slug]) + end + +end diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb deleted file mode 100644 index 14d779a..0000000 --- a/app/controllers/entries_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class EntriesController < ApplicationController - - def show - @entry = Entry.find_by_slug(params[:slug]) - end - -end diff --git a/app/models/blog.rb b/app/models/blog.rb index d2b1c27..1ace11b 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -1,4 +1,12 @@ -class Blog < Entry +class Blog < ApplicationRecord + has_many :records, as: :recordable, inverse_of: :recordable + validates :title, :body, presence: true -end + validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ + accepts_nested_attributes_for :records, allow_destroy: true + + def path + "/says/#{slug}" + end +end diff --git a/app/models/entry.rb b/app/models/entry.rb deleted file mode 100644 index 87fd46d..0000000 --- a/app/models/entry.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Entry < ApplicationRecord - has_many :records, as: :recordable, inverse_of: :recordable - - validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ - - accepts_nested_attributes_for :records, allow_destroy: true - - def path - "/says/#{slug}" - end -end diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml new file mode 100644 index 0000000..51d4aa0 --- /dev/null +++ b/app/views/blogs/show.html.haml @@ -0,0 +1,2 @@ +.breadcrumb= link_to "← Back to home page", root_path += render @blog diff --git a/app/views/entries/show.html.haml b/app/views/entries/show.html.haml deleted file mode 100644 index 3a9337c..0000000 --- a/app/views/entries/show.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -.breadcrumb= link_to "← Back to home page", root_path -= render @entry diff --git a/app/views/records/index.html.haml b/app/views/records/index.html.haml index 3850d72..200321e 100644 --- a/app/views/records/index.html.haml +++ b/app/views/records/index.html.haml @@ -4,4 +4,4 @@ %span.description= link_to record.description, record.recordable.path %ul.tags %li.record-date= record.created_at.strftime("%m.%d.%y") - %li.entry-type{ class: "entry-type-#{record.recordable.type.downcase}" }= record.recordable.type + %li.entry-type{ class: "entry-type-#{record.recordable_type.downcase}" }= record.recordable_type diff --git a/config/routes.rb b/config/routes.rb index 1216b85..cf0bdd6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ Rails.application.routes.draw do root "records#index" - get 'says/:slug', to: 'entries#show' + get 'says/:slug', to: 'blogs#show' mount Pokeviewer::Engine => '/poke3' end diff --git a/db/migrate/20180702214240_rename_entries_to_blogs.rb b/db/migrate/20180702214240_rename_entries_to_blogs.rb new file mode 100644 index 0000000..eac2e6a --- /dev/null +++ b/db/migrate/20180702214240_rename_entries_to_blogs.rb @@ -0,0 +1,24 @@ +class RenameEntriesToBlogs < ActiveRecord::Migration[5.1] + def up + rename_table :entries, :blogs + + remove_column :blogs, :type + + Record.where(recordable_type: "Entry").each do |r| + r.recordable_type = "Blog" + r.save! + end + end + + def down + rename_table :blogs, :entries + + add_column :entries, :type, :string, null: false, default: "Blog" + change_column_default :entries, :type, nil + + Record.where(recordable_type: "Blog").each do |r| + r.recordable_type = "Entry" + r.save! + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 868f6f6..e036b75 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180130021851) do +ActiveRecord::Schema.define(version: 20180702214240) do - create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| + t.string "title" + t.text "body" + t.string "slug", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "data_file_name", null: false t.string "data_content_type" t.integer "data_file_size" @@ -25,16 +33,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["type"], name: "index_ckeditor_assets_on_type" end - create_table "entries", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| - t.string "title" - t.text "body" - t.string "slug", null: false - t.string "type", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "pokeviewer_abilities", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_abilities", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.string "description", null: false t.datetime "created_at", null: false @@ -42,13 +41,13 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["name"], name: "index_pokeviewer_abilities_on_name", unique: true end - create_table "pokeviewer_gift_ribbons", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_gift_ribbons", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "description", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "pokeviewer_items", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_items", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.boolean "tm", default: false, null: false t.integer "move_id" @@ -60,13 +59,13 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["move_id"], name: "index_pokeviewer_items_on_move_id" end - create_table "pokeviewer_locations", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_locations", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "pokeviewer_moves", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_moves", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.integer "pp", null: false t.datetime "created_at", null: false @@ -78,7 +77,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["name"], name: "index_pokeviewer_moves_on_name", unique: true end - create_table "pokeviewer_pokedex_entries", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_pokedex_entries", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.bigint "trainer_id" t.bigint "species_id" t.boolean "caught", default: false @@ -89,7 +88,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["trainer_id"], name: "index_pokeviewer_pokedex_entries_on_trainer_id" end - create_table "pokeviewer_pokemon", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_pokemon", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "uuid", null: false t.integer "trainer_id" t.string "key" @@ -116,7 +115,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["uuid"], name: "index_pokeviewer_pokemon_on_uuid", unique: true end - create_table "pokeviewer_revisions", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_revisions", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.integer "pokemon_id", null: false t.integer "sequential_id", null: false t.string "nickname", null: false @@ -172,7 +171,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["species_id"], name: "index_pokeviewer_revisions_on_species_id" end - create_table "pokeviewer_species", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_species", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -183,7 +182,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["name"], name: "index_pokeviewer_species_on_name", unique: true end - create_table "pokeviewer_trainers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "pokeviewer_trainers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "game", null: false t.string "name", null: false t.integer "number", null: false @@ -220,7 +219,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["world_ribbon_id"], name: "index_pokeviewer_trainers_on_world_ribbon_id" end - create_table "records", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "records", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.text "description" t.string "recordable_type" t.integer "recordable_id" @@ -229,7 +228,7 @@ ActiveRecord::Schema.define(version: 20180130021851) do t.index ["recordable_type", "recordable_id"], name: "index_records_on_recordable_type_and_recordable_id" end - create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "login", default: "", null: false t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false -- cgit 1.4.1