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 +8 -8
- data/lib/has_localization_table/active_record/attributes/cache.rb +4 -1
- data/lib/has_localization_table/active_record/relation.rb +3 -3
- data/lib/has_localization_table/version.rb +1 -1
- data/spec/active_record/attributes_spec.rb +14 -0
- data/spec/active_record/relation_spec.rb +87 -73
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjhlMTY0MTFjZWMyZDc0ZWU1MmJlNTQ3OTJhZDNjYjdmNDFhY2E2Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Nzk0MDY0MDlmNTBiYTYzOTdlOGM1Y2YwNTYxYWFmNzQxYWNkM2RlYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODRlOGQ2NTA1MTg0MzM4ZjI4NWU5MTcxZjBlOTM4OWNjM2EyNzJkYTE2MDI0
|
10
|
+
MjNkMDM4OWQzYjM5ZjNkOGRhYjg5OGRkODg5ZTZkZDliNDIyMDgzZGU3Yzg1
|
11
|
+
MmMyMGI5ODQzMWM4ODFlNTRmZDRhYzE3ZjQ2MDc4NTAzNzM3ZjA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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)
|
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]
|
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?
|
96
|
+
localization_association.reject! { |l| !l.persisted? && localized_attributes.all?{ |attr| l.send(attr).blank? } }
|
97
97
|
end
|
98
98
|
end
|
99
99
|
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)
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
27
|
+
subject do
|
28
|
+
Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
|
29
|
+
Article
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
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
|
-
|
85
|
-
conditions.call.must_equal "article_localizations.locale_id = 2"
|
86
|
-
end
|
87
|
+
conditions = subject.reflect_on_association(:string).options[:conditions]
|
87
88
|
|
88
|
-
|
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
|
-
|
91
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
111
|
-
|
112
|
-
Article.
|
113
|
-
Article.
|
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
|