generated from BCDevOps/opendev-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsite_configuration.rb
More file actions
136 lines (116 loc) · 3.68 KB
/
site_configuration.rb
File metadata and controls
136 lines (116 loc) · 3.68 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class SiteConfiguration < ApplicationRecord
include Auditable
# Ensures that only one SiteConfiguration record can be created
before_create :ensure_single_record
validate :validate_help_link_items
has_many :revision_reasons
belongs_to :small_scale_requirement_template,
class_name: "RequirementTemplate",
optional: true
# In the future, add new landing page templates like so:
# belongs_to :medium_scale_requirement_template,
# class_name: "RequirementTemplate",
# optional: true
# belongs_to :large_scale_requirement_template,
# class_name: "RequirementTemplate",
# optional: true
accepts_nested_attributes_for :revision_reasons, allow_destroy: true
validate :max_undiscarded_revision_reasons
validate :max_revision_reasons
HELP_LINK_KEYS = %w[
get_started_link_item
best_practices_link_item
dictionary_link_item
user_guide_link_item
]
def self.instance
first_or_create
end
# This override allows discarding of reasons and updating them by reason_code
# if a discarded reason of a particular code is found and updated, it will be undiscarded.
def revision_reasons_attributes=(attributes)
attributes.each do |attribute, _|
next unless attribute["id"].blank? && attribute["reason_code"].present?
existing_reason =
self.revision_reasons.with_discarded.find_by(
reason_code: attribute["reason_code"]
)
next unless existing_reason.present?
# Undiscard and update the record
attribute["id"] = existing_reason.id
attribute["discarded_at"] = nil
end
super(attributes)
end
private
def max_revision_reasons
if revision_reasons.count > 200
errors.add(
:revision_reasons,
I18n.t(
"activerecord.errors.models.site_configuration.attributes.revision_reasons.max_records"
)
)
end
end
def max_undiscarded_revision_reasons
if revision_reasons.kept.count > 20
errors.add(
:revision_reasons,
I18n.t(
"activerecord.errors.models.site_configuration.attributes.revision_reasons.max_undiscarded_records"
)
)
end
end
# A private method to ensure only one record exists
def ensure_single_record
if SiteConfiguration.count > 0
errors.add(
:base,
I18n.t("activerecord.errors.models.site_configuration.single_record")
)
end
end
def validate_help_link_items
return unless help_link_items.present?
help_link_items.each do |key, item|
# Check if item should show
if item["show"]
unless item["href"].present? && item["title"].present? &&
item["description"].present?
errors.add(
:base,
I18n.t(
"activerecord.errors.models.site_configuration.attributes.help_link_items.incomplete",
link: key
)
)
end
end
# Check if href is a valid URL using the same logic as validate_url_attributes
if item["href"].present?
begin
uri = URI.parse(item["href"])
unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
errors.add(
:base,
I18n.t(
"activerecord.errors.models.site_configuration.attributes.help_link_items.invalid_url",
link: key
)
)
end
rescue URI::InvalidURIError
errors.add(
:base,
I18n.t(
"activerecord.errors.models.site_configuration.attributes.help_link_items.invalid_url",
link: key
)
)
end
end
end
end
end