globalize3 0.0.11 → 0.1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,6 +1,6 @@
1
1
  h1. Globalize3
2
2
 
3
- Globalize3 is the successor of Globalize for Rails. Globalize is targeted at ActiveRecord 3. It is compatible with and builds on the new "I18n api in Ruby on Rails":http://guides.rubyonrails.org/i18n.html and adds model translations to ActiveRecord.
3
+ Globalize3 is the successor of Globalize for Rails. Globalize is targeted at ActiveRecord 3. It is compatible with and builds on the new "I18n API in Ruby on Rails":http://guides.rubyonrails.org/i18n.html and adds model translations to ActiveRecord.
4
4
 
5
5
  Globalize3 is much more lightweight and compatible than its predecessor Globalize for Rails was. Model translations in Globalize3 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
6
6
 
@@ -56,6 +56,31 @@ end
56
56
 
57
57
  Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
58
58
 
59
+ h2. Migrating existing data to and from the translated version
60
+
61
+ As well as creating a translation table, you can also use @create_translation_table!@ to migrate across any
62
+ existing data to the default locale. This can also operate in reverse to restore any translations from the default locale
63
+ back to the model when you don't want to use a translation table anymore using @drop_translation_table!@
64
+
65
+ This feature makes use of @untranslated_fields@ which allows access to the model's attributes as they were before
66
+ the translation was applied. Here's an example (which assumes you already have a model called @Post@ and its table exists):
67
+
68
+ <pre><code>
69
+ class TranslatePosts < ActiveRecord::Migration
70
+ def self.up
71
+ Post.create_translation_table!({
72
+ :title => :string,
73
+ :text => :text
74
+ }, {
75
+ :migrate_data => true
76
+ })
77
+ end
78
+ def self.down
79
+ Post.drop_translation_table! :migrate_data => true
80
+ end
81
+ end
82
+ </code></pre>
83
+
59
84
  h2. Versioning with Globalize3
60
85
 
61
86
  Globalize3 nicely integrates with "vestal_versions":http://github.com/laserlemon/vestal_versions:
@@ -74,7 +74,7 @@ module Globalize
74
74
  value, requested_locale = nil, locale
75
75
 
76
76
  Globalize.fallbacks(locale).each do |fallback|
77
- translation = translations.detect { |t| t.locale == fallback }
77
+ translation = translations.to_a.detect { |t| t.locale == fallback }
78
78
  value = translation && translation.send(name)
79
79
  locale = fallback && break if value
80
80
  end
@@ -97,3 +97,4 @@ module Globalize
97
97
  end
98
98
  end
99
99
  end
100
+
@@ -43,7 +43,7 @@ module Globalize
43
43
  if(klass.nil? || (klass.class_name != (self.class_name + "Translation")))
44
44
  klass = self.const_set(:Translation, Class.new(Globalize::ActiveRecord::Translation))
45
45
  end
46
-
46
+
47
47
  if klass.table_name == 'translations'
48
48
  klass.set_table_name(translation_options[:table_name])
49
49
  klass.belongs_to name.underscore.gsub('/', '_')
@@ -76,10 +76,10 @@ module Globalize
76
76
 
77
77
  def translated_attr_accessor(name)
78
78
  define_method(:"#{name}=") do |value|
79
- write_attribute(name, value, Globalize.locale)
79
+ write_attribute(name, value)
80
80
  end
81
81
  define_method(name) do |*args|
82
- read_attribute(name, args.first || Globalize.locale)
82
+ read_attribute(name, {:locale => args.first})
83
83
  end
84
84
  alias_method :"#{name}_before_type_cast", name
85
85
  end
@@ -23,26 +23,42 @@ module Globalize
23
23
  with_given_locale(attributes) { super }
24
24
  end
25
25
 
26
- def write_attribute(name, value, locale = nil)
27
- # Make sure that we return some value as some methods might
26
+ def write_attribute(name, value, options = {})
27
+ # Make sure that we return some value as some methods might
28
28
  # rely on the data
29
29
  return_value = super(name, value)
30
- return_value = globalize.write(locale || Globalize.locale, name, value) if translated?(name)
30
+
31
+ if translated?(name)
32
+ # Deprecate old use of locale
33
+ unless options.is_a?(Hash)
34
+ warn "[DEPRECATION] passing 'locale' as #{options.inspect} is deprecated. Please use {:locale => #{options.inspect}} instead."
35
+ options = {:locale => options}
36
+ end
37
+ options = {:locale => nil}.merge(options)
38
+ return_value = globalize.write(options[:locale] || Globalize.locale, name, value)
39
+ end
31
40
  return_value
32
41
  end
33
42
 
