@@ -6,15 +6,18 @@ class Document < ApplicationRecord
6
6
7
7
belongs_to :service
8
8
belongs_to :user , optional : true
9
+ belongs_to :document_type , optional : true
9
10
10
11
has_many :points
11
12
has_many :document_comments , dependent : :destroy
12
13
13
14
validates :name , presence : true
14
15
validates :url , presence : true
15
16
validates :service_id , presence : true
17
+ validates :document_type_id , presence : true
16
18
17
- validate :custom_uniqueness_check
19
+ validate :location_uniqueness_check
20
+ validate :document_type_uniqueness_check
18
21
19
22
VALID_NAMES = [
20
23
'Terms of Service' ,
@@ -67,7 +70,7 @@ class Document < ApplicationRecord
67
70
'Business Privacy Policy'
68
71
] . freeze
69
72
70
- def custom_uniqueness_check
73
+ def location_uniqueness_check
71
74
doc = Document . where ( url : url , xpath : xpath , status : nil )
72
75
73
76
return unless doc . any? && ( doc . first . id != id )
@@ -76,6 +79,15 @@ def custom_uniqueness_check
76
79
errors . add ( :url , "A document for this URL already exists! Inspect it here: #{ go_to_doc } " )
77
80
end
78
81
82
+ def document_type_uniqueness_check
83
+ doc = Document . where ( document_type_id : document_type_id , service_id : service_id )
84
+
85
+ return unless doc . any? && ( doc . first . id != id )
86
+
87
+ go_to_doc = Rails . application . routes . url_helpers . document_url ( doc . first . id )
88
+ errors . add ( :document_type_id , "This document already exists for this service! Inspect it here: #{ go_to_doc } " )
89
+ end
90
+
79
91
def convert_xpath_to_css
80
92
runner = NodeRunner . new (
81
93
<<~JAVASCRIPT
0 commit comments