PERF: Cache categories in Site model take 3.

Previous attempt resulted in custom fields going missing in the
serialized output.

This reverts commit 83a6ad32ff.
This commit is contained in:
Alan Guo Xiang Tan
2021-06-23 15:21:11 +08:00
parent fd2aab09ef
commit 0e4b8c5318
9 changed files with 181 additions and 30 deletions

View File

@@ -58,30 +58,85 @@ describe Site do
expect(Site.new(guardian).categories.last.notification_level).to eq(1)
end
it "omits categories users can not write to from the category list" do
category = Fabricate(:category)
user = Fabricate(:user)
describe '#categories' do
fab!(:category) { Fabricate(:category) }
fab!(:user) { Fabricate(:user) }
fab!(:guardian) { Guardian.new(user) }
expect(Site.new(Guardian.new(user)).categories.count).to eq(2)
after do
Site.clear_cache
end
category.set_permissions(everyone: :create_post)
category.save
it "omits read restricted categories" do
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
SiteSetting.uncategorized_category_id, category.id
)
guardian = Guardian.new(user)
category.update!(read_restricted: true)
expect(Site.new(guardian)
.categories
.keep_if { |c| c.name == category.name }
.first
.permission)
.not_to eq(CategoryGroup.permission_types[:full])
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
SiteSetting.uncategorized_category_id
)
end
# If a parent category is not visible, the child categories should not be returned
category.set_permissions(staff: :full)
category.save
it "includes categories that a user's group can see" do
group = Fabricate(:group)
category.update!(read_restricted: true)
category.groups << group
sub_category = Fabricate(:category, parent_category_id: category.id)
expect(Site.new(guardian).categories).not_to include(sub_category)
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
SiteSetting.uncategorized_category_id
)
group.add(user)
expect(Site.new(Guardian.new(user)).categories.map(&:id)).to contain_exactly(
SiteSetting.uncategorized_category_id, category.id
)
end
it "omits categories users can not write to from the category list" do
expect(Site.new(guardian).categories.count).to eq(2)
category.set_permissions(everyone: :create_post)
category.save!
guardian = Guardian.new(user)
expect(Site.new(guardian)
.categories
.keep_if { |c| c.name == category.name }
.first
.permission)
.not_to eq(CategoryGroup.permission_types[:full])
# If a parent category is not visible, the child categories should not be returned
category.set_permissions(staff: :full)
category.save!
sub_category = Fabricate(:category, parent_category_id: category.id)
expect(Site.new(guardian).categories).not_to include(sub_category)
end
it 'should clear the cache when custom fields are updated' do
Site.preloaded_category_custom_fields << "enable_marketplace"
categories = Site.new(Guardian.new).categories
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq(nil)
category.custom_fields["enable_marketplace"] = true
category.save_custom_fields
categories = Site.new(Guardian.new).categories
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq('t')
category.upsert_custom_fields(enable_marketplace: false)
categories = Site.new(Guardian.new).categories
expect(categories.last[:custom_fields]["enable_marketplace"]).to eq('f')
end
end
it "omits groups user can not see" do