Skip to content

Commit e2d67b3

Browse files
committed
pinned links should validate that their end time is not before start time
1 parent e1a1583 commit e2d67b3

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

app/models/pinned_link.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class PinnedLink < ApplicationRecord
2929
before.or(after)
3030
}
3131

32+
validate :check_period
3233
validate :check_post_or_url
3334

3435
# Is the link not timed or started in the past & hasn't ended yet?
@@ -58,6 +59,14 @@ def timed?
5859
shown_before.present? || shown_after.present?
5960
end
6061

62+
def check_period
63+
return unless shown_before.present? && shown_after.present?
64+
65+
if shown_before < shown_after
66+
errors.add(:base, 'end date cannot be earlier than the start date')
67+
end
68+
end
69+
6170
def check_post_or_url
6271
unless post_id.present? || link.present?
6372
errors.add(:base, 'either a post or a URL must be set')

test/models/pinned_link_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@ class PinnedLinkTest < ActiveSupport::TestCase
3939
assert link.future?(now)
4040
end
4141
end
42+
43+
test 'pinned links should be correctly validated' do
44+
valid_with_link = PinnedLink.new(link: 'https://example.com')
45+
valid_with_post = PinnedLink.new(post: posts(:question_one))
46+
period_mismatch = PinnedLink.new(shown_before: DateTime.now - 1, shown_after: DateTime.now)
47+
48+
[
49+
[valid_with_link, true],
50+
[valid_with_post, true],
51+
[period_mismatch, false]
52+
].each do |test|
53+
link, status = test
54+
assert_equal status, link.valid?
55+
end
56+
end
4257
end

0 commit comments

Comments
 (0)