Sha256: e027651c47c143a1ebd0d83fdc1ac6592f6f6464a6861ba0e98c0e42daaa44b3

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 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

12 entries across 12 versions & 1 rubygems

Version Path
decidim-term_customizer-0.21.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.20.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.19.1 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.19.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.18.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.17.1 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.17.0 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.16.6 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.16.5 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.16.3 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.16.2 app/forms/decidim/term_customizer/admin/translation_form.rb
decidim-term_customizer-0.16.1 app/forms/decidim/term_customizer/admin/translation_form.rb