mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Limit the number of category sidebar links a user can have (#26756)
This commit is contained in:
committed by
GitHub
parent
989d6f921a
commit
e0e0e0506f
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
class SidebarSection < ActiveRecord::Base
|
class SidebarSection < ActiveRecord::Base
|
||||||
MAX_TITLE_LENGTH = 30
|
MAX_TITLE_LENGTH = 30
|
||||||
|
MAX_USER_CATEGORY_LINKS = 100
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
|
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class SidebarSectionLinksUpdater
|
|||||||
delete_section_links(user: user, linkable_type: "Category")
|
delete_section_links(user: user, linkable_type: "Category")
|
||||||
else
|
else
|
||||||
category_ids = Category.where(id: category_ids).pluck(:id)
|
category_ids = Category.where(id: category_ids).pluck(:id)
|
||||||
|
category_ids = category_ids[...SidebarSection::MAX_USER_CATEGORY_LINKS]
|
||||||
update_section_links(user: user, linkable_type: "Category", new_linkable_ids: category_ids)
|
update_section_links(user: user, linkable_type: "Category", new_linkable_ids: category_ids)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ RSpec.describe SidebarSectionLinksUpdater do
|
|||||||
describe ".update_category_section_links" do
|
describe ".update_category_section_links" do
|
||||||
fab!(:public_category) { Fabricate(:category) }
|
fab!(:public_category) { Fabricate(:category) }
|
||||||
fab!(:public_category2) { Fabricate(:category) }
|
fab!(:public_category2) { Fabricate(:category) }
|
||||||
|
fab!(:public_category3) { Fabricate(:category) }
|
||||||
|
|
||||||
fab!(:user_category_section_link) do
|
fab!(:user_category_section_link) do
|
||||||
Fabricate(:category_sidebar_section_link, linkable: public_category, user: user)
|
Fabricate(:category_sidebar_section_link, linkable: public_category, user: user)
|
||||||
@@ -37,6 +38,17 @@ RSpec.describe SidebarSectionLinksUpdater do
|
|||||||
SidebarSectionLink.where(linkable_type: "Category", user: user).pluck(:linkable_id),
|
SidebarSectionLink.where(linkable_type: "Category", user: user).pluck(:linkable_id),
|
||||||
).to contain_exactly(public_category.id, public_category2.id)
|
).to contain_exactly(public_category.id, public_category2.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "limits the number of category links a user can have" do
|
||||||
|
stub_const(SidebarSection, :MAX_USER_CATEGORY_LINKS, 2) do
|
||||||
|
described_class.update_category_section_links(
|
||||||
|
user,
|
||||||
|
category_ids: [public_category.id, public_category2.id, public_category3.id],
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(SidebarSectionLink.where(linkable_type: "Category", user: user).count).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".update_tag_section_links" do
|
describe ".update_tag_section_links" do
|
||||||
|
|||||||
Reference in New Issue
Block a user