-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathtenants_controller.rb
43 lines (37 loc) · 1.28 KB
/
tenants_controller.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
module Api
class TenantsController < BaseController
INVALID_TENANT_ATTRS = %w(id href ancestry).freeze
include Subcollections::CustomButtonEvents
include Subcollections::Tags
include Subcollections::Quotas
def create_resource(_type, _id, data)
bad_attrs = data_includes_invalid_attrs(data)
if bad_attrs.present?
raise BadRequestError,
"Attribute(s) #{bad_attrs} should not be specified for creating a new tenant resource"
end
parse_set_parent(data)
tenant = Tenant.create(data)
if tenant.invalid?
raise BadRequestError, "Failed to add a new tenant resource - #{tenant.errors.full_messages.join(', ')}"
end
tenant
end
def edit_resource(type, id, data)
bad_attrs = data_includes_invalid_attrs(data)
if bad_attrs.present?
raise BadRequestError, "Attributes #{bad_attrs} should not be specified for updating a tenant resource"
end
parse_set_parent(data)
super
end
private
def parse_set_parent(data)
parent = parse_fetch_tenant(data.delete("parent"))
data["parent"] = parent if parent
end
def data_includes_invalid_attrs(data)
data.keys.select { |k| INVALID_TENANT_ATTRS.include?(k) }.compact.join(", ") if data
end
end
end