Class: Locallingo::Validators::DuplicateValues
- Inherits:
-
Object
- Object
- Locallingo::Validators::DuplicateValues
- Defined in:
- lib/locallingo/validators/duplicate_values.rb
Overview
Detects source-locale keys whose value matches an
activerecord.attributes.* value. The AR key wins; the non-AR key is the
"duplicate" and should reuse the AR key instead.
Operates on the flat source hash only, to avoid false positives from grammatical-form differences in non-English locales.
Constant Summary collapse
- AR_ATTRIBUTES_PREFIX =
"activerecord.attributes."- AR_PREFIX =
"activerecord."
Instance Method Summary collapse
-
#call(source:) ⇒ Object
sourceis the flat source (en) hash.
Instance Method Details
#call(source:) ⇒ Object
source is the flat source (en) hash. Returns :duplicate_value
violations naming both keys.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/locallingo/validators/duplicate_values.rb', line 17 def call(source:) ar_keys_by_value = source .select { |key, _| key.start_with?(AR_ATTRIBUTES_PREFIX) } .group_by { |_key, value| value } .transform_values { |pairs| pairs.map(&:first) } source.filter_map do |key, value| # Skip ALL activerecord.* keys (models, attributes, etc.). Rails # reserves this namespace for AR-generated translations, and model # names colliding with attribute labels is intentional, not a dup. next if key.start_with?(AR_PREFIX) next unless ar_keys_by_value.key?(value) ar_dupes = ar_keys_by_value[value] { type: :duplicate_value, locale: "en", key:, suggestion: "Value '#{value}' duplicates #{ar_dupes.join(", ")}. " \ "Use #{ar_dupes.first} instead." } end end |