has_localization_table 0.4.2 → 0.4.3

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODMwMzhkMGY3Mjg1MzZiZjBkMDJjNDk1ZjFkNjk2Y2Y2N2MxODg1Nw==
4
+ YjhlMTY0MTFjZWMyZDc0ZWU1MmJlNTQ3OTJhZDNjYjdmNDFhY2E2Mw==
5
5
  data.tar.gz: !binary |-
6
- N2VmZWRhMmQyMjIxNDVhMTVlZTc4ZDk0YWQzNmMzZjcyYzVlNjA5Mw==
6
+ Nzk0MDY0MDlmNTBiYTYzOTdlOGM1Y2YwNTYxYWFmNzQxYWNkM2RlYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzFhMjg3NmE1OTcwMzZhNmI2ZDgzNDRhNzk0Njg0MmZlNzVjMTQ0Nzg0YTNj
10
- NTdjZTI2OGNkYjU1MzI0NmE4YjI4Y2JiNjhmYjk3MjkxM2EwNTMxZmM4MDli
11
- MmIxNjk5ODE0MzI4ZGY5MzJlOTkyZTk5NmIzMDlmYTg4NzQzZDg=
9
+ ODRlOGQ2NTA1MTg0MzM4ZjI4NWU5MTcxZjBlOTM4OWNjM2EyNzJkYTE2MDI0
10
+ MjNkMDM4OWQzYjM5ZjNkOGRhYjg5OGRkODg5ZTZkZDliNDIyMDgzZGU3Yzg1
11
+ MmMyMGI5ODQzMWM4ODFlNTRmZDRhYzE3ZjQ2MDc4NTAzNzM3ZjA=
12
12
  data.tar.gz: !binary |-
13
- ODgyYzU3Y2U3MTExMjg5MjQ5N2NiNDMwMzdhNjMwOGRjMzhjMGRmY2E2N2E0
14
- OWE5MGIzYzczZjIzODkzNTA4ZTFmMGY4YWVmYWUzNDBjZDk5YWFiYjllNjhh
15
- OTRlMzA1YzA1YjliZDQwOTIyM2U5MDEwZjUyZmJiY2Y0M2RmYzM=
13
+ MTgxODEzNjQ3MzYwNmU3OTE3ZmM3ZGJiMTJmMjM0NTQxYzc2MWZkNzk0MjFj
14
+ YjI3ODcxNDA5Y2VjYTA1OWQyN2ZlNDgwOWZkNzY2ZTFjNTk3YzIyODc5NzYw
15
+ M2NmNzdmMTFkYzZiMzQwMWE5YTdiYmMzY2FiMGQ0ZDZlMzllNjE=
@@ -27,7 +27,10 @@ module HasLocalizationTable
27
27
  end
28
28
 
29
29
  def changed?(attr, locale)
30
- klass.localization_for(locale).send(attr) != self[attr.to_sym][locale.id]
30
+ localization = klass.localization_for(locale)
31
+ return false unless localization
32
+
33
+ localization.send(attr) != self[attr.to_sym][locale.id]
31
34
  end
32
35
  end
33
36
  end
@@ -29,7 +29,7 @@ module HasLocalizationTable
29
29
  # if caller explicitly asked not to create a has_one association, there's nothing more to do
30
30
  return unless localization_table_options.fetch(:has_one, true)
31
31
 
32
- create_has_one_association if localization_table_options[:has_one] or HasLocalizationTable.create_has_one_by_default
32
+ create_has_one_association if localization_table_options[:has_one] || HasLocalizationTable.create_has_one_by_default
33
33
  end
34
34
 
35
35
  # Collect the localization for the current locale
@@ -40,7 +40,7 @@ module HasLocalizationTable
40
40
  association_name = :localization if localized_attributes.include?(association_name)
41
41
 
42
42
  has_one_options = localization_table_options.except(*RESERVED_KEYS).
43
- merge(conditions: -> { "#{table_name}.#{foreign_key} = #{HasLocalizationTable.current_locale.id}" })
43
+ merge(conditions: -> * { "#{table_name}.#{foreign_key} = #{HasLocalizationTable.current_locale.id}" })
44
44
 
45
45
  self.has_one association_name, has_one_options
46
46
  self.has_one(:localization, has_one_options) unless association_name == :localization
@@ -93,7 +93,7 @@ module HasLocalizationTable
93
93
 
