Add response templates model and table (#6615)
* Add response templates model and table * Add missing factory oops * Remove response templates on delete * Remove moderator_id column from comments for now * Use constants and add spec for response templates
This commit is contained in:
parent
079ec96d99
commit
d08f830d86
7 changed files with 82 additions and 1 deletions
17
app/models/response_template.rb
Normal file
17
app/models/response_template.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class ResponseTemplate < ApplicationRecord
|
||||
belongs_to :user, optional: true
|
||||
|
||||
UNIQUENESS_SCOPE = %i[user_id type_of content_type].freeze
|
||||
TYPE_OF_TYPES = %w[personal_comment mod_comment abuse_report_reply email_reply].freeze
|
||||
CONTENT_TYPES = %w[plain_text html body_markdown].freeze
|
||||
COMMENT_CONTENT_TYPE = %w[body_markdown].freeze
|
||||
COMMENT_VALIDATION_MSG = "Comment templates must use Markdown as its content type.".freeze
|
||||
|
||||
validates :type_of, :content_type, :content, :title, presence: true
|
||||
validates :content, uniqueness: { scope: UNIQUENESS_SCOPE }
|
||||
validates :type_of, inclusion: { in: TYPE_OF_TYPES }
|
||||
validates :content_type, inclusion: { in: CONTENT_TYPES }
|
||||
validates :content_type,
|
||||
inclusion: { in: COMMENT_CONTENT_TYPE, message: COMMENT_VALIDATION_MSG },
|
||||
if: -> { type_of&.include?("comment") }
|
||||
end
|
||||
|
|
@ -48,6 +48,7 @@ class User < ApplicationRecord
|
|||
has_many :affected_feedback_messages, class_name: "FeedbackMessage", inverse_of: :affected, foreign_key: :affected_id, dependent: :nullify
|
||||
|
||||
has_many :rating_votes, dependent: :destroy
|
||||
has_many :response_templates, foreign_key: :user_id, inverse_of: :user, dependent: :destroy
|
||||
has_many :html_variants, dependent: :destroy
|
||||
has_many :page_views, dependent: :destroy
|
||||
has_many :credits, dependent: :destroy
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ module Users
|
|||
user.pro_membership.delete if user.pro_membership.present?
|
||||
user.profile_pins.delete_all
|
||||
user.rating_votes.delete_all
|
||||
user.response_templates.delete_all
|
||||
user.tweets.delete_all
|
||||
user.classified_listings.destroy_all
|
||||
|
||||
|
|
|
|||
15
db/migrate/20200311170959_create_response_templates_table.rb
Normal file
15
db/migrate/20200311170959_create_response_templates_table.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class CreateResponseTemplatesTable < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :response_templates do |t|
|
||||
t.string "type_of", null: false # abuse_report_reply, mod_comment, email_reply, personal_comment
|
||||
t.string "content_type", null: false # body_markdown, plain_text, html
|
||||
t.text "content", null: false
|
||||
t.string "title", null: false
|
||||
t.references :user, foreign_key: true # allow null for app wide usage of templates
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
add_index :response_templates, :type_of
|
||||
end
|
||||
end
|
||||
15
db/schema.rb
15
db/schema.rb
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_03_08_144606) do
|
||||
ActiveRecord::Schema.define(version: 2020_03_11_170959) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
|
@ -960,6 +960,18 @@ ActiveRecord::Schema.define(version: 2020_03_08_144606) do
|
|||
t.index ["user_id"], name: "index_reactions_on_user_id"
|
||||
end
|
||||
|
||||
create_table "response_templates", force: :cascade do |t|
|
||||
t.text "content", null: false
|
||||
t.string "content_type", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.string "title", null: false
|
||||
t.string "type_of", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.bigint "user_id"
|
||||
t.index ["type_of"], name: "index_response_templates_on_type_of"
|
||||
t.index ["user_id"], name: "index_response_templates_on_user_id"
|
||||
end
|
||||
|
||||
create_table "roles", id: :serial, force: :cascade do |t|
|
||||
t.datetime "created_at"
|
||||
t.string "name"
|
||||
|
|
@ -1303,6 +1315,7 @@ ActiveRecord::Schema.define(version: 2020_03_08_144606) do
|
|||
add_foreign_key "oauth_access_tokens", "users", column: "resource_owner_id"
|
||||
add_foreign_key "page_views", "articles", on_delete: :cascade
|
||||
add_foreign_key "podcasts", "users", column: "creator_id"
|
||||
add_foreign_key "response_templates", "users"
|
||||
add_foreign_key "sponsorships", "organizations"
|
||||
add_foreign_key "sponsorships", "users"
|
||||
add_foreign_key "tag_adjustments", "articles", on_delete: :cascade
|
||||
|
|
|
|||
10
spec/factories/response_templates.rb
Normal file
10
spec/factories/response_templates.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
FactoryBot.define do
|
||||
factory :response_template do
|
||||
sequence(:content) { |n| "#{Faker::Lorem.sentence}#{n}" }
|
||||
|
||||
user
|
||||
type_of { "personal_comment" }
|
||||
content_type { "body_markdown" }
|
||||
title { generate :title }
|
||||
end
|
||||
end
|
||||
24
spec/models/response_template_spec.rb
Normal file
24
spec/models/response_template_spec.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ResponseTemplate, type: :model do
|
||||
it { is_expected.to validate_inclusion_of(:type_of).in_array(ResponseTemplate::TYPE_OF_TYPES) }
|
||||
it { is_expected.to validate_inclusion_of(:content_type).in_array(ResponseTemplate::CONTENT_TYPES) }
|
||||
|
||||
describe "comment content type validation" do
|
||||
context "when the type of is a personal comment" do
|
||||
it "validates that the content type is body markdown" do
|
||||
response_template = build(:response_template, type_of: "personal_comment", content_type: "html")
|
||||
expect(response_template.valid?).to eq false
|
||||
expect(response_template.errors.messages[:content_type].to_sentence).to eq ResponseTemplate::COMMENT_VALIDATION_MSG
|
||||
end
|
||||
end
|
||||
|
||||
context "when the type of is a mod comment" do
|
||||
it "validates that the content type is body markdown" do
|
||||
response_template = build(:response_template, type_of: "mod_comment", content_type: "html")
|
||||
expect(response_template.valid?).to eq false
|
||||
expect(response_template.errors.messages[:content_type].to_sentence).to eq ResponseTemplate::COMMENT_VALIDATION_MSG
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue