Custom Wordpress Serializer and Path, with Specs

This commit is contained in:
Robin Ward
2013-06-28 13:55:34 -04:00
parent 2d6118297d
commit 2deaf8ef98
8 changed files with 137 additions and 42 deletions

View 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

View File

@@ -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

View File

@@ -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

View 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