94
94
  # Remove localization objects that are not filled in
95
95
  def reject_empty_localizations!
96
- localization_association.reject! { |l| !l.persisted? and localized_attributes.all?{ |attr| l.send(attr).blank? } }
96
+ localization_association.reject! { |l| !l.persisted? && localized_attributes.all?{ |attr| l.send(attr).blank? } }
97
97
  end
98
98
  end
99
99
  end
@@ -1,3 +1,3 @@
1
1
  module HasLocalizationTable
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -266,6 +266,20 @@ describe HasLocalizationTable do
266
266
  a.localizations(true).wont_be_empty
267
267
  end
268
268
 
269
+ it 'should return nil when getting a attribute that has not been initialized' do
270
+ Article.has_localization_table
271
+
272
+ a = nil
273
+
274
+ HasLocalizationTable.with_options(all_locales: [Locale.first]) do
275
+ a = Article.new(name: 'Name')
276
+
277
+ HasLocalizationTable.with_options(current_locale: Locale.last) do
278
+ a.name(Locale.first).must_equal 'Name'
279
+ end
280
+ end
281
+ end
282
+
269
283
  it 'should update the main model when the string is directly updated' do
270
284
  Article.has_localization_table
271
285
  a = Article.new
@@ -11,106 +11,120 @@ describe HasLocalizationTable do
11
11
  end
12
12
 
13
13
  Object.send(:remove_const, :Article) rescue nil
14
- Article = Class.new(ActiveRecord::Base) do
15
- def self.localization_association_name; :strings; end
16
- def self.localization_table_options; {}; end
17
- def self.localization_class; ArticleLocalization; end
18
- def self.localized_attributes; []; end
19
- end
14
+ Article = Class.new(ActiveRecord::Base)
20
15
  end
21
16
 
22
- subject do
23
- Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
24
- Article
25
- end
17
+ describe 'reflections' do
18
+ before do
19
+ Article.class_eval do
20
+ def self.localization_association_name; :strings; end
21
+ def self.localization_table_options; {}; end
22
+ def self.localization_class; ArticleLocalization; end
23
+ def self.localized_attributes; []; end
24
+ end
25
+ end
26
26
 
27
- it "should alias with_localizations with the actual association name" do
28
- assert subject.respond_to? :with_strings
29
- end
27
+ subject do
28
+ Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
29
+ Article
30
+ end
30
31
 
31
- it "should create a has_many association" do
32
- reflection = subject.reflect_on_association(:strings)
33
- refute_nil reflection
34
- reflection.macro.must_equal :has_many
35
- end
32
+ it "should alias with_localizations with the actual association name" do
33
+ assert subject.respond_to? :with_strings
34
+ end
36
35
 
37
- it "should create a has_one association" do
38
- reflection = subject.reflect_on_association(:string)
39
- refute_nil reflection
40
- reflection.macro.must_equal :has_one
41
- end
36
+ it "should create a has_many association" do
37
+ reflection = subject.reflect_on_association(:strings)
38
+ refute_nil reflection
39
+ reflection.macro.must_equal :has_many
40
+ end
42
41
 
43
- it "should alias the has_one association as localization" do
44
- reflection = subject.reflect_on_association(:localization)
45
- refute_nil reflection
46
- reflection.macro.must_equal :has_one
47
- end
42
+ it "should create a has_one association" do
43
+ reflection = subject.reflect_on_association(:string)
44
+ refute_nil reflection
45
+ reflection.macro.must_equal :has_one
46
+ end
48
47
 
49
- it "should not create a has_one association if disabled in configuration" do
50
- HasLocalizationTable.stub :create_has_one_by_default, false do
51
- assert_nil subject.reflect_on_association(:localization)
48
+ it "should alias the has_one association as localization" do
49
+ reflection = subject.reflect_on_association(:localization)
50
+ refute_nil reflection
51
+ reflection.macro.must_equal :has_one
52
52
  end
53
- end
54
53
 
55
- it "should not create a has_one association if disabled in table options" do
56
- Article.stub :localization_table_options, { has_one: false } do
57
- assert_nil subject.reflect_on_association(:localization)
54
+ it "should not create a has_one association if disabled in configuration" do
55
+ HasLocalizationTable.stub :create_has_one_by_default, false do
56
+ assert_nil subject.reflect_on_association(:localization)
57
+ end
58
58
  end