34
- def read_attribute(name, locale = nil)
35
- if self.class.translated?(name)
36
- globalize.fetch(locale || Globalize.locale, name)
43
+ def read_attribute(name, options = {})
44
+ # Deprecate old use of locale
45
+ unless options.is_a?(Hash)
46
+ warn "[DEPRECATION] passing 'locale' as #{options.inspect} is deprecated. Please use {:locale => #{options.inspect}} instead."
47
+ options = {:locale => options}
48
+ end
49
+
50
+ options = {:translated => true, :locale => nil}.merge(options)
51
+ if self.class.translated?(name) and options[:translated]
52
+ globalize.fetch(options[:locale] || Globalize.locale, name)
37
53
  else
38
54
  super(name)
39
55
  end
40
56
  end
41
-
57
+
42
58
  def attribute_names
43
59
  translated_attribute_names.map(&:to_s) + super
44
60
  end
45
-
61
+
46
62
  def translated?(name)
47
63
  self.class.translated?(name)
48
64
  end
@@ -53,6 +69,16 @@ module Globalize
53
69
  end
54
70
  end
55
71
 
72
+ # This method is basically the method built into Rails
73
+ # but we have to pass {:translated => false}
74
+ def untranslated_attributes
75
+ attrs = {}
76
+ attribute_names.each do |name|
77
+ attrs[name] = read_attribute(name, {:translated => false})
78
+ end
79
+ attrs
80
+ end
81
+
56
82
  def set_translations(options)
57
83
  options.keys.each do |locale|
58
84
  translation = translations.find_by_locale(locale.to_s) ||
@@ -71,11 +71,28 @@ module Globalize
71
71
  end
72
72
 
73
73
  def move_data_to_translation_table
74
- # TODO
74
+ # Find all of the existing untranslated attributes for this model.
75
+ all_model_fields = @model.all
76
+ model_attributes = all_model_fields.collect {|m| m.untranslated_attributes}
77
+ all_model_fields.each do |model_record|
78
+ # Assign the attributes back to the model which will enable globalize3 to translate them.
79
+ model_record.attributes = model_attributes.detect{|a| a['id'] == model_record.id}
80
+ model_record.save!
81
+ end
75
82
  end
76
83
 
77
84
  def move_data_to_model_table
78
- # TODO
85
+ # Find all of the translated attributes for all records in the model.
86
+ all_translated_attributes = @model.all.collect{|m| m.attributes}
87
+ all_translated_attributes.each do |translated_record|
88
+ # Create a hash containing the translated column names and their values.
89
+ translated_attribute_names.inject(fields_to_update={}) do |f, name|
90
+ f.update({name.to_sym => translated_record[name.to_s]})
91
+ end
92
+
93
+ # Now, update the actual model's record with the hash.
94
+ @model.update_all(fields_to_update, {:id => translated_record['id']})
95
+ end
79
96
  end
80
97
 
81
98
  def validate_translated_fields
@@ -1,3 +1,3 @@
1
1
  module Globalize3
2
- VERSION = '0.0.11'
2
+ VERSION = '0.1.0.beta'
3
3
  end
@@ -3,8 +3,7 @@ require 'active_record/serializers/xml_serializer'
3
3
  ActiveRecord::XmlSerializer::Attribute.class_eval do
4
4
  def compute_type_with_translations
5
5
  klass = @serializable.class
6
- if klass.respond_to?(:translated_attribute_names) &&
7
- klass.translated_attribute_names.include?(name.to_sym)
6
+ if klass.translates? && klass.translated_attribute_names.include?(name.to_sym)
8
7
  :string
9
8
  else
10
9
  compute_type_without_translations
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalize3
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease:
4
+ hash: 31098185
5
+ prerelease: 6
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 11
10
- version: 0.0.11
10
+ - beta
11
+ version: 0.1.0.beta
11
12
  platform: ruby
12
13
  authors:
13
14
  - Sven Fuchs
@@ -18,7 +19,7 @@ autorequire:
18
19
  bindir: bin
19
20
  cert_chain: []
20
21
 
21
- date: 2011-01-11 00:00:00 +01:00
22
+ date: 2011-01-12 00:00:00 +01:00
22
23
  default_executable:
23
24
  dependencies:
24
25
  - !ruby/object:Gem::Dependency
@@ -192,12 +193,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
193
  required_rubygems_version: !ruby/object:Gem::Requirement
193
194
  none: false
194
195
  requirements:
195
- - - ">="
196
+ - - ">"
196
197
  - !ruby/object:Gem::Version
197
- hash: 3
198
+ hash: 25
198
199
  segments:
199
- - 0
200
- version: "0"
200
+ - 1
201
+ - 3
202
+ - 1
203
+ version: 1.3.1
201
204
  requirements: []
202
205
 
203
206
  rubyforge_project: "[none]"