Module: CamaleonCms::NormalizeAttrs

Included in:
Meta, PostComment, TermTaxonomy
Defined in:
app/models/concerns/camaleon_cms/normalize_attrs.rb

Instance Method Summary collapse

Instance Method Details

#normalize_attrs(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/concerns/camaleon_cms/normalize_attrs.rb', line 5

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