Skip to content

Commit

Permalink
enums as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Aug 23, 2021
1 parent c8d7257 commit 4ca2195
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 14 deletions.
12 changes: 1 addition & 11 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
class Post < ApplicationRecord
validates :title, presence: true

STATUSES = [:draft, :published, :banned]

validates :status, inclusion: { in: Post::STATUSES }

scope :draft, -> { where(status: 'draft') }
scope :published, -> { where(status: 'published') }
scope :banned, -> { where(status: 'banned') }

def banned?
status == 'banned'
end
enum status: { draft: 0, published: 1, banned: 13, in_review: 534 }

def to_s
title
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<div class="field">
<%= form.label :status %>
<%= form.select :status, Post::STATUSES %>
<%= form.select :status, Post.statuses.keys %>
</div>

<div class="field">
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20210823212301_add_status_to_posts.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddStatusToPosts < ActiveRecord::Migration[6.1]
def change
add_column :posts, :status, :string, default: 'draft'
add_column :posts, :status, :integer
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ca2195

Please sign in to comment.