mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Custom Wordpress Serializer and Path, with Specs
This commit is contained in:
34
app/serializers/basic_post_serializer.rb
Normal file
34
app/serializers/basic_post_serializer.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# The most basic attributes of a topic that we need to create a link for it.
|
||||
class BasicPostSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:username,
|
||||
:avatar_template,
|
||||
:created_at,
|
||||
:cooked
|
||||
|
||||
def name
|
||||
object.user.name
|
||||
end
|
||||
|
||||
def username
|
||||
object.user.username
|
||||
end
|
||||
|
||||
def avatar_template
|
||||
object.user.avatar_template
|
||||
end
|
||||
|
||||
def cooked
|
||||
if object.hidden && !scope.is_staff?
|
||||
if scope.current_user && object.user_id == scope.current_user.id
|
||||
I18n.t('flagging.you_must_edit')
|
||||
else
|
||||
I18n.t('flagging.user_must_edit')
|
||||
end
|
||||
else
|
||||
object.filter_quotes(@parent_post)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,5 +1,3 @@
|
||||
require_dependency 'age_words'
|
||||
|
||||
# The most basic attributes of a topic that we need to create a link for it.
|
||||
class BasicTopicSerializer < ApplicationSerializer
|
||||
attributes :id, :fancy_title, :slug, :posts_count
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class PostSerializer < ApplicationSerializer
|
||||
class PostSerializer < BasicPostSerializer
|
||||
|
||||
# To pass in additional information we might need
|
||||
attr_accessor :topic_slug
|
||||
@@ -8,10 +8,8 @@ class PostSerializer < ApplicationSerializer
|
||||
attr_accessor :single_post_link_counts
|
||||
attr_accessor :draft_sequence
|
||||
|
||||
attributes :id,
|
||||
:post_number,
|
||||
attributes :post_number,
|
||||
:post_type,
|
||||
:created_at,
|
||||
:updated_at,
|
||||
:reply_count,
|
||||
:reply_to_post_number,
|
||||
@@ -29,10 +27,7 @@ class PostSerializer < ApplicationSerializer
|
||||
:can_delete,
|
||||
:can_recover,
|
||||
:link_counts,
|
||||
:cooked,
|
||||
:read,
|
||||
:username,
|
||||
:name,
|
||||
:user_title,
|
||||
:reply_to_user,
|
||||
:bookmarked,
|
||||
@@ -40,7 +35,6 @@ class PostSerializer < ApplicationSerializer
|
||||
:actions_summary,
|
||||
:moderator?,
|
||||
:staff?,
|
||||
:avatar_template,
|
||||
:user_id,
|
||||
:draft_sequence,
|
||||
:hidden,
|
||||
@@ -57,10 +51,6 @@ class PostSerializer < ApplicationSerializer
|
||||
object.user.staff?
|
||||
end
|
||||
|
||||
def avatar_template
|
||||
object.user.avatar_template
|
||||
end
|
||||
|
||||
def yours
|
||||
scope.user == object.user
|
||||
end
|
||||
@@ -77,6 +67,10 @@ class PostSerializer < ApplicationSerializer
|
||||
scope.can_recover_post?(object)
|
||||
end
|
||||
|
||||
def display_username
|
||||
object.user.name
|
||||
end
|
||||
|
||||
def link_counts
|
||||
|
||||
return @single_post_link_counts if @single_post_link_counts.present?
|
||||
@@ -93,18 +87,6 @@ class PostSerializer < ApplicationSerializer
|
||||
end
|
||||
end
|
||||
|
||||
def cooked
|
||||
if object.hidden && !scope.is_staff?
|
||||
if scope.current_user && object.user_id == scope.current_user.id
|
||||
I18n.t('flagging.you_must_edit')
|
||||
else
|
||||
I18n.t('flagging.user_must_edit')
|
||||
end
|
||||
else
|
||||
object.filter_quotes(@parent_post)
|
||||
end
|
||||
end
|
||||
|
||||
def read
|
||||
@topic_view.read?(object.post_number)
|
||||
end
|
||||
@@ -113,22 +95,10 @@ class PostSerializer < ApplicationSerializer
|
||||
object.score || 0
|
||||
end
|
||||
|
||||
def display_username
|
||||
object.user.name
|
||||
end
|
||||
|
||||
def version
|
||||
object.cached_version
|
||||
end
|
||||
|
||||
def username
|
||||
object.user.username
|
||||
end
|
||||
|
||||
def name
|
||||
object.user.name
|
||||
end
|
||||
|
||||
def user_title
|
||||
object.user.title
|
||||
end
|
||||
|
||||
32
app/serializers/topic_view_wordpress_serializer.rb
Normal file
32
app/serializers/topic_view_wordpress_serializer.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class TopicViewWordpressSerializer < ApplicationSerializer
|
||||
|
||||
# These attributes will be delegated to the topic
|
||||
attributes :id,
|
||||
:posts_count,
|
||||
:filtered_posts_count,
|
||||
:posts
|
||||
|
||||
has_many :participants, serializer: BasicUserSerializer, embed: :objects
|
||||
has_many :posts, serializer: BasicPostSerializer, embed: :objects
|
||||
|
||||
def id
|
||||
object.topic.id
|
||||
end
|
||||
|
||||
def posts_count
|
||||
object.topic.posts_count
|
||||
end
|
||||
|
||||
def filtered_posts_count
|
||||
object.filtered_posts_count
|
||||
end
|
||||
|
||||
def participants
|
||||
object.participants.values
|
||||
end
|
||||
|
||||
def posts
|
||||
object.posts
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user