batch_translations 0.1.2 → 0.1.3

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.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "batch_translations"
3
- s.version = "0.1.2"
3
+ s.version = "0.1.3"
4
4
  s.date = "2011-11-06"
5
5
  s.summary = "allow saving multiple globalize2 translations in the same request"
6
6
  s.email = "sebcioz@gmail.com"
@@ -3,13 +3,23 @@ module ActionView
3
3
  class FormBuilder
4
4
  def globalize_fields_for(locale, *args, &proc)
5
5
  raise ArgumentError, "Missing block" unless block_given?
6
- @index = @index ? @index + 1 : 1
6
+ @locale_index ||= {}
7
+
8
+ if @locale_index[locale].nil?
9
+ @index = @index ? @index + 1 : 1
10
+ @locale_index[locale] = @index
11
+ else
12
+ @index = @locale_index[locale]
13
+ end
14
+
7
15
  object_name = "#{@object_name}[translations_attributes][#{@index}]"
8
- object = @object.translations.find_by_locale locale.to_s
16
+ object = @object.translations.select{|t| t.locale.to_s == locale.to_s}.first || @object.translations.find_by_locale(locale.to_s)
9
17
  @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
10
18
  @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
11
19
  if @template.respond_to? :simple_fields_for
12
20
  @template.simple_fields_for(object_name, object, *args, &proc)
21
+ elsif @template.respond_to? :semantic_fields_for
22
+ @template.semantic_fields_for(object_name, object, *args, &proc)
13
23
  else
14
24
  @template.fields_for(object_name, object, *args, &proc)
15
25
  end
@@ -21,15 +31,13 @@ end
21
31
  module Globalize
22
32
  module ActiveRecord
23
33
  module InstanceMethods
24
- def update_attributes_with_translations(options)
25
- options.each do |key, value|
34
+ def update_attributes_with_translations(attributes)
35
+ attributes.each do |key, value|
26
36
  if key == "translations_attributes"
27
- translated_attrs = {}
28
- value.each do |rec_id, rec_value|
29
- rec_value.delete("id")
30
- translated_attrs[rec_value.delete("locale")] = rec_value
37
+ value.each do |rec_index, rec_value|
38
+ locale = rec_value.delete("locale")
39
+ write_attribute(rec_value.keys.first, rec_value.values.first, options = { :locale => locale })
31
40
  end
32
- self.set_translations(translated_attrs)
33
41
  else
34
42
  self.update_attribute(key, value)
35
43
  end
@@ -50,7 +58,7 @@ module Globalize
50
58
  def after_save
51
59
  init_translations
52
60
  end
53
- # Builds an empty translation for each available
61
+ # Builds an empty translation for each available
54
62
  # locale not in use after creation
55
63
  def init_translations
56
64
  I18n.translated_locales.reject{|key| key == :root }.each do |locale|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-06 00:00:00.000000000 +01:00
13
- default_executable:
12
+ date: 2011-11-06 00:00:00.000000000 Z
14
13
  dependencies: []
15
14
  description: Helper that renders globalize_translations fields on a per-locale basis,
16
15
  so you can use them separately in the same form and still saving them all at once
@@ -22,7 +21,6 @@ extra_rdoc_files: []
22
21
  files:
23
22
  - batch_translations.gemspec
24
23
  - lib/batch_translations.rb
25
- has_rdoc: false
26
24
  homepage: https://github.com/sebcioz/batch_translations
27
25
  licenses: []
28
26
  post_install_message:
@@ -43,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
41
  version: '0'
44
42
  requirements: []
45
43
  rubyforge_project:
46
- rubygems_version: 1.6.2
44
+ rubygems_version: 1.8.24
47
45
  signing_key:
48
46
  specification_version: 3
49
47
  summary: allow saving multiple globalize2 translations in the same request