datamapper/dm-serializer

Default Serialization options for Collection

Opened this issue · 1 comments

It's possible to override the default serialization for a single record by overriding the to_json method, however, it would be nice to be able to apply the same global overrides to a collection of said resource

+1 it would be cool to have default associations option which would specify the association query options.

Usage could be something like:

class Article
  belongs_to :user
  has n, :comments

  serialization_scope :default, { 
      comments: { limit: 5, :created_at.gt => self.updated_at }, 
      user: { fields: [ :name ] } 
    }
end

@article.to_json(scope: :default)
# => { comments: [ {}, {}, {}, {}, {} ], user: {}, other fields... }