A platform for community discussion. Free, open, simple.
DEV: Limit the number of category sidebar links a user can have (#26756)
Daniel Waterworthe0e0e0506fc3
diff --git a/app/models/sidebar_section.rb b/app/models/sidebar_section.rbindex 3b564c3faf0..24a3cd568f9 100644--- a/app/models/sidebar_section.rb+++ b/app/models/sidebar_section.rb@@ -2,6 +2,7 @@class SidebarSection < ActiveRecord::BaseMAX_TITLE_LENGTH = 30+ MAX_USER_CATEGORY_LINKS = 100belongs_to :userhas_many :sidebar_section_links, -> { order("position") }, dependent: :destroydiff --git a/app/services/sidebar_section_links_updater.rb b/app/services/sidebar_section_links_updater.rbindex 682372aafb3..0f3d9363162 100644--- a/app/services/sidebar_section_links_updater.rb+++ b/app/services/sidebar_section_links_updater.rb@@ -6,6 +6,7 @@ class SidebarSectionLinksUpdaterdelete_section_links(user: user, linkable_type: "Category")elsecategory_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)endenddiff --git a/spec/lib/sidebar_section_links_updater_spec.rb b/spec/lib/sidebar_section_links_updater_spec.rbindex 6bb8ad3aa0f..18f90db2201 100644--- a/spec/lib/sidebar_section_links_updater_spec.rb+++ b/spec/lib/sidebar_section_links_updater_spec.rb@@ -7,6 +7,7 @@ RSpec.describe SidebarSectionLinksUpdater dodescribe ".update_category_section_links" dofab!(:public_category) { Fabricate(:category) }fab!(:public_category2) { Fabricate(:category) }+ fab!(:public_category3) { Fabricate(:category) }fab!(:user_category_section_link) doFabricate(:category_sidebar_section_link, linkable: public_category, user: user)@@ -37,6 +38,17 @@ RSpec.describe SidebarSectionLinksUpdater doSidebarSectionLink.where(linkable_type: "Category", user: user).pluck(:linkable_id),).to contain_exactly(public_category.id, public_category2.id)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+ endenddescribe ".update_tag_section_links" do