Skip to content

Commit 2234e90

Browse files
committed
implements document type validations for document model
1 parent a929305 commit 2234e90

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

app/models/document.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ class Document < ApplicationRecord
66

77
belongs_to :service
88
belongs_to :user, optional: true
9+
belongs_to :document_type, optional: true
910

1011
has_many :points
1112
has_many :document_comments, dependent: :destroy
1213

1314
validates :name, presence: true
1415
validates :url, presence: true
1516
validates :service_id, presence: true
17+
validates :document_type_id, presence: true
1618

17-
validate :custom_uniqueness_check
19+
validate :location_uniqueness_check
20+
validate :document_type_uniqueness_check
1821

1922
VALID_NAMES = [
2023
'Terms of Service',
@@ -67,7 +70,7 @@ class Document < ApplicationRecord
6770
'Business Privacy Policy'
6871
].freeze
6972

70-
def custom_uniqueness_check
73+
def location_uniqueness_check
7174
doc = Document.where(url: url, xpath: xpath, status: nil)
7275

7376
return unless doc.any? && (doc.first.id != id)
@@ -76,6 +79,15 @@ def custom_uniqueness_check
7679
errors.add(:url, "A document for this URL already exists! Inspect it here: #{go_to_doc}")
7780
end
7881

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+
7991
def convert_xpath_to_css
8092
runner = NodeRunner.new(
8193
<<~JAVASCRIPT

0 commit comments

Comments
 (0)