-
Notifications
You must be signed in to change notification settings - Fork 8
/
author.rb
95 lines (75 loc) · 1.84 KB
/
author.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
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
require_relative 'base'
DB.create_table?(:authors) do
primary_key :id
String :email, null: false, unique: true
String :real_name
String :display_name
TrueClass :admin, default: false
Float :created_at
Float :updated_at
end
class Author < Sequel::Model
plugin :auto_validations, not_null: :presence
plugin :boolean_readers
plugin :finder
plugin :timestamps
set_allowed_columns :email, :real_name, :display_name, :admin
finder def self.by_email(arg)
where(email: arg)
end
one_to_many :comments
one_to_many :posts
end
# We have to create an admin user here, otherwise we have no way to create one.
Author.create(email: 'all@yourbase.com', admin: true) if Author.where(admin: true).empty?
class AuthorSerializer < BaseSerializer
attribute(:display_name) { object.display_name || 'Anonymous Coward' }
has_many :comments
has_many :posts
end
AuthorController = proc do
helpers do
def before_create(attr)
halt 403, 'Only admins can admin admins' if attr.key?(:admin) && !role?(:superuser)
end
alias before_update before_create
def find(id)
Author.with_pk(id.to_i)
end
def role
Array(super).tap do |a|
a << :self if resource == current_user
end
end
end
show
show_many do |ids|
Author.where_all(id: ids.map!(&:to_i))
end
index do
Author.dataset
end
create do |attr|
author = Author.new(attr)
author.save(validate: false)
next_pk author
end
update(roles: %i[self superuser]) do |attr|
resource.set(attr)
resource.save_changes(validate: false)
end
destroy(roles: %i[self superuser]) do
resource.destroy
end
has_many :comments do
fetch(roles: :logged_in) do
resource.comments_dataset
end
end
has_many :posts do
fetch do
resource.posts_dataset
end
end
end