puret 1.0.1 → 1.0.2
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/lib/puret/active_record_extensions.rb +18 -6
- data/lib/puret/version.rb +1 -1
- data/test/puret_test.rb +12 -1
- metadata +1 -1
|
@@ -21,14 +21,21 @@ module Puret
|
|
|
21
21
|
make_it_puret! unless included_modules.include?(InstanceMethods)
|
|
22
22
|
|
|
23
23
|
attributes.each do |attribute|
|
|
24
|
+
# attribute setter
|
|
24
25
|
define_method "#{attribute}=" do |value|
|
|
25
|
-
puret_attributes[attribute] = value
|
|
26
|
+
puret_attributes[I18n.locale][attribute] = value
|
|
26
27
|
end
|
|
27
28
|
|
|
29
|
+
# attribute getter
|
|
28
30
|
define_method attribute do
|
|
29
|
-
return
|
|
31
|
+
# return previously setted attributes if present
|
|
32
|
+
return puret_attributes[I18n.locale][attribute] if puret_attributes[I18n.locale][attribute]
|
|
30
33
|
return if new_record?
|
|
31
34
|
|
|
35
|
+
# Lookup chain:
|
|
36
|
+
# if translation not present in current locale,
|
|
37
|
+
# use default locale, if present.
|
|
38
|
+
# Otherwise use first translation
|
|
32
39
|
translation = translations.detect { |t| t.locale.to_sym == I18n.locale } ||
|
|
33
40
|
translations.detect { |t| t.locale.to_sym == I18n.default_locale } ||
|
|
34
41
|
translations.first
|
|
@@ -40,6 +47,7 @@ module Puret
|
|
|
40
47
|
|
|
41
48
|
private
|
|
42
49
|
|
|
50
|
+
# configure model
|
|
43
51
|
def make_it_puret!
|
|
44
52
|
include InstanceMethods
|
|
45
53
|
|
|
@@ -49,15 +57,19 @@ module Puret
|
|
|
49
57
|
end
|
|
50
58
|
|
|
51
59
|
module InstanceMethods
|
|
60
|
+
# attributes are stored in @puret_attributes instance variable via setter
|
|
52
61
|
def puret_attributes
|
|
53
|
-
@puret_attributes ||= {}
|
|
62
|
+
@puret_attributes ||= Hash.new { |hash, key| hash[key] = {} }
|
|
54
63
|
end
|
|
55
64
|
|
|
65
|
+
# called after save
|
|
56
66
|
def update_translations!
|
|
57
67
|
return if puret_attributes.blank?
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
puret_attributes.each do |locale, attributes|
|
|
69
|
+
translation = translations.find_or_initialize_by_locale(locale.to_s)
|
|
70
|
+
translation.attributes = translation.attributes.merge(attributes)
|
|
71
|
+
translation.save!
|
|
72
|
+
end
|
|
61
73
|
end
|
|
62
74
|
end
|
|
63
75
|
end
|
data/lib/puret/version.rb
CHANGED
data/test/puret_test.rb
CHANGED
|
@@ -54,10 +54,21 @@ class PuretTest < ActiveSupport::TestCase
|
|
|
54
54
|
I18n.locale = :de
|
|
55
55
|
post = Post.first
|
|
56
56
|
post.text = 'Deutscher Text'
|
|
57
|
-
post.title.blank?
|
|
57
|
+
assert !post.title.blank?
|
|
58
58
|
assert_equal 'Deutscher Text', post.text
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
test 'temporary locale switch should work like expected' do
|
|
62
|
+
post = Post.new
|
|
63
|
+
post.title = 'English title'
|
|
64
|
+
I18n.locale = :de
|
|
65
|
+
post.title = 'Deutscher Titel'
|
|
66
|
+
post.save
|
|
67
|
+
assert_equal 'Deutscher Titel', post.title
|
|
68
|
+
I18n.locale = :en
|
|
69
|
+
assert_equal 'English title', post.title
|
|
70
|
+
end
|
|
71
|
+
|
|
61
72
|
test 'translation model should validate presence of model' do
|
|
62
73
|
t = PostTranslation.new
|
|
63
74
|
t.valid?
|