-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
poro.rb
85 lines (66 loc) · 1.52 KB
/
poro.rb
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
class Model
def initialize(hash={})
@attributes = hash
end
def read_attribute_for_serialization(name)
if name == :id || name == 'id'
id
else
@attributes[name]
end
end
def id
@attributes[:id] || @attributes['id'] || object_id
end
def method_missing(meth, *args)
if meth.to_s =~ /^(.*)=$/
@attributes[$1.to_sym] = args[0]
elsif @attributes.key?(meth)
@attributes[meth]
else
super
end
end
end
class Profile < Model
end
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description
urls :posts, :comments
end
Post = Class.new(Model)
Comment = Class.new(Model)
Author = Class.new(Model)
Bio = Class.new(Model)
Blog = Class.new(Model)
Role = Class.new(Model)
PostSerializer = Class.new(ActiveModel::Serializer) do
attributes :title, :body, :id
has_many :comments
belongs_to :author
url :comments
end
CommentSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :body
belongs_to :post
belongs_to :author
end
AuthorSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :name
has_many :posts, embed: :ids
has_many :roles, embed: :ids
belongs_to :bio
end
RoleSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :name
belongs_to :author
end
BioSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :content
belongs_to :author
end
BlogSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :name
belongs_to :writer
has_many :articles
end