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.rbindex 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: sectionsenddiff --git a/app/models/site.rb b/app/models/site.rbindex 1390c1c25a3..03b435dede7 100644--- a/app/models/site.rb+++ b/app/models/site.rb@@ -188,6 +188,13 @@ class Sitequeryend+ def anonymous_sidebar_sections+ SidebarSection+ .public_sections+ .includes(:sidebar_urls)+ .order("(section_type IS NOT NULL) DESC, (public IS TRUE) DESC")+ end+def archetypesArchetype.list.reject { |t| t.id == Archetype.private_message }enddiff --git a/app/models/user.rb b/app/models/user.rbindex 65048da9b94..8a5d182965a 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -367,6 +367,13 @@ class User < ActiveRecord::Basecustom_fields_clean? || SiteSetting.disable_watched_word_checking_in_user_fieldsend+ 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 ||= guardiandiff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 977dd60bafa..aba648259d0 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -80,21 +80,16 @@ class CurrentUserSerializer < BasicUserSerializerdelegate :any_posts, :draft_count, :pending_posts_count, :read_faq?, to: :user_stathas_one :user_option, embed: :object, serializer: CurrentUserOptionSerializer+ has_many :all_sidebar_sections,+ embed: :object,+ key: :sidebar_sections,+ serializer: SidebarSectionSerializerdef initialize(object, options = {})superoptions[:include_status] = trueend- 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 groupsowned_group_ids = GroupUser.where(user_id: id, owner: true).pluck(:group_id).to_setdiff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rbindex 9108ad3bbb9..1f5bed13b85 100644--- a/app/serializers/site_serializer.rb+++ b/app/serializers/site_serializer.rb@@ -53,6 +53,7 @@ class SiteSerializer < ApplicationSerializerhas_many :archetypes, embed: :objects, serializer: ArchetypeSerializerhas_many :user_fields, embed: :objects, serializer: UserFieldSerializerhas_many :auth_providers, embed: :objects, serializer: AuthProviderSerializer+ has_many :anonymous_sidebar_sections, embed: :objects, serializer: SidebarSectionSerializerdef user_themescache_fragment("user_themes") do@@ -294,14 +295,6 @@ class SiteSerializer < ApplicationSerializeranonymous_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?enddiff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 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 doexpect(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 doexpect(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.countdiff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rbindex 668c30f3894..d6440fb1d0a 100644--- a/spec/serializers/site_serializer_spec.rb+++ b/spec/serializers/site_serializer_spec.rb@@ -220,7 +220,7 @@ RSpec.describe SiteSerializer doit "includes only public sidebar sections serialised object when user is anonymous" doserialized = 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 doexpect(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 doexpect(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,