Skip to content

Commit

Permalink
Add validation to post status enum; remove redundant validation and c…
Browse files Browse the repository at this point in the history
…ustom setter method
  • Loading branch information
binos30 committed Feb 2, 2025
1 parent 0bc3a99 commit 9242630
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Post < ApplicationRecord
extend FriendlyId
friendly_id :title

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

belongs_to :user, inverse_of: :posts

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

scope :public_status,
Expand Down Expand Up @@ -51,14 +50,6 @@ class Post < ApplicationRecord
end
end

# Overwrite the setter to rely on validations instead of [ArgumentError]
# https://github.com/rails/rails/issues/13971#issuecomment-721821257
def status=(value)
self[:status] = value
rescue ArgumentError
self[:status] = nil
end

# Whether to generate a new slug.
def should_generate_new_friendly_id?
slug.blank? || title_changed?
Expand Down

0 comments on commit 9242630

Please # to comment.