mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
DEV: Use has_many and ArraySerializer for SidebarSectionsSerializer (#26716)
This commit is contained in:
parent
10f77556cd
commit
a6b8051645
|
@ -11,7 +11,14 @@ class SidebarSectionsController < ApplicationController
|
||||||
.includes(:sidebar_urls)
|
.includes(:sidebar_urls)
|
||||||
.where("public OR user_id = ?", current_user.id)
|
.where("public OR user_id = ?", current_user.id)
|
||||||
.order("(public IS TRUE) DESC, title ASC")
|
.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
|
render json: sections
|
||||||
end
|
end
|
||||||
|
|
|
@ -188,6 +188,13 @@ class Site
|
||||||
query
|
query
|
||||||
end
|
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
|
def archetypes
|
||||||
Archetype.list.reject { |t| t.id == Archetype.private_message }
|
Archetype.list.reject { |t| t.id == Archetype.private_message }
|
||||||
end
|
end
|
||||||
|
|
|
@ -367,6 +367,13 @@ class User < ActiveRecord::Base
|
||||||
custom_fields_clean? || SiteSetting.disable_watched_word_checking_in_user_fields
|
custom_fields_clean? || SiteSetting.disable_watched_word_checking_in_user_fields
|
||||||
end
|
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)
|
def secured_sidebar_category_ids(user_guardian = nil)
|
||||||
user_guardian ||= guardian
|
user_guardian ||= guardian
|
||||||
|
|
||||||
|
|
|
@ -80,21 +80,16 @@ class CurrentUserSerializer < BasicUserSerializer
|
||||||
delegate :any_posts, :draft_count, :pending_posts_count, :read_faq?, to: :user_stat
|
delegate :any_posts, :draft_count, :pending_posts_count, :read_faq?, to: :user_stat
|
||||||
|
|
||||||
has_one :user_option, embed: :object, serializer: CurrentUserOptionSerializer
|
has_one :user_option, embed: :object, serializer: CurrentUserOptionSerializer
|
||||||
|
has_many :all_sidebar_sections,
|
||||||
|
embed: :object,
|
||||||
|
key: :sidebar_sections,
|
||||||
|
serializer: SidebarSectionSerializer
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
super
|
super
|
||||||
options[:include_status] = true
|
options[:include_status] = true
|
||||||
end
|
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
|
def groups
|
||||||
owned_group_ids = GroupUser.where(user_id: id, owner: true).pluck(:group_id).to_set
|
owned_group_ids = GroupUser.where(user_id: id, owner: true).pluck(:group_id).to_set
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ class SiteSerializer < ApplicationSerializer
|
||||||
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
|
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
|
||||||
has_many :user_fields, embed: :objects, serializer: UserFieldSerializer
|
has_many :user_fields, embed: :objects, serializer: UserFieldSerializer
|
||||||
has_many :auth_providers, embed: :objects, serializer: AuthProviderSerializer
|
has_many :auth_providers, embed: :objects, serializer: AuthProviderSerializer
|
||||||
|
has_many :anonymous_sidebar_sections, embed: :objects, serializer: SidebarSectionSerializer
|
||||||
|
|
||||||
def user_themes
|
def user_themes
|
||||||
cache_fragment("user_themes") do
|
cache_fragment("user_themes") do
|
||||||
|
@ -294,14 +295,6 @@ class SiteSerializer < ApplicationSerializer
|
||||||
anonymous_default_navigation_menu_tags.present?
|
anonymous_default_navigation_menu_tags.present?
|
||||||
end
|
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?
|
def include_anonymous_sidebar_sections?
|
||||||
scope.anonymous?
|
scope.anonymous?
|
||||||
end
|
end
|
||||||
|
|
|
@ -330,7 +330,7 @@ RSpec.describe CurrentUserSerializer do
|
||||||
|
|
||||||
expect(serialized[:sidebar_sections].count).to eq(2)
|
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_1.linkable.id],
|
||||||
)
|
)
|
||||||
end.count
|
end.count
|
||||||
|
@ -344,7 +344,7 @@ RSpec.describe CurrentUserSerializer do
|
||||||
|
|
||||||
expect(serialized[:sidebar_sections].count).to eq(2)
|
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],
|
[custom_sidebar_section_link_1.linkable.id, custom_sidebar_section_link_2.linkable.id],
|
||||||
)
|
)
|
||||||
end.count
|
end.count
|
||||||
|
|
|
@ -220,7 +220,7 @@ RSpec.describe SiteSerializer do
|
||||||
|
|
||||||
it "includes only public sidebar sections serialised object when user is anonymous" 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
|
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"],
|
["Community", "Public section"],
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -237,7 +237,7 @@ RSpec.describe SiteSerializer do
|
||||||
|
|
||||||
expect(serialized[:anonymous_sidebar_sections].count).to eq(2)
|
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.linkable.id],
|
||||||
)
|
)
|
||||||
end.count
|
end.count
|
||||||
|
@ -253,7 +253,7 @@ RSpec.describe SiteSerializer do
|
||||||
|
|
||||||
expect(serialized[:anonymous_sidebar_sections].count).to eq(2)
|
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.linkable.id,
|
||||||
public_section_link_2.linkable.id,
|
public_section_link_2.linkable.id,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user