Add PostStatus model

This commit is contained in:
riggraz
2019-08-24 12:06:48 +02:00
parent 8e610aa381
commit 726236b8aa
6 changed files with 71 additions and 1 deletions

View File

@@ -0,0 +1,4 @@
class PostStatus < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :color, format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/ }
end

View File

@@ -0,0 +1,10 @@
class CreatePostStatuses < ActiveRecord::Migration[6.0]
def change
create_table :post_statuses do |t|
t.string :name
t.string :color
t.timestamps
end
end
end

View File

@@ -0,0 +1,5 @@
class AddUniqueToNameOfPostStatuses < ActiveRecord::Migration[6.0]
def change
add_index :post_statuses, :name, unique: true
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_08_22_141109) do
ActiveRecord::Schema.define(version: 2019_08_24_093755) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -23,6 +23,14 @@ ActiveRecord::Schema.define(version: 2019_08_22_141109) do
t.index ["name"], name: "index_boards_on_name", unique: true
end
create_table "post_statuses", force: :cascade do |t|
t.string "name"
t.string "color"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["name"], name: "index_post_statuses_on_name", unique: true
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false

View File

@@ -0,0 +1,6 @@
FactoryBot.define do
factory :post_status do
sequence(:name) { |n| "Post Status #{n}" }
color { "#ffffff" }
end
end

View File

@@ -0,0 +1,37 @@
require 'rails_helper'
RSpec.describe PostStatus, type: :model do
let(:post_status) { FactoryBot.create(:post_status) }
it 'must have a name' do
emptyname = FactoryBot.build(:post_status, name: "")
nilname = FactoryBot.build(:post_status, name: nil)
expect(emptyname.valid?).to be_falsy
expect(nilname.valid?).to be_falsy
expect(post_status.valid?).to be_truthy
end
it 'has a unique name' do
post_status2 = FactoryBot.build(:post_status, name: post_status.name)
expect(post_status2.valid?).to be_falsy
expect(post_status.valid?).to be_truthy
end
it 'has a valid hex color' do
nilcolor = FactoryBot.build(:post_status, color: nil)
emptycolor = FactoryBot.build(:post_status, color: "")
invalidcolor = FactoryBot.build(:post_status, color: "ffffff")
invalidcolor2 = FactoryBot.build(:post_status, color: "#ffff")
validcolor = FactoryBot.build(:post_status, color: "#fff")
validcolor2 = FactoryBot.build(:post_status, color: "#ffffff")
expect(nilcolor.valid?).to be_falsy
expect(emptycolor.valid?).to be_falsy
expect(invalidcolor.valid?).to be_falsy
expect(invalidcolor2.valid?).to be_falsy
expect(validcolor.valid?).to be_truthy
expect(validcolor2.valid?).to be_truthy
end
end