Skip to content

Commit 9242630

Browse files
committed
Add validation to post status enum; remove redundant validation and custom setter method
1 parent 0bc3a99 commit 9242630

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

app/models/post.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Post < ApplicationRecord
44
extend FriendlyId
55
friendly_id :title
66

7-
enum :status, { public: 0, private: 1, archived: 2 }, default: :public, suffix: true
7+
enum :status, { public: 0, private: 1, archived: 2 }, validate: true, default: :public, suffix: true
88

99
belongs_to :user, inverse_of: :posts
1010

@@ -20,7 +20,6 @@ class Post < ApplicationRecord
2020
case_sensitive: false
2121
},
2222
format: %r{\A[a-zA-Z0-9\s\-\[\]/*&;,._:()!?ñÑ]*\z}
23-
validates :status, inclusion: { in: statuses.keys }
2423
validates :content, presence: true
2524

2625
scope :public_status,
@@ -51,14 +50,6 @@ class Post < ApplicationRecord
5150
end
5251
end
5352

54-
# Overwrite the setter to rely on validations instead of [ArgumentError]
55-
# https://github.com/rails/rails/issues/13971#issuecomment-721821257
56-
def status=(value)
57-
self[:status] = value
58-
rescue ArgumentError
59-
self[:status] = nil
60-
end
61-
6253
# Whether to generate a new slug.
6354
def should_generate_new_friendly_id?
6455
slug.blank? || title_changed?

0 commit comments

Comments
 (0)