From c2baa86c09a56a267b166a004a6dca7d332a99d9 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 29 Jan 2023 00:56:47 +0100 Subject: [PATCH] Add `pinned_at` to answers --- app/models/answer.rb | 4 ++++ db/migrate/20230128233136_add_pinned_at_to_answers.rb | 8 ++++++++ db/schema.rb | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 db/migrate/20230128233136_add_pinned_at_to_answers.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index 91a6c956..d1e3e645 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -14,6 +14,8 @@ class Answer < ApplicationRecord validates :question_id, uniqueness: { scope: :user_id } # rubocop:enable Rails/UniqueValidationWithoutIndex + scope :pinned, -> { where.not(pinned_at: nil) } + SHORT_ANSWER_MAX_LENGTH = 640 # rubocop:disable Rails/SkipsModelValidations @@ -56,4 +58,6 @@ class Answer < ApplicationRecord end def long? = content.length > SHORT_ANSWER_MAX_LENGTH + + def pinned? = pinned_at.present? end diff --git a/db/migrate/20230128233136_add_pinned_at_to_answers.rb b/db/migrate/20230128233136_add_pinned_at_to_answers.rb new file mode 100644 index 00000000..8501ba1e --- /dev/null +++ b/db/migrate/20230128233136_add_pinned_at_to_answers.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddPinnedAtToAnswers < ActiveRecord::Migration[6.1] + def change + add_column :answers, :pinned_at, :timestamp + add_index :answers, %i[user_id pinned_at] + end +end diff --git a/db/schema.rb b/db/schema.rb index 0d70a9eb..33418a33 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -48,8 +48,10 @@ ActiveRecord::Schema.define(version: 2023_02_12_181044) do t.datetime "created_at" t.datetime "updated_at" t.integer "smile_count", default: 0, null: false + t.datetime "pinned_at" t.index ["question_id"], name: "index_answers_on_question_id" t.index ["user_id", "created_at"], name: "index_answers_on_user_id_and_created_at" + t.index ["user_id", "pinned_at"], name: "index_answers_on_user_id_and_pinned_at" end create_table "appendables", force: :cascade do |t|