|
| 1 | +class AuthorSerializer < ActiveModel::Serializer |
| 2 | + attributes :id, :name |
| 3 | + |
| 4 | + has_many :posts, embed: :ids |
| 5 | + has_one :bio |
| 6 | +end |
| 7 | + |
| 8 | +class BlogSerializer < ActiveModel::Serializer |
| 9 | + attributes :id, :name |
| 10 | +end |
| 11 | + |
| 12 | +class CommentSerializer < ActiveModel::Serializer |
| 13 | + attributes :id, :body |
| 14 | + |
| 15 | + belongs_to :post |
| 16 | + belongs_to :author |
| 17 | +end |
| 18 | + |
| 19 | +class PostSerializer < ActiveModel::Serializer |
| 20 | + attributes :id, :title, :body |
| 21 | + |
| 22 | + has_many :comments, serializer: CommentSerializer |
| 23 | + belongs_to :blog, serializer: BlogSerializer |
| 24 | + belongs_to :author, serializer: AuthorSerializer |
| 25 | + |
| 26 | + def blog |
| 27 | + Blog.new(id: 999, name: 'Custom blog') |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +class CachingAuthorSerializer < AuthorSerializer |
| 32 | + cache key: 'writer', skip_digest: true |
| 33 | +end |
| 34 | + |
| 35 | +class CachingCommentSerializer < CommentSerializer |
| 36 | + cache expires_in: 1.day, skip_digest: true |
| 37 | +end |
| 38 | + |
| 39 | +class CachingPostSerializer < PostSerializer |
| 40 | + cache key: 'post', expires_in: 0.1, skip_digest: true |
| 41 | + belongs_to :blog, serializer: BlogSerializer |
| 42 | + belongs_to :author, serializer: CachingAuthorSerializer |
| 43 | + has_many :comments, serializer: CachingCommentSerializer |
| 44 | +end |
| 45 | + |
| 46 | +# ActiveModelSerializers::Model is a convenient |
| 47 | +# serializable class to inherit from when making |
| 48 | +# serializable non-activerecord objects. |
| 49 | +class DummyModel |
| 50 | + include ActiveModel::Model |
| 51 | + include ActiveModel::Serializers::JSON |
| 52 | + |
| 53 | + attr_reader :attributes |
| 54 | + |
| 55 | + def initialize(attributes = {}) |
| 56 | + @attributes = attributes |
| 57 | + super |
| 58 | + end |
| 59 | + |
| 60 | + # Defaults to the downcased model name. |
| 61 | + def id |
| 62 | + attributes.fetch(:id) { self.class.name.downcase } |
| 63 | + end |
| 64 | + |
| 65 | + # Defaults to the downcased model name and updated_at |
| 66 | + def cache_key |
| 67 | + attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" } |
| 68 | + end |
| 69 | + |
| 70 | + # Defaults to the time the serializer file was modified. |
| 71 | + def updated_at |
| 72 | + attributes.fetch(:updated_at) { File.mtime(__FILE__) } |
| 73 | + end |
| 74 | + |
| 75 | + def read_attribute_for_serialization(key) |
| 76 | + if key == :id || key == 'id' |
| 77 | + attributes.fetch(key) { id } |
| 78 | + else |
| 79 | + attributes[key] |
| 80 | + end |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +class Comment < DummyModel |
| 85 | + attr_accessor :id, :body |
| 86 | + |
| 87 | + def cache_key |
| 88 | + "#{self.class.name.downcase}/#{id}" |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +class Author < DummyModel |
| 93 | + attr_accessor :id, :name, :posts |
| 94 | +end |
| 95 | + |
| 96 | +class Post < DummyModel |
| 97 | + attr_accessor :id, :title, :body, :comments, :blog, :author |
| 98 | + |
| 99 | + def cache_key |
| 100 | + 'benchmarking::post/1-20151215212620000000000' |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +class Blog < DummyModel |
| 105 | + attr_accessor :id, :name |
| 106 | +end |
0 commit comments