Skip to content

Commit

Permalink
Generalize merging embedded attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Parrish committed Jul 30, 2015
1 parent 981a586 commit a2ad978
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
5 changes: 1 addition & 4 deletions app/serializers/announcement_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ class AnnouncementSerializer
all_attributes
can_sort_by :created_at
can_filter_by :section
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]

def custom_attributes
super.merge attributes_from :project
end
end
5 changes: 1 addition & 4 deletions app/serializers/board_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class BoardSerializer
can_include :discussions, :parent, :sub_boards
can_filter_by :subject_default
can_sort_by :created_at
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]

def custom_attributes
super.merge attributes_from :project
end
end
7 changes: 2 additions & 5 deletions app/serializers/comment_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ class CommentSerializer
can_include :discussion
can_sort_by :created_at
can_filter_by :user_id, :focus_id, :focus_type
embed_attributes_from :discussion, :board, :project
self.default_sort = 'created_at'
self.eager_loads = [:user, :focus, :discussion, :board, :project]

def custom_attributes
super
.merge(attributes_from(:discussion))
.merge(attributes_from(:board))
.merge(attributes_from(:project))
.merge focus: focus, user_display_name: model.user.display_name
super.merge focus: focus, user_display_name: model.user.display_name
end

def focus
Expand Down
20 changes: 20 additions & 0 deletions app/serializers/concerns/embedded_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
module EmbeddedAttributes
extend ActiveSupport::Concern

included do
class_attribute :_embedded_attributes
self._embedded_attributes = []
end

module ClassMethods
def embed_attributes_from(*list)
self._embedded_attributes += list.map(&:to_sym)
self._embedded_attributes.uniq!
end
end

def discussion_attributes
%w(comments_count subject_default title updated_at users_count)
end
Expand All @@ -13,6 +25,14 @@ def project_attributes
%w(slug)
end

def custom_attributes
super.tap do |custom_attrs|
self._embedded_attributes.each do |relation|
custom_attrs.merge! attributes_from relation
end
end
end

def attributes_from(relation)
{ }.tap do |attrs|
record_attributes = model.send(relation).attributes rescue { }
Expand Down
6 changes: 2 additions & 4 deletions app/serializers/discussion_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ class DiscussionSerializer
can_include :comments, :board, :user
can_filter_by :title, :subject_default, :sticky
can_sort_by :updated_at, :sticky, :sticky_position
embed_attributes_from :project, :board
self.default_sort = '-sticky,sticky_position,-updated_at'
self.eager_loads = [:user, :board, :project]

def custom_attributes
super
.merge(attributes_from(:project))
.merge(attributes_from(:board))
.merge user_display_name: model.user.display_name
super.merge user_display_name: model.user.display_name
end
end
5 changes: 1 addition & 4 deletions app/serializers/notification_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class NotificationSerializer
can_sort_by :created_at
can_filter_by :section, :delivered
can_include :subscription
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]

def custom_attributes
super.merge attributes_from :project
end
end
5 changes: 1 addition & 4 deletions app/serializers/tag_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ class TagSerializer
include TalkSerializer
include EmbeddedAttributes
all_attributes
embed_attributes_from :project
self.eager_loads = [:project]

def custom_attributes
super.merge attributes_from :project
end
end

0 comments on commit a2ad978

Please sign in to comment.