Sha256: d9072bae05193c2e4f9e80cdb3ef4330cc0119d8a77ac92c0995fe9a549a6206

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module ActionView
  module Helpers
    class FormBuilder
      #
      # Helper that renders translations fields
      # on a per-locale basis, so you can use them separately
      # in the same form and still saving them all at once
      # in the same request.
      #
      # Use it like this:
      #
      # <h1>Editing post</h1>
      #
      # <% form_for(@post) do |f| %>
      #   <%= f.error_messages %>
      #
      #   <h2>English (default locale)</h2>
      #   <p><%= f.text_field :title %></p>
      #   <p><%= f.text_field :teaser %></p>
      #   <p><%= f.text_field :body %></p>
      #
      #   <hr/>
      #
      #   <h2>Spanish translation</h2>
      #   <% f.globalize_fields_for :es do |g| %>
      #     <p><%= g.text_field :title %></p>
      #     <p><%= g.text_field :teaser %></p>
      #     <p><%= g.text_field :body %></p>
      #   <% end %>
      #
      #   <hr/>
      #
      #   <h2>French translation</h2>
      #   <% f.globalize_fields_for :fr do |g| %>
      #     <p><%= g.text_field :title %></p>
      #     <p><%= g.text_field :teaser %></p>
      #     <p><%= g.text_field :body %></p>
      #   <% end %>
      #
      # <% end %>
      #
      def globalize_fields_for(locale, *args, &proc)
        raise ArgumentError, 'Missing block' unless block_given?
        @locales ||= []

        first = false
        unless @locales.include?(locale)
          @locales << locale
          first = true
        end
        object_name = "#{@object_name}[translations_attributes][#{@locales.index(locale) + 1}]"
        object = @object.translations.find_by locale: locale.to_s

        # The following tags are added only once for the first time.
        if first
          @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : '')
          @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
        end

        @template.fields_for(object_name, object, *args, &proc)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dynamic_scaffold-0.9.0 lib/dynamic_scaffold/form_builder.rb