Skip to content
Merged
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
9 changes: 0 additions & 9 deletions lib/bunko/models/post_methods/publishable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module Publishable

# Callbacks
before_validation :set_published_at, if: :should_set_published_at?
validate :validate_status_value

# Scopes
scope :published, -> { where(status: "published").where("published_at <= ?", Time.current).order(published_at: :desc) }
Expand All @@ -37,14 +36,6 @@ def should_set_published_at?
def set_published_at
self.published_at = Time.current
end

def validate_status_value
return if status.blank?

unless Bunko.configuration.valid_statuses.include?(status)
raise ArgumentError, "#{status} is not a valid status"
end
end
end
end
end
Expand Down
36 changes: 18 additions & 18 deletions test/models/post_publishing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ class PostPublishingTest < ActiveSupport::TestCase
post_type: @blog_type
)

assert_raises(ArgumentError) do
assert_raises(ActiveRecord::RecordInvalid) do
post.update!(status: "invalid_status")
end

refute post.update(status: "invalid_status")
assert_includes post.errors[:status], "invalid_status is not a valid status"
end

test "posts with published status but future published_at are treated as scheduled" do
Expand Down Expand Up @@ -109,28 +112,25 @@ class PostPublishingTest < ActiveSupport::TestCase
refute post.send(:should_set_published_at?)
end

test "validate_status_value raises error for invalid status" do
post = Post.new(status: "invalid", post_type: @blog_type)

error = assert_raises(ArgumentError) do
post.send(:validate_status_value)
end
test "invalid status adds a validation error instead of raising" do
post = Post.new(title: "Test", content: "Content", status: "invalid", post_type: @blog_type)

assert_match(/invalid is not a valid status/, error.message)
refute post.valid?
assert_includes post.errors[:status], "invalid is not a valid status"
end

test "validate_status_value does not raise error for valid status" do
post = Post.new(status: "published", post_type: @blog_type)
assert_nothing_raised do
post.send(:validate_status_value)
end
test "valid status produces no status errors" do
post = Post.new(title: "Test", content: "Content", status: "published", post_type: @blog_type)

post.valid?
assert_empty post.errors[:status]
end

test "validate_status_value returns early if status is blank" do
post = Post.new(post_type: @blog_type)
assert_nothing_raised do
post.send(:validate_status_value)
end
test "blank status adds a presence error instead of raising" do
post = Post.new(title: "Test", content: "Content", status: nil, post_type: @blog_type)

refute post.valid?
assert_includes post.errors[:status], "can't be blank"
end

test "scheduled? returns true for published posts with future published_at" do
Expand Down
Loading