File: poro.rb

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (227 lines) | stat: -rw-r--r-- 5,206 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# frozen_string_literal: true

class Model < ActiveModelSerializers::Model
  rand(2).zero? && derive_attributes_from_names_and_fix_accessors

  attr_writer :id

  # At this time, just for organization of intent
  class_attribute :association_names
  self.association_names = []

  def self.associations(*names)
    self.association_names |= names.map(&:to_sym)
    # Silence redefinition of methods warnings
    ActiveModelSerializers.silence_warnings do
      attr_accessor(*names)
    end
  end

  def associations
    association_names.each_with_object({}) do |association_name, result|
      result[association_name] = public_send(association_name).freeze
    end.with_indifferent_access.freeze
  end

  def attributes
    super.except(*association_names)
  end
end

# see
# https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
# The below allows you to do:
#
#   model = ModelWithErrors.new
#   model.validate!            # => ["cannot be nil"]
#   model.errors.full_messages # => ["name cannot be nil"]
class ModelWithErrors < Model
  attributes :name
end

class Profile < Model
  attributes :name, :description
  associations :comments
end
class ProfileSerializer < ActiveModel::Serializer
  attributes :name, :description
end
class ProfilePreviewSerializer < ActiveModel::Serializer
  attributes :name
end

class Author < Model
  attributes :name
  associations :posts, :bio, :roles, :comments
end
class AuthorSerializer < ActiveModel::Serializer
  cache key: 'writer', skip_digest: true
  attribute :id
  attribute :name

  has_many :posts
  has_many :roles
  has_one :bio
end
class AuthorPreviewSerializer < ActiveModel::Serializer
  attributes :id
  has_many :posts
end

class Comment < Model
  attributes :body, :date
  associations :post, :author, :likes
end
class CommentSerializer < ActiveModel::Serializer
  cache expires_in: 1.day, skip_digest: true
  attributes :id, :body
  belongs_to :post
  belongs_to :author
end
class CommentPreviewSerializer < ActiveModel::Serializer
  attributes :id

  belongs_to :post
end

class Post < Model
  attributes :title, :body
  associations :author, :comments, :blog, :tags, :related
end
class PostSerializer < ActiveModel::Serializer
  cache key: 'post', expires_in: 0.1, skip_digest: true
  attributes :id, :title, :body

  has_many :comments
  belongs_to :blog
  belongs_to :author

  def blog
    Blog.new(id: 999, name: 'Custom blog')
  end
end
class SpammyPostSerializer < ActiveModel::Serializer
  attributes :id
  has_many :related
end
class PostPreviewSerializer < ActiveModel::Serializer
  attributes :title, :body, :id

  has_many :comments, serializer: ::CommentPreviewSerializer
  belongs_to :author, serializer: ::AuthorPreviewSerializer
end
class PostWithCustomKeysSerializer < ActiveModel::Serializer
  attributes :id
  has_many :comments, key: :reviews
  belongs_to :author, key: :writer
  has_one :blog, key: :site
end

class Bio < Model
  attributes :content, :rating
  associations :author
end
class BioSerializer < ActiveModel::Serializer
  cache except: [:content], skip_digest: true
  attributes :id, :content, :rating

  belongs_to :author
end

class Blog < Model
  attributes :name, :type, :special_attribute
  associations :writer, :articles
end
class BlogSerializer < ActiveModel::Serializer
  cache key: 'blog'
  attributes :id, :name

  belongs_to :writer
  has_many :articles
end
class AlternateBlogSerializer < ActiveModel::Serializer
  attribute :id
  attribute :name, key: :title
end
class CustomBlogSerializer < ActiveModel::Serializer
  attribute :id
  attribute :special_attribute
  has_many :articles
end

class Role < Model
  attributes :name, :description, :special_attribute
  associations :author
end
class RoleSerializer < ActiveModel::Serializer
  cache only: [:name, :slug], skip_digest: true
  attributes :id, :name, :description
  attribute :friendly_id, key: :slug
  belongs_to :author

  def friendly_id
    "#{object.name}-#{object.id}"
  end
end

class Location < Model
  attributes :lat, :lng
  associations :place
end
class LocationSerializer < ActiveModel::Serializer
  cache only: [:address], skip_digest: true
  attributes :id, :lat, :lng

  belongs_to :place, key: :address

  def place
    'Nowhere'
  end
end

class Place < Model
  attributes :name
  associations :locations
end
class PlaceSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :locations
end

class Like < Model
  attributes :time
  associations :likeable
end
class LikeSerializer < ActiveModel::Serializer
  attributes :id, :time
  belongs_to :likeable
end

module Spam
  class UnrelatedLink < Model
  end
  class UnrelatedLinkSerializer < ActiveModel::Serializer
    cache only: [:id]
    attributes :id
  end
end

class VirtualValue < Model; end
class VirtualValueSerializer < ActiveModel::Serializer
  attributes :id
  has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
                                     { type: 'reviews', id: '2' }]
  has_one :maker, virtual_value: { type: 'makers', id: '1' }

  def reviews
  end

  def maker
  end
end

class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
  def json_key
    'paginated'
  end
end