-
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.
- Loading branch information
Michael Parrish
committed
Dec 5, 2014
1 parent
5e1b56b
commit 33a3324
Showing
27 changed files
with
226 additions
and
7 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
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,5 @@ | ||
class BoardSerializer | ||
include TalkSerializer | ||
all_attributes | ||
can_include :discussions | ||
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CommentSerializer | ||
include TalkSerializer | ||
all_attributes | ||
can_include :focus | ||
|
||
def links | ||
{ | ||
focus_type: model.focus_type | ||
} if model.focus_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module TalkSerializer | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include RestPack::Serializer | ||
attr_reader :model | ||
attributes :href, :links | ||
can_filter_by(:section) if model_class.columns_hash.has_key? 'section' | ||
end | ||
|
||
module ClassMethods | ||
def all_attributes | ||
attributes *model_class.attribute_names | ||
end | ||
end | ||
|
||
def links | ||
{ } | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ConversationSerializer | ||
include TalkSerializer | ||
all_attributes | ||
can_include :messages | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class DiscussionSerializer | ||
include TalkSerializer | ||
all_attributes | ||
can_include :comments, :board, :user | ||
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class FocusSerializer | ||
include TalkSerializer | ||
all_attributes | ||
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class MessageSerializer | ||
include TalkSerializer | ||
all_attributes | ||
attributes :sender, :recipient | ||
can_include :conversation | ||
|
||
def sender | ||
UserSerializer.as_json model.sender | ||
end | ||
|
||
def recipient | ||
UserSerializer.as_json model.recipient | ||
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class ModerationSerializer | ||
include TalkSerializer | ||
all_attributes | ||
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class TagSerializer | ||
include TalkSerializer | ||
all_attributes | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UserConversationSerializer | ||
include TalkSerializer | ||
all_attributes | ||
can_include :user, :conversation | ||
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class UserSerializer | ||
include TalkSerializer | ||
all_attributes | ||
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,19 @@ | ||
require 'restpack_serializer' | ||
|
||
module RestPack::Serializer | ||
class SideLoadDataBuilder | ||
# Bug fix. Prevent side loaded association from try to find non-existant records | ||
def side_load_belongs_to | ||
foreign_keys = @models.map { |model| model.send(@association.foreign_key) }.uniq.compact | ||
side_load = foreign_keys.any? ? @association.klass.find(foreign_keys) : [] | ||
json_model_data = side_load.map { |model| @serializer.as_json(model) } | ||
{ @association.plural_name.to_sym => json_model_data, meta: { } } | ||
end | ||
end | ||
end | ||
|
||
# preload autoloaded serializers | ||
Dir[Rails.root.join('app/serializers/**/*.rb')].sort.each do |path| | ||
name = path.match(/serializers\/(.+)\.rb$/)[1] | ||
name.classify.constantize unless path =~ /serializers\/concerns/ | ||
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,6 +1,6 @@ | ||
FactoryGirl.define do | ||
factory :tag do | ||
|
||
name{ "tag#{ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe BoardSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:discussions] | ||
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe CommentSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:focus] | ||
|
||
it 'should specify the focus type in links' do | ||
subject = create :subject | ||
comment = create :comment_for_focus, focus: subject | ||
json = CommentSerializer.resource id: comment.id | ||
comment_json = json[:comments].first | ||
expect(comment_json[:links]).to eql focus: subject.id.to_s, focus_type: 'Subject' | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe ConversationSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:messages] | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe DiscussionSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:comments, :board, :user] | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe FocusSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all | ||
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe MessageSerializer, type: :serializer do | ||
let(:conversation){ create :conversation_with_messages } | ||
let(:object){ conversation.messages.first } | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:conversation] | ||
|
||
it 'should sideload sender' do | ||
json = MessageSerializer.resource id: object.id | ||
message_json = json[:messages].first | ||
expect(message_json[:sender][:id]).to eql object.sender.id | ||
expect(message_json[:recipient][:id]).to eql object.recipient.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe ModerationSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe TagSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe UserConversationSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all, including: [:user, :conversation] | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe UserSerializer, type: :serializer do | ||
it_behaves_like 'a talk serializer', exposing: :all | ||
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
RSpec.shared_context 'a serializer' do | ||
let(:serializer){ described_class } | ||
let(:model){ serializer.model_class } | ||
let(:instance){ FactoryGirl.create serializer.singular_key } | ||
let(:model_instance){ instance } | ||
let(:json){ serializer.as_json model_instance } | ||
end | ||
|
||
RSpec.shared_examples 'a talk serializer' do |exposing: nil, including: nil| | ||
include_context 'a serializer' | ||
let(:model_instance){ defined?(object) ? object : instance } | ||
|
||
describe 'attributes' do | ||
subject{ json } | ||
|
||
if exposing == :all | ||
described_class.model_class.attribute_names.each do |name| | ||
it{ is_expected.to include name.to_sym } | ||
end | ||
elsif exposing | ||
exposing.each do |name| | ||
it{ is_expected.to include name.to_sym } | ||
end | ||
end | ||
|
||
it{ is_expected.to include :href } | ||
end | ||
|
||
describe 'associations' do | ||
if including | ||
including.each do |association| | ||
it "should allow inclusion of #{ association }" do | ||
association = association.to_s | ||
json = serializer.resource id: model_instance.id, include: association | ||
association_key = association.to_s.pluralize.to_sym | ||
expect(json[:linked]).to include association_key | ||
|
||
macro = model.reflect_on_association(association).macro | ||
unless macro.in? [:belongs_to, :has_one] | ||
expect(json[:meta]).to include association_key | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |