Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/draft_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def update # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
when :write
@post.assign_attributes(post_params)
@post.slug = nil # Resets FriendlyID
@post.save!(:validate => false)
@post.save!(context: :draft)

unless params[:commit] == 'review'
respond_to do |format|
Expand Down
10 changes: 8 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class Post < ApplicationRecord

has_rich_text :body

validates :subject, presence: true, length: { maximum: 255 }
validate :body_cannot_be_empty
# Presence validations only apply when publishing (not in :draft context)
validates :subject, presence: true, unless: :draft_context?
validates :subject, length: { maximum: 255 }
validate :body_cannot_be_empty, unless: :draft_context?

default_scope { where(archived: false).order(published_at: :desc, updated_at: :desc) }
scope :published, lambda {
Expand Down Expand Up @@ -68,6 +70,10 @@ def body_cannot_be_empty
errors.add(:body, "can't be empty") if body.blank?
end

def draft_context?
validation_context == :draft
end

def remove_pinned_by
pinned_by&.update(pinned_post: nil)
end
Expand Down