batch_translations 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "batch_translations"
3
+ s.version = "0.1"
4
+ s.date = "2011-11-06"
5
+ s.summary = "allow saving multiple globalize2 translations in the same request"
6
+ s.email = "sebcioz@gmail.com"
7
+ s.homepage = "https://github.com/sebcioz/batch_translations"
8
+ s.description = "Helper that renders globalize_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."
9
+ s.require_path = "lib"
10
+ s.has_rdoc = false
11
+ s.authors = ['Szymon Fiedler']
12
+ s.files = ["batch_translations.gemspec", 'lib/batch_translation.rb'] +
13
+ Dir.glob("lib/**/*")
14
+ end
@@ -0,0 +1,49 @@
1
+ module ActionView
2
+ module Helpers
3
+ class FormBuilder
4
+ def globalize_fields_for(locale, *args, &proc)
5
+ raise ArgumentError, "Missing block" unless block_given?
6
+ @index = @index ? @index + 1 : 1
7
+ object_name = "#{@object_name}[translations_attributes][#{@index}]"
8
+ object = @object.translations.find_by_locale locale.to_s
9
+ @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
10
+ @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
11
+ if @template.respond_to? :simple_fields_for
12
+ @template.simple_fields_for(object_name, object, *args, &proc)
13
+ else
14
+ @template.fields_for(object_name, object, *args, &proc)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module Globalize
22
+ module Model
23
+ module ActiveRecord
24
+ module Translated
25
+ module Callbacks
26
+ def enable_nested_attributes
27
+ accepts_nested_attributes_for :translations
28
+ end
29
+ end
30
+ module InstanceMethods
31
+ def after_save
32
+ init_translations
33
+ end
34
+ # Builds an empty translation for each available
35
+ # locale not in use after creation
36
+ def init_translations
37
+ I18n.translated_locales.reject{|key| key == :root }.each do |locale|
38
+ translation = self.translations.find_by_locale locale.to_s
39
+ if translation.nil?
40
+ translations.build :locale => locale
41
+ save
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batch_translations
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Szymon Fiedler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-06 00:00:00.000000000 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Helper that renders globalize_translations fields on a per-locale basis,
16
+ so you can use them separately in the same form and still saving them all at once
17
+ in the same request.
18
+ email: sebcioz@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - batch_translations.gemspec
24
+ - lib/batch_translation.rb
25
+ has_rdoc: false
26
+ homepage: https://github.com/sebcioz/batch_translations
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.6.2
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: allow saving multiple globalize2 translations in the same request
50
+ test_files: []