Skip to content

Commit 78c1f6e

Browse files
authored
Merge pull request #1727 from codidact/0valt/pinned_links
Fixes for featured (pinned) links
2 parents 0cf65e1 + e8b5f5f commit 78c1f6e

File tree

3 files changed

+111
-26
lines changed

3 files changed

+111
-26
lines changed

app/controllers/pinned_links_controller.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,42 @@ def new
2727
end
2828

2929
def create
30-
@link = PinnedLink.create pinned_link_params
30+
@link = PinnedLink.new(pinned_link_params)
3131

32-
attr = @link.attributes_print
33-
AuditLog.moderator_audit(event_type: 'pinned_link_create', related: @link, user: current_user,
34-
comment: "<<PinnedLink #{attr}>>")
32+
if @link.save
33+
attr = @link.attributes_print
3534

36-
flash[:success] = 'Your pinned link has been created. Due to caching, it may take some time until it is shown.'
37-
redirect_to pinned_links_path
35+
AuditLog.moderator_audit(event_type: 'pinned_link_create',
36+
related: @link,
37+
user: current_user,
38+
comment: "<<PinnedLink #{attr}>>")
39+
40+
flash[:success] =
41+
'Your pinned link has been created. Due to caching, it may take some time until it is shown.'
42+
redirect_to pinned_links_path
43+
else
44+
render 'pinned_links/new', status: :bad_request
45+
end
3846
end
3947

4048
def edit; end
4149

4250
def update
4351
before = @link.attributes_print
44-
@link.update pinned_link_params
45-
after = @link.attributes_print
46-
AuditLog.moderator_audit(event_type: 'pinned_link_update', related: @link, user: current_user,
47-
comment: "from <<PinnedLink #{before}>>\nto <<PinnedLink #{after}>>")
4852

49-
flash[:success] = 'The pinned link has been updated. Due to caching, it may take some time until it is shown.'
50-
redirect_to pinned_links_path
53+
if @link.update(pinned_link_params)
54+
after = @link.attributes_print
55+
56+
AuditLog.moderator_audit(event_type: 'pinned_link_update',
57+
related: @link,
58+
user: current_user,
59+
comment: "from <<PinnedLink #{before}>>\nto <<PinnedLink #{after}>>")
60+
61+
flash[:success] = 'The pinned link has been updated. Due to caching, it may take some time until it is shown.'
62+
redirect_to pinned_links_path
63+
else
64+
render 'pinned_links/edit', status: :bad_request
65+
end
5166
end
5267

5368
private

app/models/pinned_link.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
class PinnedLink < ApplicationRecord
22
include MaybeCommunityRelated
3-
belongs_to :post
3+
belongs_to :post, optional: true
4+
5+
validate :check_post_or_url
46

57
# Is the link time-constrained?
68
# @return [Boolean] check result
79
def timed?
810
shown_before.present? || shown_after.present?
911
end
12+
13+
def check_post_or_url
14+
unless post_id.present? || link.present?
15+
errors.add(:base, 'either a post or a URL must be set')
16+
end
17+
end
1018
end

test/controllers/pinned_links_controller_test.rb

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,93 @@
33
class PinnedLinksControllerTest < ActionController::TestCase
44
include Devise::Test::ControllerHelpers
55

6-
test 'edit should require moderator' do
7-
sign_in users(:standard_user)
8-
get :edit, params: { id: pinned_links(:active_with_label).id }
9-
assert_response(:not_found)
6+
test 'only mods or higher should be able to create pinned links' do
7+
post = posts(:question_one)
8+
9+
users.each do |user|
10+
sign_in user
11+
try_create_pinned_link(post: post)
12+
assert_response(user.at_least_moderator? ? :found : :not_found)
13+
end
14+
end
15+
16+
test 'only mods or higher should be able to edit pinned links' do
17+
link = pinned_links(:active_with_label)
18+
19+
users.each do |user|
20+
sign_in user
21+
try_edit_pinned_link(link)
22+
assert_response(user.at_least_moderator? ? :success : :not_found)
23+
end
24+
end
25+
26+
test 'only mods or higher should be able to update pinned links' do
27+
link = pinned_links(:active_with_label)
28+
29+
users.each do |user|
30+
sign_in user
31+
try_update_pinned_link(link, label: 'updated label')
32+
assert_response(user.at_least_moderator? ? :found : :not_found)
33+
end
1034
end
1135

12-
test 'edit should work for moderators' do
36+
test 'create should correctly create pinned links' do
1337
sign_in users(:moderator)
14-
get :edit, params: { id: pinned_links(:active_with_label).id }
15-
assert_response(:success)
38+
39+
try_create_pinned_link(post: posts(:question_one))
40+
41+
assert_response(:found)
42+
assert_redirected_to pinned_links_path
1643
assert_not_nil assigns(:link)
1744
end
1845

19-
test 'update should require moderator' do
20-
sign_in users(:standard_user)
21-
post :update, params: { id: pinned_links(:active_with_label).id, pinned_link: { label: 'updated label' } }
22-
assert_response(:not_found)
46+
test 'create should correctly handle invalid pinned links' do
47+
sign_in users(:moderator)
48+
try_create_pinned_link
49+
assert_response(:bad_request)
50+
assert assigns(:link)&.errors&.any?
2351
end
2452

25-
test 'update should work for moderators' do
53+
test 'update should correctly update pinned links' do
2654
sign_in users(:moderator)
27-
post :update, params: { id: pinned_links(:active_with_label).id, pinned_link: { label: 'updated label' } }
55+
56+
try_update_pinned_link(pinned_links(:active_with_label), label: 'updated label')
57+
2858
assert_response(:found)
2959
assert_redirected_to pinned_links_path
3060
assert_not_nil assigns(:link)
3161
assert_equal 'updated label', assigns(:link).label
3262
end
63+
64+
test 'update should correctly handle invlid pinned links' do
65+
sign_in users(:moderator)
66+
try_update_pinned_link(pinned_links(:active_with_label), link: nil)
67+
assert_response(:bad_request)
68+
assert assigns(:link)&.errors&.any?
69+
end
70+
71+
private
72+
73+
def try_create_pinned_link(**opts)
74+
community_id = opts.delete(:community)&.id
75+
post_id = opts.delete(:post)&.id
76+
77+
post :create, params: {
78+
pinned_link: {
79+
community_id: community_id,
80+
post_id: post_id
81+
}.merge(opts)
82+
}
83+
end
84+
85+
def try_edit_pinned_link(link)
86+
get :edit, params: { id: link.id }
87+
end
88+
89+
def try_update_pinned_link(link, **opts)
90+
post :update, params: {
91+
id: link.id,
92+
pinned_link: opts
93+
}
94+
end
3395
end

0 commit comments

Comments
 (0)