Sha256: 0dd0fdb69ff0e2a31641a5030e7f56a151ecb8e53138b6fe72c0522e8ddcdb22

Contents?: true

Size: 1.61 KB

Versions: 23

Compression:

Stored size: 1.61 KB

Contents

## https://github.com/fidel/batch_translations/
module ActionView
  module Helpers
    class FormBuilder
      def globalize_fields_for(locale, *args, &proc)
        raise ArgumentError, "Missing block" unless block_given?
        @index = @index ? @index + 1 : 1
        object_name = "#{@object_name}[translations_attributes][#{@index}]"
        object = @object.translations.find_by_locale locale.to_s
        @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
        @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
        if @template.respond_to? :simple_fields_for
          @template.simple_fields_for(object_name, object, *args, &proc)
        else
          @template.fields_for(object_name, object, *args, &proc)
        end
      end
    end
  end
end

module Globalize
  module Model
    module ActiveRecord
      module Translated
        module Callbacks
          def enable_nested_attributes
            accepts_nested_attributes_for :translations
          end
        end
        module InstanceMethods
          def after_save
            init_translations
          end
          # Builds an empty translation for each available 
          # locale not in use after creation
          def init_translations
            I18n.translated_locales.reject{|key| key == :root }.each do |locale|
              translation = self.translations.find_by_locale locale.to_s
              if translation.nil?
                translations.build :locale => locale
                save
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
magic_locales-0.0.21 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.20 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.19 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.18 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.17 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.17.beta2 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.17.beta1 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.16 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.15 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.14 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.13 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.12 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.11 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.10 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.9 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.8 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.7 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.6 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.5 lib/magic_locales/globalized_fields.rb
magic_locales-0.0.4 lib/magic_locales/globalized_fields.rb