simple_model_translations 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_model_translations (0.2.4)
4
+ simple_model_translations (0.2.5)
5
5
  activerecord (~> 3.0.0)
6
6
 
7
7
  GEM
@@ -6,11 +6,7 @@ module SimpleModelTranslations
6
6
  end
7
7
 
8
8
  def find_translation_by_locale(locale)
9
- if translations.loaded? || @record.new_record?
10
- translations.detect { |t| t.locale.to_sym == locale }
11
- else
12
- translations.find_by_locale(locale)
13
- end
9
+ translations.detect { |t| t.locale.to_sym == locale }
14
10
  end
15
11
 
16
12
  def find_or_build_translation_by_locale(locale)
@@ -30,13 +26,28 @@ module SimpleModelTranslations
30
26
  end
31
27
 
32
28
  def current_translation
33
- cached_translations[current_locale_for_translation] ||= find_translation_by_locale(current_locale_for_translation) || find_translation_by_locale(default_locale_for_translation)
29
+ @current_translation || find_translation_by_locale(current_locale_for_translation) || find_translation_by_locale(default_locale_for_translation)
34
30
  end
35
31
 
36
32
  def find_or_build_current_translation
37
33
  find_or_build_translation_by_locale(current_locale_for_translation)
38
34
  end
39
35
 
36
+ def force_translation_with_locale(locale)
37
+ translation =
38
+ if translations.loaded?
39
+ find_translation_by_locale(locale)
40
+ else
41
+ translations.find_by_locale(locale)
42
+ end
43
+ if translation
44
+ @record.readonly!
45
+ @current_translation = translation
46
+ else
47
+ raise "Cannot use translation with locale '#{locale}' for object '#{@record}'"
48
+ end
49
+ end
50
+
40
51
  private
41
52
  def foreign_object_key
42
53
  @record.class.name.underscore.to_sym
@@ -1,3 +1,3 @@
1
1
  module SimpleModelTranslations
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  end
@@ -61,6 +61,13 @@ describe Article do
61
61
  @article.should have_translated_attribute(:ru, :name, 'Hello')
62
62
  @article.should have_translated_attribute(:en, :name, 'Hello in English')
63
63
  end
64
+
65
+ it 'should not create additional translations when updating record, loaded from database' do
66
+ article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World', :locale => :ru)
67
+ article.reload
68
+ article.update_attributes!(:name => 'Hello', :content => 'World', :locale => :en)
69
+ article.translations.length.should == 2
70
+ end
64
71
  end
65
72
 
66
73
  describe '#attributes=' do
@@ -42,6 +42,29 @@ describe Article do
42
42
  article.translation_helper.current_translation.should be_nil
43
43
  end
44
44
  end
45
+
46
+ describe '#force_translation_with_locale(locale)' do
47
+ it 'should use data from selected translation' do
48
+ article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World', :locale => :en)
49
+ article.update_attributes!(:name => 'Привет', :locale => :ru)
50
+ article.translation_helper.force_translation_with_locale(:en)
51
+ article.content.should == 'World'
52
+ article.name.should == 'Hello'
53
+ end
54
+
55
+ it 'should raise exception when there is no translation' do
56
+ article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
57
+ expect { article.translation_helper.force_translation_with_locale(:en) }.to raise_error
58
+ end
59
+
60
+ it 'should make record read-only' do
61
+ article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World', :locale => :en)
62
+ article.update_attributes!(:name => 'Привет', :locale => :ru)
63
+ article.translation_helper.force_translation_with_locale(:en)
64
+ article.should be_readonly
65
+ end
66
+
67
+ end
45
68
  end
46
69
 
47
70
  describe ArticleTranslation do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model_translations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pavel Forkert
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-17 00:00:00 +02:00
18
+ date: 2011-03-19 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency