Skip to content

A platform for community discussion. Free, open, simple.

DEV: Use has_many and ArraySerializer for SidebarSectionsSerializer (#26716)

Daniel Waterwortha6b8051645cf
diff --git a/app/controllers/sidebar_sections_controller.rb b/app/controllers/sidebar_sections_controller.rb
index 39b34f79dab..96e823be114 100644
--- a/app/controllers/sidebar_sections_controller.rb
+++ b/app/controllers/sidebar_sections_controller.rb
@@ -11,7 +11,14 @@ class SidebarSectionsController < ApplicationController
.includes(:sidebar_urls)
.where("public OR user_id = ?", current_user.id)
.order("(public IS TRUE) DESC, title ASC")
- .map { |section| SidebarSectionSerializer.new(section, root: false) }
+
+ sections =
+ ActiveModel::ArraySerializer.new(
+ sections,
+ each_serializer: SidebarSectionSerializer,
+ scope: guardian,
+ root: "sidebar_sections",
+ )
render json: sections
end
diff --git a/app/models/site.rb b/app/models/site.rb
index 1390c1c25a3..03b435dede7 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -188,6 +188,13 @@ class Site
query
end
+ def anonymous_sidebar_sections
+ SidebarSection
+ .public_sections
+ .includes(:sidebar_urls)
+ .order("(section_type IS NOT NULL) DESC, (public IS TRUE) DESC")
+ end
+
def archetypes
Archetype.list.reject { |t| t.id == Archetype.private_message }
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 65048da9b94..8a5d182965a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -367,6 +367,13 @@ class User < ActiveRecord::Base
custom_fields_clean? || SiteSetting.disable_watched_word_checking_in_user_fields
end
+ def all_sidebar_sections
+ sidebar_sections
+ .or(SidebarSection.public_sections)
+ .includes(:sidebar_urls)
+ .order("(section_type IS NOT NULL) DESC, (public IS TRUE) DESC")
+ end
+
def secured_sidebar_category_ids(user_guardian = nil)
user_guardian ||= guardian
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb
index 977dd60bafa..aba648259d0 100644
--- a/app/serializers/current_user_serializer.rb
+++ b/app/serializers/current_user_serializer.rb
@@ -80,21 +80,16 @@ class CurrentUserSerializer < BasicUserSerializer
delegate :any_posts, :draft_count, :pending_posts_count, :read_faq?, to: :user_stat
has_one :user_option, embed: :object, serializer: CurrentUserOptionSerializer
+ has_many :all_sidebar_sections,
+ embed: :object,
+ key: :sidebar_sections,
+ serializer: SidebarSectionSerializer
def initialize(object, options = {})
super
options[:include_status] = true
end
- def sidebar_sections
- SidebarSection
- .public_sections
- .or(SidebarSection.where(user_id: object.id))
- .includes(:sidebar_urls)
- .order("(section_type IS NOT NULL) DESC, (public IS TRUE) DESC")
- .map { |section| SidebarSectionSerializer.new(section, root: false) }
- end
-
def groups
owned_group_ids = GroupUser.where(user_id: id, owner: true).pluck(:group_id).to_set
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index 9108ad3bbb9..1f5bed13b85 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -53,6 +53,7 @@ class SiteSerializer < ApplicationSerializer
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
has_many :user_fields, embed: :objects, serializer: UserFieldSerializer
has_many :auth_providers, embed: :objects, serializer: AuthProviderSerializer
+ has_many :anonymous_sidebar_sections, embed: :objects, serializer: SidebarSectionSerializer
def user_themes
cache_fragment("user_themes") do
@@ -294,14 +295,6 @@ class SiteSerializer < ApplicationSerializer
anonymous_default_navigation_menu_tags.present?
end
- def anonymous_sidebar_sections
- SidebarSection
- .public_sections
- .includes(:sidebar_urls)
- .order("(section_type IS NOT NULL) DESC, (public IS TRUE) DESC")
- .map { |section| SidebarSectionSerializer.new(section, root: false) }
- end
-
def include_anonymous_sidebar_sections?
scope.anonymous?
end
diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rb
index 27614aecd70..5fa3074f6c8 100644
--- a/spec/serializers/current_user_serializer_spec.rb
+++ b/spec/serializers/current_user_serializer_spec.rb
@@ -330,7 +330,7 @@ RSpec.describe CurrentUserSerializer do
expect(serialized[:sidebar_sections].count).to eq(2)
- expect(serialized[:sidebar_sections].last.links.map { |link| link.id }).to eq(
+ expect(serialized[:sidebar_sections].last[:links].map { |link| link.id }).to eq(
[custom_sidebar_section_link_1.linkable.id],
)
end.count
@@ -344,7 +344,7 @@ RSpec.describe CurrentUserSerializer do
expect(serialized[:sidebar_sections].count).to eq(2)
- expect(serialized[:sidebar_sections].last.links.map { |link| link.id }).to eq(
+ expect(serialized[:sidebar_sections].last[:links].map { |link| link.id }).to eq(
[custom_sidebar_section_link_1.linkable.id, custom_sidebar_section_link_2.linkable.id],
)
end.count
diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb
index 668c30f3894..d6440fb1d0a 100644
--- a/spec/serializers/site_serializer_spec.rb
+++ b/spec/serializers/site_serializer_spec.rb
@@ -220,7 +220,7 @@ RSpec.describe SiteSerializer do
it "includes only public sidebar sections serialised object when user is anonymous" do
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
- expect(serialized[:anonymous_sidebar_sections].map(&:title)).to eq(
+ expect(serialized[:anonymous_sidebar_sections].map { |section| section[:title] }).to eq(
["Community", "Public section"],
)
end
@@ -237,7 +237,7 @@ RSpec.describe SiteSerializer do
expect(serialized[:anonymous_sidebar_sections].count).to eq(2)
- expect(serialized[:anonymous_sidebar_sections].last.links.map { |link| link.id }).to eq(
+ expect(serialized[:anonymous_sidebar_sections].last[:links].map { |link| link.id }).to eq(
[public_section_link.linkable.id],
)
end.count
@@ -253,7 +253,7 @@ RSpec.describe SiteSerializer do
expect(serialized[:anonymous_sidebar_sections].count).to eq(2)
- expect(serialized[:anonymous_sidebar_sections].last.links.map { |link| link.id }).to eq(
+ expect(serialized[:anonymous_sidebar_sections].last[:links].map { |link| link.id }).to eq(
[
public_section_link.linkable.id,
public_section_link_2.linkable.id,