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.
@@ -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 puret_attributes[attribute] if puret_attributes[attribute]
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
- translation = translations.find_or_initialize_by_locale(I18n.locale.to_s)
59
- translation.attributes = translation.attributes.merge(puret_attributes)
60
- translation.save!
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
@@ -1,3 +1,3 @@
1
1
  module Puret
2
- VERSION = "1.0.1".freeze
2
+ VERSION = "1.0.2".freeze
3
3
  end
@@ -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?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puret
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Johannes J\xC3\xB6rg Schmidt"