-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathisolation_segment_model.rb
More file actions
42 lines (32 loc) · 1.61 KB
/
isolation_segment_model.rb
File metadata and controls
42 lines (32 loc) · 1.61 KB
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
module VCAP::CloudController
class IsolationSegmentModel < Sequel::Model(:isolation_segments)
SHARED_ISOLATION_SEGMENT_GUID = '933b4c58-120b-499a-b85d-4b6fc9e2903b'.freeze
include Serializer
ISOLATION_SEGMENT_MODEL_REGEX = /\A[[:print:]]+\Z/
one_to_many :spaces, key: :isolation_segment_guid, primary_key: :guid
one_to_many :labels, class: 'VCAP::CloudController::IsolationSegmentLabelModel', key: :resource_guid, primary_key: :guid
one_to_many :annotations, class: 'VCAP::CloudController::IsolationSegmentAnnotationModel', key: :resource_guid, primary_key: :guid
many_to_many :organizations,
left_key: :isolation_segment_guid,
left_primary_key: :guid,
right_key: :organization_guid,
right_primary_key: :guid,
join_table: :organizations_isolation_segments, without_guid_generation: true
add_association_dependencies labels: :destroy
add_association_dependencies annotations: :destroy
def around_save
yield
rescue Sequel::UniqueConstraintViolation => e
raise e unless e.message.include?('isolation_segment_name_unique_constraint')
errors.add(:name, :unique)
raise validation_failed_error
end
def validate
validates_format ISOLATION_SEGMENT_MODEL_REGEX, :name, message: Sequel.lit('Isolation Segment names can only contain non-blank unicode characters')
validates_unique [:name], message: Sequel.lit('Isolation Segment names are case insensitive and must be unique')
end
def is_shared_segment?
guid == SHARED_ISOLATION_SEGMENT_GUID
end
end
end