File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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' )
Original file line number Diff line number Diff 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
4257end
You can’t perform that action at this time.
0 commit comments