-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathnormalize_attrs.rb
More file actions
26 lines (24 loc) · 1.04 KB
/
normalize_attrs.rb
File metadata and controls
26 lines (24 loc) · 1.04 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
# frozen_string_literal: true
module CamaleonCms
module NormalizeAttrs
def normalize_attrs(*args)
# TODO: Remove the 1st branch when support will be dropped of Rails < 7.1
if ::Rails::VERSION::STRING < '7.1.0'
before_validation(on: %i[create update]) do
args.each do |attr|
next unless new_record? || attribute_changed?(attr)
self[attr] = ActionController::Base.helpers.sanitize(
__send__(attr)&.gsub(CamaleonRecord::TRANSLATION_TAG_HIDE_REGEX, CamaleonRecord::TRANSLATION_TAG_HIDE_MAP)
)&.gsub(CamaleonRecord::TRANSLATION_TAG_RESTORE_REGEX, CamaleonRecord::TRANSLATION_TAG_RESTORE_MAP)
end
end
else
normalizes(*args, with: lambda { |field|
ActionController::Base.helpers.sanitize(
field.gsub(CamaleonRecord::TRANSLATION_TAG_HIDE_REGEX, CamaleonRecord::TRANSLATION_TAG_HIDE_MAP)
).gsub(CamaleonRecord::TRANSLATION_TAG_RESTORE_REGEX, CamaleonRecord::TRANSLATION_TAG_RESTORE_MAP)
})
end
end
end
end