has_many_translations 0.3.8 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ group :development do
3
+ gem 'activesupport'
4
+ gem 'activerecord'
5
+ gem 'shoulda'
6
+ gem 'mocha'
7
+ gem 'rspec'
8
+ gem 'cucumber'
9
+ gem 'launchy'
10
+ gem 'pickler'
11
+ gem 'jeweler'
12
+ end
13
+
14
+ gem 'artforge-rtranslate', '>=1.3.4'
15
+ gem 'activequeue'
16
+ gem 'settingslogic'
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.3)
5
+ activesupport (= 3.0.3)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activequeue (0.2.0)
9
+ activerecord (3.0.3)
10
+ activemodel (= 3.0.3)
11
+ activesupport (= 3.0.3)
12
+ arel (~> 2.0.2)
13
+ tzinfo (~> 0.3.23)
14
+ activesupport (3.0.3)
15
+ arel (2.0.6)
16
+ artforge-rtranslate (1.3.4)
17
+ json (>= 1.1.3)
18
+ builder (2.1.2)
19
+ configuration (1.2.0)
20
+ cucumber (0.10.0)
21
+ builder (>= 2.1.2)
22
+ diff-lcs (~> 1.1.2)
23
+ gherkin (~> 2.3.2)
24
+ json (~> 1.4.6)
25
+ term-ansicolor (~> 1.0.5)
26
+ diff-lcs (1.1.2)
27
+ gherkin (2.3.3)
28
+ json (~> 1.4.6)
29
+ git (1.2.5)
30
+ i18n (0.5.0)
31
+ jeweler (1.5.2)
32
+ bundler (~> 1.0.0)
33
+ git (>= 1.2.5)
34
+ rake
35
+ json (1.4.6)
36
+ launchy (0.3.7)
37
+ configuration (>= 0.0.5)
38
+ rake (>= 0.8.1)
39
+ mocha (0.9.10)
40
+ rake
41
+ pickler (0.1.5)
42
+ activesupport (>= 2.0.0)
43
+ cucumber (>= 0.8.0)
44
+ rake (0.8.7)
45
+ rspec (2.4.0)
46
+ rspec-core (~> 2.4.0)
47
+ rspec-expectations (~> 2.4.0)
48
+ rspec-mocks (~> 2.4.0)
49
+ rspec-core (2.4.0)
50
+ rspec-expectations (2.4.0)
51
+ diff-lcs (~> 1.1.2)
52
+ rspec-mocks (2.4.0)
53
+ settingslogic (2.0.6)
54
+ shoulda (2.11.3)
55
+ term-ansicolor (1.0.5)
56
+ tzinfo (0.3.23)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ activequeue
63
+ activerecord
64
+ activesupport
65
+ artforge-rtranslate (>= 1.3.4)
66
+ cucumber
67
+ jeweler
68
+ launchy
69
+ mocha
70
+ pickler
71
+ rspec
72
+ settingslogic
73
+ shoulda
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
  require 'rake/testtask'
4
- require 'rcov/rcovtask'
4
+ # require 'rcov/rcovtask'
5
5
  require 'rake/rdoctask'
6
6
 
7
7
  begin
@@ -10,9 +10,9 @@ begin
10
10
  g.name = 'has_many_translations'
11
11
  g.summary = %(Makes models' speak in tongues)
12
12
  g.description = %(Keep a DRY multilingual translation of your ActiveRecord models' textual attributes)
13
- g.email = 'adam@artforge.com'
14
- g.homepage = 'http://github.com/opengotham/has_many_translations'
15
- g.authors = ["opengotham","Artforge"]
13
+ g.email = 'mjording@opengotham.com'
14
+ g.homepage = 'http://github.com/artforge/has_many_translations'
15
+ g.authors = ["OpenGotham","Artforge"]
16
16
  g.add_dependency 'activerecord', '>= 2.1.0'
17
17
  g.add_development_dependency 'shoulda'
18
18
  g.add_development_dependency 'mocha'
@@ -30,10 +30,10 @@ Rake::TestTask.new do |t|
30
30
  t.pattern = 'test/**/*_test.rb'
31
31
  end
32
32
 
33
- Rcov::RcovTask.new do |t|
34
- t.libs = %w(test)
35
- t.pattern = 'test/**/*_test.rb'
36
- end
33
+ # Rcov::RcovTask.new do |t|
34
+ # t.libs = %w(test)
35
+ # t.pattern = 'test/**/*_test.rb'
36
+ # end
37
37
 
38
38
  task :test => :check_dependencies
39
39
  task :default => :test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.8
1
+ 0.4.3
@@ -0,0 +1,5 @@
1
+ @https://www.pivotaltracker.com/story/show/8303995
2
+ Feature: allow overide of default language options at model level
3
+ As a developer.
4
+ I want to override default_languages for individual models.
5
+ So model specific default languages can be specified.
@@ -0,0 +1,5 @@
1
+ @https://www.pivotaltracker.com/story/show/8304163
2
+ Feature: bypass hmt if current lang == native language
3
+ As a user.
4
+ when user language equals the models current language.
5
+ I want to prevent translation requests.
@@ -0,0 +1,5 @@
1
+ @https://www.pivotaltracker.com/story/show/8303803
2
+ Feature: extend initializer with default language options
3
+ As a developer
4
+ I want to set default_languages as an initialization setting
5
+ So assumed languages can be limited
@@ -0,0 +1,5 @@
1
+ @https://www.pivotaltracker.com/story/show/8303649 @init,options
2
+ Feature: extend initializer with predefined language options
3
+ As a developer.
4
+ I want to define configuration settings within an rails/rack initializer.
5
+ So model translation settings are customizable.
@@ -0,0 +1,2 @@
1
+ project_id: 192345
2
+ ssl: true
@@ -9,7 +9,7 @@ class CreateTranslations < ActiveRecord::Migration
9
9
 
10
10
  create_table :translations do |t|
11
11
  t.belongs_to :translated, :polymorphic => true
12
- t.string :attribute
12
+ t.string :model_attribute
13
13
  t.text :value
14
14
  t.string :locale_code
15
15
  t.string :locale_name
@@ -17,7 +17,7 @@ class CreateTranslations < ActiveRecord::Migration
17
17
  t.boolean :machine_translation
18
18
  t.timestamps
19
19
  end
20
- add_index :translations, [:locale_code, :attribute]
20
+ add_index :translations, [:locale_code, :model_attribute]
21
21
  add_index :translations, [:translated_id, :translated_type]
22
22
 
23
23
  change_table :users do |t|
@@ -5,22 +5,29 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_many_translations}
8
- s.version = "0.3.8"
8
+ s.version = "0.4.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["opengotham", "Artforge"]
12
- s.date = %q{2010-09-29}
11
+ s.authors = ["OpenGotham", "Artforge"]
12
+ s.date = %q{2011-01-26}
13
13
  s.description = %q{Keep a DRY multilingual translation of your ActiveRecord models' textual attributes}
14
- s.email = %q{adam@artforge.com}
14
+ s.email = %q{mjording@opengotham.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
19
  ".gitignore",
20
+ "Gemfile",
21
+ "Gemfile.lock",
20
22
  "History.txt",
21
23
  "README.rdoc",
22
24
  "Rakefile",
23
25
  "VERSION",
26
+ "features/allow_overide_of_default_language_options_at_model_level.feature",
27
+ "features/bypass_hmt_if_current_lang_native_language.feature",
28
+ "features/extend_initializer_with_default_language_options.feature",
29
+ "features/extend_initializer_with_predefined_language_options.feature",
30
+ "features/tracker.yml",
24
31
  "generators/has_many_translations/has_many_translations_generator.rb",
25
32
  "generators/has_many_translations/templates/initializer.rb",
26
33
  "generators/has_many_translations/templates/migration.rb",
@@ -37,13 +44,18 @@ Gem::Specification.new do |s|
37
44
  "lib/has_many_translations/translation_jobs.rb",
38
45
  "lib/has_many_translations/translations.rb",
39
46
  "lib/translation_spec.rb",
47
+ "spec/has_many_translations.spec",
48
+ "spec/spec_helper.rb",
40
49
  "tasks/has_many_translations_tasks.rake"
41
50
  ]
42
- s.homepage = %q{http://github.com/opengotham/has_many_translations}
51
+ s.homepage = %q{http://github.com/artforge/has_many_translations}
43
52
  s.rdoc_options = ["--charset=UTF-8"]
44
53
  s.require_paths = ["lib"]
45
54
  s.rubygems_version = %q{1.3.7}
46
55
  s.summary = %q{Makes models' speak in tongues}
56
+ s.test_files = [
57
+ "spec/spec_helper.rb"
58
+ ]
47
59
 
48
60
  if s.respond_to? :specification_version then
49
61
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -22,6 +22,7 @@ module HasManyTranslations
22
22
  include Options
23
23
  include TranslationJobs
24
24
  include Creation
25
+ include Control
25
26
  include Translations
26
27
  prepare_translated_options(options)
27
28
  has_many :translations, options, &block
@@ -5,12 +5,29 @@ module HasManyTranslations
5
5
  def self.included(base) # :nodoc:
6
6
  base.class_eval do
7
7
  include InstanceMethods
8
- alias_method_chain :create_translation?, :control
9
- alias_method_chain :update_translation?, :control
8
+ # alias_method_chain :create_translation?, :control
9
+ # alias_method_chain :update_translation?, :control
10
10
  end
11
11
  end
12
12
 
13
13
  module InstanceMethods
14
+ def force_update
15
+ with_translation_flag(:force_update) do
16
+ yield if block_given?
17
+ save
18
+ end
19
+ end
20
+ def force_update!
21
+ with_translation_flag(:force_update) do
22
+ yield if block_given?
23
+ update_translations!
24
+ end
25
+ end
26
+
27
+ def force_update?
28
+ !!@force_update
29
+ end
30
+
14
31
  def skip_translation
15
32
  with_translation_flag(:skip_translation) do
16
33
  yield if block_given?
@@ -19,7 +36,7 @@ module HasManyTranslations
19
36
  end
20
37
 
21
38
  def skip_translation!
22
- with_version_flag(:skip_translation) do
39
+ with_translation_flag(:skip_translation) do
23
40
  yield if block_given?
24
41
  save!
25
42
  end
@@ -37,10 +54,10 @@ module HasManyTranslations
37
54
  # block raising an exception.
38
55
  def with_translation_flag(flag)
39
56
  begin
40
- instance_variable_set("@#{flag}", true)
57
+ instance_variable_set("@#{flag}", !instance_variable_get("@#{flag}"))
41
58
  yield
42
- ensure
43
- instance_variable_set("@#{flag}", nil)
59
+ # ensure
60
+ # instance_variable_set("@#{flag}", nil)
44
61
  end
45
62
  end
46
63
 
@@ -29,14 +29,12 @@ module HasManyTranslations
29
29
  #unless try(name)
30
30
  define_method name, lambda { |*args|
31
31
  #
32
- unless self.translations.blank? || self.translations.first.origin_locale_code == self.hmt_locale || read_attribute(name.to_sym).nil?
33
- trans = self.translations.first(:conditions => {:locale_code => self.hmt_locale, :attribute => name})
32
+ if self.translations.blank? || self.translations.first.origin_locale_code == self.hmt_locale || read_attribute(name.to_sym).nil?
33
+ read_attribute(name.to_sym)
34
+ else
35
+ trans = self.translations.first(:conditions => {:locale_code => self.hmt_locale, :model_attribute => name})
34
36
  val = trans.nil? ? read_attribute(name.to_sym) : trans.value
35
37
  #self.hmt_locale
36
- else
37
- #self.id
38
- read_attribute(name.to_sym)
39
- #try(name)
40
38
  end
41
39
  #HasManyTranslations.fetch(args.first || self.class.locale || I18n.locale, name)
42
40
  }
@@ -49,7 +47,12 @@ module HasManyTranslations
49
47
 
50
48
  self.has_many_translations_options[:only] = Array(options.delete(:only)).map(&:to_s).uniq if options[:only]
51
49
  self.has_many_translations_options[:except] = Array(options.delete(:except)).map(&:to_s).uniq if options[:except]
52
- #self.has_many_translations_options[:locales] = Array(options.delete(:locales)).map(&:to_s).uniq if options[:locales]
50
+ self.has_many_translations_options[:locales] = Array(options.delete(:locales)).map(&:to_s).uniq if options[:locales]
51
+ self.has_many_translations_options[:default_languages] = options[:default_languages] ? Array(options.delete(:default_languages)).map(&:to_s).uniq : nil
52
+ self.has_many_translations_options[:languages] = options[:languages] ? Array(options.delete(:languages)).map(&:to_s).uniq : nil
53
+ # self.has_many_translations_options[:force_on_update] = options[:force_on_update] ? Array(options.delete(:force_on_update)).map(&:to_s).uniq : nil
54
+
55
+
53
56
  result
54
57
  end
55
58
 
@@ -65,6 +68,10 @@ module HasManyTranslations
65
68
  @translator.key = Settings.google_api_key
66
69
  end
67
70
 
71
+ def force_on_update?
72
+ return false
73
+ end
74
+
68
75
  def allowed_locales
69
76
  t = TranslationSpec.first(:conditions => {:translated_id => self.id, :translated_type => self.class.to_s})
70
77
  t.blank? ? nil : t.codes.split(',').map{|c| c.to_sym}
@@ -120,9 +127,7 @@ module HasManyTranslations
120
127
  #reset_translation
121
128
  end
122
129
 
123
- # Returns whether the last translation should be updated upon updating the parent record.
124
- # This method is overridden in HasManyTranslations::Control to account for a control block that
125
- # merges changes onto the previous translation.
130
+
126
131
  def update_translations!
127
132
  #translated_columns.each do |attrib|
128
133
  self.locales.each do |loc|
@@ -134,11 +139,14 @@ module HasManyTranslations
134
139
  #end
135
140
  end
136
141
 
142
+ # Returns whether the last translation should be updated upon updating the parent record.
143
+ # This method is overridden in HasManyTranslations::Control to account for a control block that
144
+ # merges changes onto the previous translation.
137
145
  def update_translation?
138
146
  unless self.translations.blank? || self.translations.first.origin_locale_code == self.hmt_locale
139
147
  dirty_translations = self.translations.all(:conditions => {:translated_id => self.id, :locale_code => self.hmt_locale})
140
148
  dirty_translations.each do |dt|
141
- dt.value = try(dt.attribute)
149
+ dt.value = try(dt.model_attribute)
142
150
  dt.save
143
151
  end
144
152
  return false
@@ -153,7 +161,7 @@ module HasManyTranslations
153
161
 
154
162
  # Updates the last translation's changes by appending the current translation changes.
155
163
  def update_translation(attrib, loc, origin_locale)
156
- unless translations.first(:conditions => {:attribute => attrib, :locale_code => loc.to_s})
164
+ if !translations.first(:conditions => {:model_attribute => attrib, :locale_code => loc.to_s}) || self.force_on_update?
157
165
  update_translation!(attrib, loc, origin_locale.to_s)
158
166
  end
159
167
  end
@@ -163,10 +171,8 @@ module HasManyTranslations
163
171
  if defined? HmtSettings
164
172
  @translator.key = HmtSettings.google_api_key
165
173
  end
166
- # translation_val = @translator.translate(try(attrib), :from => origin_locale.to_s, :to => loc.to_s)
167
- translation_val = @translator.post_translate(try(attrib), :from => origin_locale.to_s, :to => loc.to_s)
168
- # translation_val = @translator.translate(try(attrib), origin_locale.to_s, loc.to_s, {:method => :post})
169
- translations.create(:attribute => attrib, :locale_code => loc.to_s, :value => translation_val, :locale_name => Google::Language::Languages[loc.to_s], :machine_translation => true, :origin_locale_code => origin_locale ) unless translation_val.nil? || translation_val.match('Error: ')
174
+ translation_val = @translator.translate(try(attrib), :from => origin_locale.to_s, :to => loc.to_s)
175
+ translations.create(:model_attribute => attrib, :locale_code => loc.to_s, :value => translation_val, :locale_name => Google::Language::Languages[loc.to_s], :machine_translation => true, :origin_locale_code => origin_locale ) unless translation_val.nil? || translation_val.match('Error: ')
170
176
  end
171
177
 
172
178
 
@@ -178,8 +184,13 @@ module HasManyTranslations
178
184
  # columns maintained by Rails are never translationed.
179
185
  def translated_columns
180
186
  textual_columns = self.class.columns.map{|c|c.type == :string || c.type == :text ? c.name : nil}.compact
181
- textual_columns = self.has_many_translations_options[:only] ? textual_columns & self.has_many_translations_options[:only] : textual_columns
182
- textual_columns = self.has_many_translations_options[:except] ? textual_columns - self.has_many_translations_options[:except] : textual_columns
187
+
188
+ if self.has_many_translations_options[:except]
189
+ textual_columns = textual_columns - self.has_many_translations_options[:except]
190
+ elsif self.has_many_translations_options[:only]
191
+ textual_columns = textual_columns & self.has_many_translations_options[:only]
192
+ end
193
+
183
194
  return textual_columns
184
195
  end
185
196
 
@@ -195,15 +206,16 @@ module HasManyTranslations
195
206
 
196
207
  def locales
197
208
  if allowed_locales
198
- retloc = allowed_locales.map{|l|l.to_s}
209
+ retloc = has_many_translations_options[:default_locales] ? (has_many_translations_options[:default_locales] & allowed_locales.map{|l|l.to_s}) : allowed_locales.map{|l|l.to_s}
199
210
  elsif super_locales.present?
200
211
  super_locales.each do |sloc|
201
212
  retloc.nil? ? retloc = eval("self.#{sloc}.locales") : retloc | eval("self.#{sloc}.locales")
202
213
  end
203
- else
204
- retloc = has_many_translations_options[:locales] && I18n && Google ? has_many_translations_options[:locales] & Google::Language::Languages.keys : Google::Language::Languages.keys & I18n.available_locales.map{|l|l.to_s}
205
214
  end
206
- return retloc
215
+ retloc ||= (has_many_translations_options[:locales] && I18n && Google) ? (has_many_translations_options[:locales] & Google::Language::Languages.keys) : (Google::Language::Languages.keys & I18n.available_locales.map{|l|l.to_s})
216
+ if has_many_translations_options[:languages]
217
+ retloc = retloc.empty? ? has_many_translations_options[:languages] : (has_many_translations_options[:languages] & retloc)
218
+ end
207
219
  # I18n.available_locales.map(&:to_s)
208
220
  end
209
221
 
@@ -1,4 +1,24 @@
1
- class HmtSettings < Settingslogic
2
- source "#{Rails.root}/config/settings.yml"
3
- namespace Rails.env
1
+ class HmtSettings
2
+ @@google_api_key = ""
3
+ @@languages = []
4
+ @@default_languages = []
5
+
6
+ def self.google_api_key=(key)
7
+ @@google_api_key = key
8
+ end
9
+ def self.google_api_key
10
+ @@google_api_key
11
+ end
12
+ def self.languages=(langs)
13
+ @@languages = langs
14
+ end
15
+ def self.languages
16
+ @@languages
17
+ end
18
+ def self.default_languages=(langs)
19
+ @@default_languages = langs
20
+ end
21
+ def self.default_languages
22
+ @@default_languages
23
+ end
4
24
  end
@@ -35,6 +35,10 @@ module HasManyTranslations
35
35
  self.translator = Translate::RTranslate.new
36
36
  if defined? HmtSettings
37
37
  self.translator.key = HmtSettings.google_api_key
38
+ options[:default_languages] = HmtSettings.default_languages ? HmtSettings.default_languages : nil
39
+ options[:languages] = HmtSettings.languages ? HmtSettings.languages : nil
40
+ # options[:force_on_update] = HmtSettings.force_on_update ? HmtSettings.force_on_update : nil
41
+
38
42
  end
39
43
  self.has_many_translations_options = options.dup
40
44
 
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HasManyTranslations" do
4
+ it "fails" do
5
+ false.should be_true
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ # Requires supporting files with custom matchers and macros, etc,
5
+ # in ./support/ and its subdirectories.
6
+
7
+
8
+ require 'fileutils'
9
+ require 'rspec'
metadata CHANGED
@@ -1,22 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_many_translations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 4
8
9
  - 3
9
- - 8
10
- version: 0.3.8
10
+ version: 0.4.3
11
11
  platform: ruby
12
12
  authors:
13
- - opengotham
13
+ - OpenGotham
14
14
  - Artforge
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-09-29 00:00:00 -04:00
19
+ date: 2011-01-26 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -108,7 +108,7 @@ dependencies:
108
108
  type: :runtime
109
109
  version_requirements: *id006
110
110
  description: Keep a DRY multilingual translation of your ActiveRecord models' textual attributes
111
- email: adam@artforge.com
111
+ email: mjording@opengotham.com
112
112
  executables: []
113
113
 
114
114
  extensions: []
@@ -117,10 +117,17 @@ extra_rdoc_files:
117
117
  - README.rdoc
118
118
  files:
119
119
  - .gitignore
120
+ - Gemfile
121
+ - Gemfile.lock
120
122
  - History.txt
121
123
  - README.rdoc
122
124
  - Rakefile
123
125
  - VERSION
126
+ - features/allow_overide_of_default_language_options_at_model_level.feature
127
+ - features/bypass_hmt_if_current_lang_native_language.feature
128
+ - features/extend_initializer_with_default_language_options.feature
129
+ - features/extend_initializer_with_predefined_language_options.feature
130
+ - features/tracker.yml
124
131
  - generators/has_many_translations/has_many_translations_generator.rb
125
132
  - generators/has_many_translations/templates/initializer.rb
126
133
  - generators/has_many_translations/templates/migration.rb
@@ -137,9 +144,11 @@ files:
137
144
  - lib/has_many_translations/translation_jobs.rb
138
145
  - lib/has_many_translations/translations.rb
139
146
  - lib/translation_spec.rb
147
+ - spec/has_many_translations.spec
148
+ - spec/spec_helper.rb
140
149
  - tasks/has_many_translations_tasks.rake
141
150
  has_rdoc: true
142
- homepage: http://github.com/opengotham/has_many_translations
151
+ homepage: http://github.com/artforge/has_many_translations
143
152
  licenses: []
144
153
 
145
154
  post_install_message:
@@ -172,5 +181,5 @@ rubygems_version: 1.3.7
172
181
  signing_key:
173
182
  specification_version: 3
174
183
  summary: Makes models' speak in tongues
175
- test_files: []
176
-
184
+ test_files:
185
+ - spec/spec_helper.rb