Skip to content

Commit afcb387

Browse files
committed
Use Rails enum, backed by an integer column with CHECK constraint
1 parent 6cf5b6e commit afcb387

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

app/models/event.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Event < ActiveRecord::Base
2-
ACTIONS = ['create', 'update'].freeze
2+
enum action: { created: 0, updated: 1 }
33

44
belongs_to :post
55
belongs_to :member
66
belongs_to :transfer
77

8-
validates :action, inclusion: { in: ACTIONS }, presence: true
8+
validates :action, presence: true
99
validate :resource_presence
1010

1111
private

db/migrate/20180501093846_create_events.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
class CreateEvents < ActiveRecord::Migration
1010
def up
1111
execute <<-SQL
12-
CREATE TYPE action_enum AS ENUM ('create', 'update');
13-
1412
CREATE TABLE events (
1513
id serial PRIMARY KEY,
16-
action action_enum NOT NULL,
14+
action integer NOT NULL,
1715
post_id integer REFERENCES posts,
1816
member_id integer REFERENCES members,
1917
transfer_id integer REFERENCES transfers,
2018
created_at timestamp without time zone,
2119
updated_at timestamp without time zone,
20+
CHECK(action IN (0, 1)),
2221
CHECK(
2322
(
2423
(post_id IS NOT NULL)::integer +

db/schema.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,18 @@
7676
add_index "documents", ["documentable_id", "documentable_type"], name: "index_documents_on_documentable_id_and_documentable_type", using: :btree
7777
add_index "documents", ["label"], name: "index_documents_on_label", using: :btree
7878

79-
# Could not dump table "events" because of following StandardError
80-
# Unknown type 'action_enum' for column 'action'
79+
create_table "events", force: :cascade do |t|
80+
t.integer "action", null: false
81+
t.integer "post_id"
82+
t.integer "member_id"
83+
t.integer "transfer_id"
84+
t.datetime "created_at"
85+
t.datetime "updated_at"
86+
end
87+
88+
add_index "events", ["member_id"], name: "events_member_id_idx", unique: true, where: "(member_id IS NOT NULL)", using: :btree
89+
add_index "events", ["post_id"], name: "events_post_id_idx", unique: true, where: "(post_id IS NOT NULL)", using: :btree
90+
add_index "events", ["transfer_id"], name: "events_transfer_id_idx", unique: true, where: "(transfer_id IS NOT NULL)", using: :btree
8191

8292
create_table "members", force: :cascade do |t|
8393
t.integer "user_id"

spec/models/event_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
require "spec_helper"
1+
require 'spec_helper'
22

33
describe Event do
44
describe 'Validations' do
55
it { is_expected.to validate_presence_of(:action) }
6-
it { is_expected.to validate_inclusion_of(:action).in_array(::Event::ACTIONS) }
6+
it do
7+
is_expected.to define_enum_for(:action)
8+
.with([:created, :updated])
9+
end
710

811
describe '#resource_presence validation' do
912
let(:post) { Fabricate(:post) }
1013
let(:member) { Fabricate(:member) }
11-
let(:event) { Event.new action: 'create' }
14+
let(:event) { Event.new action: 'created' }
1215

1316
context 'has no resources' do
1417
it { expect(event).to_not be_valid }

0 commit comments

Comments
 (0)