-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract and generalize embedded attributes in serializers
- Loading branch information
Michael Parrish
committed
Jul 30, 2015
1 parent
0123e12
commit ec11e12
Showing
8 changed files
with
86 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
class BoardSerializer | ||
include TalkSerializer | ||
include EmbeddedAttributes | ||
|
||
all_attributes | ||
can_include :discussions, :parent, :sub_boards | ||
can_filter_by :subject_default | ||
can_sort_by :created_at | ||
self.default_sort = 'created_at' | ||
self.eager_loads = [:project] | ||
|
||
def custom_attributes | ||
super.merge attributes_from :project | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module EmbeddedAttributes | ||
extend ActiveSupport::Concern | ||
|
||
def discussion_attributes | ||
%w(comments_count subject_default title updated_at users_count) | ||
end | ||
|
||
def board_attributes | ||
%w(comments_count description discussions_count id parent_id subject_default title users_count) | ||
end | ||
|
||
def project_attributes | ||
%w(slug) | ||
end | ||
|
||
def attributes_from(relation) | ||
{ }.tap do |attrs| | ||
record_attributes = model.send(relation).attributes rescue { } | ||
send("#{ relation }_attributes").each do |attr| | ||
value = record_attributes[attr] | ||
value = value.to_s if attr =~ /(_id)|(^id$)$/ | ||
attrs[:"#{ relation }_#{ attr }"] = value | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
class DiscussionSerializer | ||
include TalkSerializer | ||
include EmbeddedAttributes | ||
include ModerationActions | ||
|
||
all_attributes | ||
can_include :comments, :board, :user | ||
can_filter_by :title, :subject_default, :sticky | ||
can_sort_by :updated_at, :sticky, :sticky_position | ||
self.default_sort = '-sticky,sticky_position,-updated_at' | ||
self.eager_loads = [:user] | ||
self.eager_loads = [:user, :board, :project] | ||
|
||
def custom_attributes | ||
super.merge user_display_name: model.user.display_name | ||
super | ||
.merge(attributes_from(:project)) | ||
.merge(attributes_from(:board)) | ||
.merge user_display_name: model.user.display_name | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rspec' | ||
|
||
RSpec.shared_examples_for 'a serializer with embedded attributes' do |relations: []| | ||
let!(:project) do | ||
Project.find_by_id(model_instance.project_id) || | ||
create(:project, id: model_instance.project_id) | ||
end | ||
|
||
if relations.include?(:board) | ||
describe '#board_attributes' do | ||
subject{ json } | ||
its([:board_comments_count]){ is_expected.to eql model_instance.board.comments_count } | ||
its([:board_description]){ is_expected.to eql model_instance.board.description } | ||
its([:board_discussions_count]){ is_expected.to eql model_instance.board.discussions_count } | ||
its([:board_id]){ is_expected.to eql model_instance.board.id.to_s } | ||
its([:board_parent_id]){ is_expected.to eql model_instance.board.parent_id.to_s } | ||
its([:board_subject_default]){ is_expected.to eql model_instance.board.subject_default } | ||
its([:board_title]){ is_expected.to eql model_instance.board.title } | ||
its([:board_users_count]){ is_expected.to eql model_instance.board.users_count } | ||
end | ||
end | ||
|
||
if relations.include?(:discussion) | ||
describe '#discussion_attributes' do | ||
subject{ json } | ||
its([:discussion_comments_count]){ is_expected.to eql model_instance.discussion.comments_count } | ||
its([:discussion_subject_default]){ is_expected.to eql model_instance.discussion.subject_default } | ||
its([:discussion_title]){ is_expected.to eql model_instance.discussion.title } | ||
its([:discussion_updated_at]){ is_expected.to be_within(1.second).of model_instance.discussion.updated_at } | ||
its([:discussion_users_count]){ is_expected.to eql model_instance.discussion.users_count } | ||
end | ||
end | ||
|
||
if relations.include?(:project) | ||
describe '#project_attributes' do | ||
subject{ json } | ||
its([:project_slug]){ is_expected.to eql model_instance.project.slug } | ||
end | ||
end | ||
end |