59
- end
60
59
 
61
- it "should create a has_one association if asked for, even if disabled in configuration" do
62
- HasLocalizationTable.stub :create_has_one_by_default, false do
63
- Article.stub :localization_table_options, { has_one: true } do
64
- reflection = subject.reflect_on_association(:localization)
65
- refute_nil reflection
66
- reflection.macro.must_equal :has_one
60
+ it "should not create a has_one association if disabled in table options" do
61
+ Article.stub :localization_table_options, { has_one: false } do
62
+ assert_nil subject.reflect_on_association(:localization)
67
63
  end
68
64
  end
69
- end
70
65
 
71
- it "should not create an association that conflicts with an attribute name" do
72
- Article.stub :localized_attributes, [:string] do
73
- Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
74
- assert_nil Article.reflect_on_association(:string)
66
+ it "should create a has_one association if asked for, even if disabled in configuration" do
67
+ HasLocalizationTable.stub :create_has_one_by_default, false do
68
+ Article.stub :localization_table_options, { has_one: true } do
69
+ reflection = subject.reflect_on_association(:localization)
70
+ refute_nil reflection
71
+ reflection.macro.must_equal :has_one
72
+ end
73
+ end
75
74
  end
76
- end
77
75
 
78
- it "should use the current locale for the has_one association" do
79
- locale = MiniTest::Mock.new
80
- locale.expect :id, 2
76
+ it "should not create an association that conflicts with an attribute name" do
77
+ Article.stub :localized_attributes, [:string] do
78
+ Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
79
+ assert_nil Article.reflect_on_association(:string)
80
+ end
81
+ end
81
82
 
82
- conditions = subject.reflect_on_association(:string).options[:conditions]
83
+ it "should use the current locale for the has_one association" do
84
+ locale = MiniTest::Mock.new
85
+ locale.expect :id, 2
83
86
 
84
- HasLocalizationTable.stub :current_locale, locale do
85
- conditions.call.must_equal "article_localizations.locale_id = 2"
86
- end
87
+ conditions = subject.reflect_on_association(:string).options[:conditions]
87
88
 
88
- locale.expect :id, 3
89
+ HasLocalizationTable.stub :current_locale, locale do
90
+ conditions.call.must_equal "article_localizations.locale_id = 2"
91
+ end
92
+
93
+ locale.expect :id, 3
89
94
 
90
- HasLocalizationTable.stub :current_locale, locale do
91
- conditions.call.must_equal "article_localizations.locale_id = 3"
95
+ HasLocalizationTable.stub :current_locale, locale do
96
+ conditions.call.must_equal "article_localizations.locale_id = 3"
97
+ end
92
98
  end
93
- end
94
99
 
95
- it 'should add a default scope if include: true is given' do
96
- Article.stub :localization_table_options, { include: true } do
97
- Article.default_scopes.must_be_empty
98
- Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
99
- Article.default_scopes.size.must_equal(1)
100
+ it 'should add a default scope if include: true is given' do
101
+ Article.stub :localization_table_options, { include: true } do
102
+ Article.default_scopes.must_be_empty
103
+ Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
104
+ Article.default_scopes.size.must_equal(1)
105
+ end
100
106
  end
101
- end
102
107
 
103
- it 'should not add a default scope if include: false is given' do
104
- Article.stub :localization_table_options, { include: false } do
105
- Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
106
- Article.default_scopes.must_be_empty
108
+ it 'should not add a default scope if include: false is given' do
109
+ Article.stub :localization_table_options, { include: false } do
110
+ Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
111
+ Article.default_scopes.must_be_empty
112
+ end
113
+ end
114
+
115
+ it 'should not add a default scope if include: is not given' do
116
+ Article.stub :localization_table_options, { } do
117
+ Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
118
+ Article.default_scopes.must_be_empty
119
+ end
107
120
  end
108
121
  end
109
122
 
110
- it 'should not add a default scope if include: is not given' do
111
- Article.stub :localization_table_options, { } do
112
- Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
113
- Article.default_scopes.must_be_empty
123
+ describe 'real' do
124
+ it 'should create a has_one association if requested' do
125
+ Article.has_localization_table has_one: true
126
+ a = Article.create!(name: 'Name')
127
+ a.localization.must_equal a.current_localization
114
128
  end
115
129
  end
116
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_localization_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Vandersluis