efficient_translations 0.0.5 → 0.0.6

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- efficient_translations (0.0.5)
4
+ efficient_translations (0.0.6)
5
5
  activerecord (~> 2.3)
6
6
  activesupport (~> 2.3)
7
7
 
data/README.md CHANGED
@@ -82,6 +82,9 @@ Done! You have the EfficientTranslations power in your hands :-)
82
82
  I18n.locale = :it
83
83
  # when the current locale is not found, the default locale will be used
84
84
  product.name # => 'Efficient Translations'
85
+ # if you don't want the default locale fallback
86
+ product.name! # => nil
87
+ product.name_translation! :it # => nil
85
88
 
86
89
  # .name= is a wrapper to #set_name_translation I18n.locale
87
90
  product.name = 'Traduzioni Efficienti'
@@ -47,7 +47,7 @@ module EfficientTranslations
47
47
  def define_translation_accessors field
48
48
  field = field.to_sym
49
49
  class_eval do
50
- define_method "#{field}_translation" do |locale|
50
+ define_method "#{field}_translation!" do |locale|
51
51
  locale = locale.to_sym
52
52
  # search in cache
53
53
  if efficient_translations_attributes[locale][field]
@@ -55,12 +55,16 @@ module EfficientTranslations
55
55
  else
56
56
  # search in relationship
57
57
  translation = translations.detect { |t| t.locale.to_sym == locale }
58
- if translation
59
- translation[field]
60
- elsif locale != I18n.default_locale
61
- # try to fetch default locale
62
- self.send "#{field}_translation", I18n.default_locale
63
- end
58
+ translation && translation[field] || nil
59
+ end
60
+ end
61
+
62
+ define_method "#{field}_translation" do |locale|
63
+ translation_field = self.send "#{field}_translation!", locale
64
+ if translation_field
65
+ translation_field
66
+ elsif locale.to_sym != I18n.default_locale
67
+ self.send "#{field}_translation!", I18n.default_locale
64
68
  end
65
69
  end
66
70
 
@@ -73,6 +77,10 @@ module EfficientTranslations
73
77
  self.send "#{field}_translation", I18n.locale
74
78
  end
75
79
 
80
+ define_method "#{field}!" do
81
+ self.send "#{field}_translation!", I18n.locale
82
+ end
83
+
76
84
  define_method "#{field}=" do |value|
77
85
  self.send "set_#{field}_translation", I18n.locale, value
78
86
  end
@@ -1,3 +1,3 @@
1
1
  module EfficientTranslations
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -75,20 +75,14 @@ describe EfficientTranslations do
75
75
  WorkingModel.first.name.should == 'pippo'
76
76
  end
77
77
 
78
- describe 'field_translation' do
79
- before do
80
- @model = WorkingModel.new
81
- @model.set_name_translation :en, 'pippo'
82
- @model.save!
83
- end
84
-
78
+ shared_examples 'translation finder' do
85
79
  context 'when cache contains the translated value' do
86
80
  before do
87
81
  @model.set_name_translation :en, 'foo'
88
82
  end
89
83
 
90
84
  it 'should fetch the value from cache' do
91
- @model.name_translation(:en).should == 'foo'
85
+ invoke(:en).should == 'foo'
92
86
  end
93
87
  end
94
88
 
@@ -96,19 +90,53 @@ describe EfficientTranslations do
96
90
  it 'should search in the relationship' do
97
91
  @model = WorkingModel.find @model.id
98
92
  @model.translation_model.create! :working_model_id => @model.id, :locale => 'fr', :name => 'frfr'
99
- @model.name_translation(:fr).should == 'frfr'
93
+ invoke(:fr).should == 'frfr'
94
+ end
95
+ end
96
+ end
97
+
98
+ describe 'field_translation!' do
99
+ before do
100
+ @model = WorkingModel.new
101
+ @model.set_name_translation :en, 'pippo'
102
+ @model.save!
103
+ end
104
+
105
+ def invoke *args
106
+ @model.name_translation! *args
107
+ end
108
+
109
+ it_should_behave_like 'translation finder'
110
+
111
+ context 'when cache is empty and no value is found' do
112
+ it 'should return nil' do
113
+ invoke(:de).should be_nil
100
114
  end
101
115
  end
116
+ end
117
+
118
+ describe 'field_translation' do
119
+ before do
120
+ @model = WorkingModel.new
121
+ @model.set_name_translation :en, 'pippo'
122
+ @model.save!
123
+ end
124
+
125
+ def invoke *args
126
+ @model.name_translation *args
127
+ end
128
+
129
+ it_should_behave_like 'translation finder'
102
130
 
103
131
  context 'when cache is empty and no value is found' do
104
132
  it 'should search for I18n.default_locale if locale != I18n.default_locale' do
105
133
  I18n.default_locale = :en
106
- @model.name_translation(:de).should == @model.name_translation(:en)
134
+ invoke(:de).should == @model.name_translation(:en)
107
135
  end
108
136
 
109
137
  it 'should return nil if locale == I18n.default_locale' do
110
138
  I18n.default_locale = :de
111
- @model.name_translation(:de).should be_nil
139
+ invoke(:de).should be_nil
112
140
  end
113
141
  end
114
142
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: efficient_translations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nicola Racco