mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 14:52:46 +08:00
DEV: Use serializers for user_notification_schedule
and featured_topic
(#27719)
Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
parent
906da0f3d1
commit
33c68b28b6
|
@ -294,7 +294,7 @@ class CurrentUserSerializer < BasicUserSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def featured_topic
|
def featured_topic
|
||||||
object.user_profile.featured_topic
|
BasicTopicSerializer.new(object.user_profile.featured_topic, scope: scope, root: false).as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_topic_draft
|
def has_topic_draft
|
||||||
|
|
|
@ -214,7 +214,7 @@ class UserCardSerializer < BasicUserSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def featured_topic
|
def featured_topic
|
||||||
object.user_profile.featured_topic
|
BasicTopicSerializer.new(object.user_profile.featured_topic, scope: scope, root: false).as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_timezone?
|
def include_timezone?
|
||||||
|
|
21
app/serializers/user_notification_schedule_serializer.rb
Normal file
21
app/serializers/user_notification_schedule_serializer.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class UserNotificationScheduleSerializer < ApplicationSerializer
|
||||||
|
attributes :id,
|
||||||
|
:user_id,
|
||||||
|
:enabled,
|
||||||
|
:day_0_start_time,
|
||||||
|
:day_0_end_time,
|
||||||
|
:day_1_start_time,
|
||||||
|
:day_1_end_time,
|
||||||
|
:day_2_start_time,
|
||||||
|
:day_2_end_time,
|
||||||
|
:day_3_start_time,
|
||||||
|
:day_3_end_time,
|
||||||
|
:day_4_start_time,
|
||||||
|
:day_4_end_time,
|
||||||
|
:day_5_start_time,
|
||||||
|
:day_5_end_time,
|
||||||
|
:day_6_start_time,
|
||||||
|
:day_6_end_time
|
||||||
|
end
|
|
@ -77,7 +77,11 @@ class UserSerializer < UserCardSerializer
|
||||||
###
|
###
|
||||||
#
|
#
|
||||||
def user_notification_schedule
|
def user_notification_schedule
|
||||||
object.user_notification_schedule || UserNotificationSchedule::DEFAULT
|
UserNotificationScheduleSerializer.new(
|
||||||
|
object.user_notification_schedule,
|
||||||
|
scope: scope,
|
||||||
|
root: false,
|
||||||
|
).as_json || UserNotificationSchedule::DEFAULT
|
||||||
end
|
end
|
||||||
|
|
||||||
def mailing_list_posts_per_day
|
def mailing_list_posts_per_day
|
||||||
|
|
|
@ -352,4 +352,25 @@ RSpec.describe CurrentUserSerializer do
|
||||||
expect(initial_count).to eq(final_count)
|
expect(initial_count).to eq(final_count)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#featured_topic" do
|
||||||
|
fab!(:featured_topic) { Fabricate(:topic) }
|
||||||
|
|
||||||
|
before { user.user_profile.update!(featured_topic_id: featured_topic.id) }
|
||||||
|
|
||||||
|
it "includes the featured topic" do
|
||||||
|
payload = serializer.as_json
|
||||||
|
|
||||||
|
expect(payload[:featured_topic]).to_not be_nil
|
||||||
|
expect(payload[:featured_topic][:id]).to eq(featured_topic.id)
|
||||||
|
expect(payload[:featured_topic][:title]).to eq(featured_topic.title)
|
||||||
|
expect(payload[:featured_topic].keys).to contain_exactly(
|
||||||
|
:id,
|
||||||
|
:title,
|
||||||
|
:fancy_title,
|
||||||
|
:slug,
|
||||||
|
:posts_count,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -126,4 +126,27 @@ RSpec.describe UserCardSerializer do
|
||||||
expect(json.keys).not_to include :status
|
expect(json.keys).not_to include :status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#featured_topic" do
|
||||||
|
fab!(:user)
|
||||||
|
fab!(:featured_topic) { Fabricate(:topic) }
|
||||||
|
|
||||||
|
before { user.user_profile.update(featured_topic_id: featured_topic.id) }
|
||||||
|
|
||||||
|
it "includes the featured topic" do
|
||||||
|
serializer = described_class.new(user, scope: Guardian.new(user), root: false)
|
||||||
|
json = serializer.as_json
|
||||||
|
|
||||||
|
expect(json[:featured_topic]).to_not be_nil
|
||||||
|
expect(json[:featured_topic][:id]).to eq(featured_topic.id)
|
||||||
|
expect(json[:featured_topic][:title]).to eq(featured_topic.title)
|
||||||
|
expect(json[:featured_topic].keys).to contain_exactly(
|
||||||
|
:id,
|
||||||
|
:title,
|
||||||
|
:fancy_title,
|
||||||
|
:slug,
|
||||||
|
:posts_count,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -339,6 +339,21 @@ RSpec.describe UserSerializer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "with a custom notification schedule" do
|
||||||
|
let(:schedule) do
|
||||||
|
UserNotificationSchedule.create({ user: user }.merge(UserNotificationSchedule::DEFAULT))
|
||||||
|
end
|
||||||
|
let(:scope) { Guardian.new(user) }
|
||||||
|
|
||||||
|
it "includes the serialized schedule" do
|
||||||
|
expect(json[:user_notification_schedule][:enabled]).to eq(schedule[:enabled])
|
||||||
|
expect(json[:user_notification_schedule][:day_0_start_time]).to eq(
|
||||||
|
schedule[:day_0_start_time],
|
||||||
|
)
|
||||||
|
expect(json[:user_notification_schedule][:day_6_end_time]).to eq(schedule[:day_6_end_time])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with custom_fields" do
|
context "with custom_fields" do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user