armot 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,12 +52,15 @@ module Armot
52
52
  res ? res : raise(ActiveRecord::RecordNotFound)
53
53
  end
54
54
  end
55
+
56
+ # To implement by armotized classes
57
+ define_method :"reload_armot!" do
58
+ end
55
59
  end
56
60
 
57
61
  instance_mixin.module_eval do
58
62
  define_method :"#{attribute}=" do |value|
59
63
  armot_attributes[I18n.locale]["#{attribute}"] = value
60
- I18n.backend.reload!
61
64
  end
62
65
 
63
66
  define_method :"#{attribute}_changed?" do
@@ -66,17 +69,19 @@ module Armot
66
69
 
67
70
  define_method :"#{attribute}" do
68
71
  return armot_attributes[I18n.locale]["#{attribute}"] if armot_attributes[I18n.locale]["#{attribute}"]
69
- return if new_record?
70
72
 
71
- trans = I18n.t "#{attribute}_#{id}", :scope => "armot.#{self.class.to_s.underscore.pluralize}.#{attribute}", :default => Armot.token
72
- return trans if trans != Armot.token
73
+ if armot_attributes.any?
74
+ I18n.fallbacks[I18n.locale].each do |fallback|
75
+ return armot_attributes[fallback]["#{attribute}"] if armot_attributes[fallback]["#{attribute}"]
76
+ end
77
+ end
73
78
 
74
- (I18n.available_locales - [I18n.locale]).each do |lang|
75
- trans = I18n.t "#{attribute}_#{id}", :scope => "armot.#{self.class.to_s.underscore.pluralize}.#{attribute}", :default => Armot.token, :locale => lang
76
- break if trans != Armot.token
79
+ if persisted?
80
+ trans = I18n.t "#{attribute}_#{id}", :scope => "armot.#{self.class.to_s.underscore.pluralize}.#{attribute}", :default => Armot.token
81
+ return trans if trans != Armot.token
77
82
  end
78
83
 
79
- trans == Armot.token ? self[:"#{attribute}"] : trans
84
+ ( new_record? || trans == Armot.token) ? self[:"#{attribute}"] : trans
80
85
  end
81
86
  end
82
87
  end
@@ -105,9 +110,8 @@ module Armot
105
110
  @armot_attributes ||= Hash.new { |hash, key| hash[key] = {} }
106
111
  end
107
112
 
108
- # called after save
109
113
  def update_translations!
110
- return if armot_attributes.blank?
114
+ return if armot_attributes.empty?
111
115
 
112
116
  armot_attributes.each do |locale, attributes|
113
117
  attributes.each do |k, v|
@@ -116,6 +120,7 @@ module Armot
116
120
  end
117
121
  end
118
122
 
123
+ self.class.reload_armot!
119
124
  armot_attributes.clear
120
125
  end
121
126
 
@@ -1,3 +1,3 @@
1
1
  module Armot
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -13,6 +13,7 @@ class ArmotTest < ActiveSupport::TestCase
13
13
  def setup
14
14
  setup_db
15
15
  I18n.locale = I18n.default_locale = :en
16
+ I18n.fallbacks.clear
16
17
  Post.create(:title => 'English title')
17
18
  end
18
19
 
@@ -109,16 +110,6 @@ class ArmotTest < ActiveSupport::TestCase
109
110
  assert_equal "original title", Post.first.title
110
111
  end
111
112
 
112
- test "should look for translations in other languages before fail" do
113
- post = Post.first
114
- I18n::Backend::ActiveRecord::Translation.delete_all
115
- I18n.locale = :ca
116
- post.title = "Catalan title"
117
- post.save!
118
- I18n.locale = :it
119
- assert_equal "Catalan title", post.title
120
- end
121
-
122
113
  test "should find by translated title in database as a translation" do
123
114
  post = Post.first
124
115
  I18n.locale = :ca
@@ -306,4 +297,33 @@ class ArmotTest < ActiveSupport::TestCase
306
297
  res = Product.find_by_name("Catalan foo customized")
307
298
  assert_equal "Catalan foo customized_override", res
308
299
  end
300
+
301
+ test "should respect I18n standard fallback system" do
302
+ I18n.fallbacks.map :es => :ca
303
+ post = Post.first
304
+ I18n.locale = :ca
305
+ post.title = "Bola de drac"
306
+ I18n.locale = :en
307
+ post.title = "Dragon ball"
308
+ I18n.locale = :es
309
+ post.save!
310
+ assert_equal "Bola de drac", post.title
311
+ end
312
+
313
+ test "should return the fallback even if not saved" do
314
+ I18n.fallbacks.map :es => :ca
315
+ post = Post.first
316
+ I18n.locale = :ca
317
+ post.title = "Bola de drac"
318
+ I18n.locale = :en
319
+ post.title = "Dragon ball"
320
+ I18n.locale = :es
321
+ assert_equal "Bola de drac", post.title
322
+ end
323
+
324
+ test "should return db-column values even if not persisted" do
325
+ post = Post.new
326
+ post[:title] = "Hello world"
327
+ assert_equal "Hello world", post.title
328
+ end
309
329
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: armot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-08 00:00:00.000000000 Z
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n-active_record