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
# 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] = CamaleonRecord.cama_sanitize_translatable(__send__(attr))
      end
    end
  else
    normalizes(*args, with: ->(field) { CamaleonRecord.cama_sanitize_translatable(field) })
  end
end