Skip to content

Commit

Permalink
More flexible serializer preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Parrish committed Aug 3, 2015
1 parent 873b3e5 commit 31d7889
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/serializers/announcement_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class AnnouncementSerializer
can_filter_by :section
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]
self.includes = [:project]
end
2 changes: 1 addition & 1 deletion app/serializers/board_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class BoardSerializer
can_sort_by :created_at
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]
self.includes = [:project]
end
2 changes: 1 addition & 1 deletion app/serializers/comment_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CommentSerializer
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]
self.includes = [:user, :focus, :discussion, :board, :project]

def custom_attributes
super.merge focus: focus, user_display_name: model.user.display_name
Expand Down
14 changes: 9 additions & 5 deletions app/serializers/concerns/talk_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ module TalkSerializer
attributes :href, :links

class << self
attr_accessor :eager_loads
attr_accessor :eager_loads, :preloads, :includes
attr_accessor :default_sort
end

self.eager_loads ||= []
self.preloads ||= []
self.includes ||= []
is_sectioned = model_class.columns_hash.has_key?('section') rescue false
can_filter_by(:section) if is_sectioned
stringify_primary_key
Expand Down Expand Up @@ -46,16 +48,18 @@ def stringify_foreign_keys

module ClassMethodOverrides
def resource(params = { }, scope = nil, context = { })
super params, scope_eager_loads_for(scope), context
super params, scope_preloading_for(scope), context
end

def page(params = { }, scope = nil, context = { })
super params, scope_eager_loads_for(scope), context
super params, scope_preloading_for(scope), context
end

def scope_eager_loads_for(scope)
def scope_preloading_for(scope)
scope ||= model_class.all
scope = scope.includes(*eager_loads) if eager_loads.any?
scope = scope.includes(*includes) if includes.any?
scope = scope.preload(*preloads) if preloads.any?
scope = scope.eager_load(*eager_loads) if eager_loads.any?
scope
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/discussion_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DiscussionSerializer
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]
self.includes = [:user, :board, :project]

def custom_attributes
super.merge user_display_name: model.user.display_name
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/notification_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class NotificationSerializer
can_include :subscription
embed_attributes_from :project
self.default_sort = 'created_at'
self.eager_loads = [:project]
self.includes = [:project]
end
2 changes: 1 addition & 1 deletion app/serializers/tag_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class TagSerializer
all_attributes
can_filter_by :taggable_id, :taggable_type
embed_attributes_from :project
self.eager_loads = [:project]
self.includes = [:project]
end

0 comments on commit 31d7889

Please sign in to comment.