Sha256: c3e6e94ca2ec0fae9317f91faa991ca1a9dd764fe8c2e18374d812ba03a3a836

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      class TranslationForm < Decidim::Form
        include TranslatableAttributes

        mimic :translation

        delegate :translation_set, to: :context, prefix: false, allow_nil: true

        attribute :key, String
        translatable_attribute :value, String

        validates :key, presence: true
        validates :key, format: { with: %r{\A([a-z0-9_/\?\-]+\.)*[a-z0-9_/\?\-]+\z} }, unless: -> { key.blank? }
        validates :value, translatable_presence: true
        validate :key_uniqueness

        def map_model(model)
          self.value = Hash[Decidim::TermCustomizer::Translation.where(
            translation_set: model.translation_set,
            key: model.key
          ).map do |translation|
            [translation.locale, translation.value]
          end]
        end

        def key_uniqueness
          errors.add(:key, :taken) if translation_set && translation_set.translations.where(
            locale: I18n.locale,
            key: key
          ).where.not(id: id).exists?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-term_customizer-0.23.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.22.0 app/forms/decidim/term_customizer/admin/translation_form.rb