-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PERF: summary serializer for events list (#484)
* PERF: summary serializer for events list
- Loading branch information
Showing
7 changed files
with
58 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/serializers/discourse_post_event/event_summary_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscoursePostEvent | ||
class EventSummarySerializer < ApplicationSerializer | ||
attributes :id | ||
attributes :starts_at | ||
attributes :ends_at | ||
attributes :timezone | ||
attributes :post | ||
attributes :name | ||
attributes :category_id | ||
|
||
# lightweight post object containing | ||
# only needed info for client | ||
def post | ||
{ | ||
id: object.post.id, | ||
post_number: object.post.post_number, | ||
url: object.post.url, | ||
topic: { | ||
id: object.post.topic.id, | ||
title: object.post.topic.title, | ||
}, | ||
} | ||
end | ||
|
||
def category_id | ||
object.post.topic.category_id | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
spec/serializers/discourse_post_event/event_summary_serializer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
require "rails_helper" | ||
|
||
describe DiscoursePostEvent::EventSummarySerializer do | ||
before do | ||
SiteSetting.calendar_enabled = true | ||
SiteSetting.discourse_post_event_enabled = true | ||
end | ||
|
||
fab!(:category) | ||
fab!(:topic) { Fabricate(:topic, category: category) } | ||
fab!(:post) { Fabricate(:post, topic: topic) } | ||
fab!(:event) { Fabricate(:event, post: post) } | ||
|
||
it "returns the event summary" do | ||
json = DiscoursePostEvent::EventSummarySerializer.new(event, scope: Guardian.new).as_json | ||
summary = json[:event_summary] | ||
expect(summary[:starts_at]).to eq(event.starts_at) | ||
expect(summary[:ends_at]).to eq(event.ends_at) | ||
expect(summary[:timezone]).to eq(event.timezone) | ||
expect(summary[:name]).to eq(event.name) | ||
expect(summary[:post][:url]).to eq(post.url) | ||
expect(summary[:post][:topic][:title]).to eq(topic.title) | ||
expect(summary[:category_id]).to eq(category.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters