has_translations 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- HasTranslations v0.3.1
1
+ HasTranslations v0.3.2
2
2
  ======================
3
3
 
4
4
  This simple plugin creates translations for your model.
data/Rakefile CHANGED
@@ -31,7 +31,7 @@ begin
31
31
  gemspec.email = "dmitry.polushkin@gmail.com"
32
32
  gemspec.homepage = "http://github.com/dmitry/has_translations"
33
33
  gemspec.authors = ["Dmitry Polushkin"]
34
- gemspec.version = '0.3.0'
34
+ gemspec.version = '0.3.2'
35
35
  end
36
36
  rescue LoadError
37
37
  puts "Jeweler not available. Install it with: gem install jeweler"
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_translations}
8
+ s.version = "0.3.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dmitry Polushkin"]
12
+ s.date = %q{2011-02-04}
13
+ s.description = %q{Create translations for your ActiveRecord models. Uses delegate pattern. Fully tested and used in a several production sites.}
14
+ s.email = %q{dmitry.polushkin@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "generators/has_translations/USAGE",
23
+ "generators/has_translations/has_translations_generator.rb",
24
+ "has_translations.gemspec",
25
+ "init.rb",
26
+ "install.rb",
27
+ "lib/has_translations.rb",
28
+ "tasks/has_translations_tasks.rake",
29
+ "test/.gitignore",
30
+ "test/has_translations_test.rb",
31
+ "test/schema.rb",
32
+ "test/test_helper.rb",
33
+ "uninstall.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/dmitry/has_translations}
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.5.0}
38
+ s.summary = %q{Create translations for your ActiveRecord models.}
39
+ s.test_files = [
40
+ "test/has_translations_test.rb",
41
+ "test/schema.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
54
+
@@ -89,15 +89,23 @@ class ActiveRecord::Base
89
89
 
90
90
  translation_class_name = "#{self.model_name}Translation"
91
91
  translation_class = translation_class_name.constantize
92
- belongs_to = self.model_name.demodulize.singularize.underscore.to_sym
92
+ belongs_to = self.model_name.demodulize.underscore.to_sym
93
93
 
94
94
  write_inheritable_attribute :has_translations_options, options
95
95
  class_inheritable_reader :has_translations_options
96
96
 
97
- send :define_method, :find_or_build_translation do |*args|
98
- locale = args.first.to_s
99
- build = args.second.present?
100
- find_translation(locale) || (build ? self.translations.build(:locale => locale) : self.translations.new(:locale => locale))
97
+ def find_or_create_translation(locale)
98
+ locale = locale.to_s
99
+ (find_translation(locale) || self.translations.new).tap do |t|
100
+ t.locale = locale
101
+ end
102
+ end
103
+
104
+ def find_or_build_translation(locale)
105
+ locale = locale.to_s
106
+ (find_translation(locale) || self.translations.build).tap do |t|
107
+ t.locale = locale
108
+ end
101
109
  end
102
110
 
103
111
  def translation(locale, fallback=has_translations_options[:fallback])
@@ -107,7 +115,7 @@ class ActiveRecord::Base
107
115
 
108
116
  def all_translations
109
117
  t = I18n.available_locales.map do |locale|
110
- [locale, find_or_build_translation(locale)]
118
+ [locale, find_or_create_translation(locale)]
111
119
  end
112
120
  ActiveSupport::OrderedHash[t]
113
121
  end
@@ -128,7 +136,7 @@ class ActiveRecord::Base
128
136
  if options[:writer]
129
137
  attrs.each do |name|
130
138
  send :define_method, "#{name}=" do |value|
131
- translation = find_or_build_translation(I18n.locale, true)
139
+ translation = find_or_build_translation(I18n.locale)
132
140
  translation.send(:"#{name}=", value)
133
141
  end
134
142
  end
@@ -140,12 +148,10 @@ class ActiveRecord::Base
140
148
  translation_class.validates_presence_of :locale
141
149
  translation_class.validates_uniqueness_of :locale, :scope => :"#{belongs_to}_id"
142
150
 
143
- # Rails 3.0
144
- if Object.const_defined?("ActiveModel")
145
- scope :translated, lambda { |locale| {:conditions => ["#{translation_class.table_name}.locale = ?", locale.to_s], :joins => :translations} }
146
- else
147
- named_scope :translated, lambda { |locale| {:conditions => ["#{translation_class.table_name}.locale = ?", locale.to_s], :joins => :translations} }
148
- end
151
+ # Workaround to support Rails 2
152
+ scope_method = if ActiveRecord::VERSION::MAJOR < 3 then :named_scope else :scope end
153
+
154
+ send scope_method, :translated, lambda { |locale| {:conditions => ["#{translation_class.table_name}.locale = ?", locale.to_s], :joins => :translations} }
149
155
 
150
156
  private
151
157
 
@@ -23,7 +23,10 @@ class HasTranslationsTest < Test::Unit::TestCase
23
23
 
24
24
  def test_reader_text_for_a_given_locale
25
25
  article = Article.create!
26
- article_translation = ArticleTranslation.create!(:article => article, :locale => 'en', :description => 'desc', :text => 'text')
26
+ article_translation = ArticleTranslation.new(:description => 'desc', :text => 'text')
27
+ article_translation.article = article
28
+ article_translation.locale = 'en'
29
+ article_translation.save!
27
30
  assert_not_equal article.text, article_translation.text
28
31
  I18n.locale = :en
29
32
  assert_equal article.text, article_translation.text
@@ -45,7 +48,10 @@ class HasTranslationsTest < Test::Unit::TestCase
45
48
  def test_translations_association_and_translations
46
49
  article = Article.create!
47
50
  assert_equal [], article.translations
48
- article_translation = ArticleTranslation.create!(:article => article, :locale => 'ru', :description => 'описание', :text => 'текст')
51
+ article_translation = ArticleTranslation.new(:description => 'описание', :text => 'текст')
52
+ article_translation.article = article
53
+ article_translation.locale = 'ru'
54
+ article_translation.save!
49
55
  assert_equal [], article.translations
50
56
  assert_equal [article_translation], article.reload.translations
51
57
  assert_equal 'текст', article.text
@@ -57,6 +63,12 @@ class HasTranslationsTest < Test::Unit::TestCase
57
63
  assert_equal [], ArticleTranslation.all
58
64
  end
59
65
 
66
+ def test_all_translation_works_fine_with_attr_accessible
67
+ article = Article.create!
68
+ t = article.all_translations
69
+ assert_equal 'ru', t[:ru].locale
70
+ end
71
+
60
72
  def test_translation_validations
61
73
  article_translation = ArticleTranslation.create(:description => 'description', :text => 'text')
62
74
  assert article_translation.errors[:locale].present?
data/test/schema.rb CHANGED
@@ -22,15 +22,14 @@ ActiveRecord::Schema.define(:version => 0) do
22
22
  end
23
23
 
24
24
  class ArticleTranslation < ActiveRecord::Base
25
+ attr_accessible :description, :text
25
26
  end
26
-
27
27
  class Article < ActiveRecord::Base
28
28
  translations :description, :text, :writer => true
29
29
  end
30
30
 
31
31
  class TeamTranslation < ActiveRecord::Base
32
32
  end
33
-
34
33
  class Team < ActiveRecord::Base
35
34
  translations :text, :fallback => true, :nil => nil
36
- end
35
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_translations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dmitry Polushkin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-09 00:00:00 +01:00
18
+ date: 2011-02-04 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -33,6 +33,7 @@ files:
33
33
  - Rakefile
34
34
  - generators/has_translations/USAGE
35
35
  - generators/has_translations/has_translations_generator.rb
36
+ - has_translations.gemspec
36
37
  - init.rb
37
38
  - install.rb
38
39
  - lib/has_translations.rb
@@ -47,8 +48,8 @@ homepage: http://github.com/dmitry/has_translations
47
48
  licenses: []
48
49
 
49
50
  post_install_message:
50
- rdoc_options:
51
- - --charset=UTF-8
51
+ rdoc_options: []
52
+
52
53
  require_paths:
53
54
  - lib
54
55
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -72,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  requirements: []
73
74
 
74
75
  rubyforge_project:
75
- rubygems_version: 1.3.7
76
+ rubygems_version: 1.5.0
76
77
  signing_key:
77
78
  specification_version: 3
78
79
  summary: Create translations for your ActiveRecord models.