Sha256: 3ebf0dd99789eb227afd72641a0791e9487dee41fca6e3492f1c2e9ae2427531

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
require "foundation_rails_helper/form_builder"

module Decidim
  # This custom FormBuilder adds fields needed to deal with translatable fields,
  # following the conventions set on `Decidim::TranslatableAttributes`.
  class FormBuilder < FoundationRailsHelper::FormBuilder
    # Public: Generates an form field for each locale.
    #
    # type - The form field's type, like `text_area` or `text_input`
    # name - The name of the field
    # options - The set of options to send to the field
    #
    # Renders form fields for each locale.
    def translated(type, name, options = {})
      locales.map do |locale|
        label = options[:label] || label_for(name)
        language = locale

        send(type, "#{name}_#{locale}", options.merge(label: "#{label} (#{language})"))
      end.join.html_safe
    end

    private

    def locales
      I18n.available_locales
    end

    def label_for(attribute)
      if object.class.respond_to?(:human_attribute_name)
        object.class.human_attribute_name(attribute)
      else
        attribute.to_s.humanize
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-core-0.0.1.alpha7 lib/decidim/form_builder.rb
decidim-core-0.0.1.alpha6 lib/decidim/form_builder.rb
decidim-core-0.0.1.alpha5 lib/decidim/form_builder.rb
decidim-core-0.0.1.alpha4 lib/decidim/form_builder.rb
decidim-core-0.0.1.alpha3 lib/decidim/form_builder.rb