has_localization_table 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ module HasLocalizationTable
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def localized_attributes
|
17
|
-
# Determine which attributes of the association model should be
|
17
|
+
# Determine which attributes of the association model should be accessible through the base class
|
18
18
|
# ie. everything that's not a primary key, foreign key, or timestamp attribute
|
19
19
|
association_name = self.localization_table_options[:association_name] || :strings
|
20
20
|
association = reflect_on_association(association_name)
|
@@ -23,23 +23,30 @@ module HasLocalizationTable
|
|
23
23
|
|
24
24
|
def create_localization_associations!
|
25
25
|
create_has_many_association
|
26
|
-
|
26
|
+
|
27
|
+
# if caller explicitly asked not to create a has_one association, there's nothing more to do
|
28
|
+
return unless localization_table_options.fetch(:has_one, true)
|
29
|
+
|
30
|
+
create_has_one_association if localization_table_options[:has_one] or HasLocalizationTable.create_has_one_by_default
|
27
31
|
end
|
28
32
|
|
29
33
|
# Collect the localization for the current locale
|
30
34
|
def create_has_one_association
|
31
35
|
table_name = localization_class.table_name
|
32
36
|
foreign_key = HasLocalizationTable.locale_foreign_key
|
37
|
+
association_name = localization_association_name.to_s.singularize.to_sym
|
38
|
+
association_name = :localization if localized_attributes.include?(association_name)
|
33
39
|
|
34
|
-
has_one_options = localization_table_options.except(:association_name, :required, :optional, :dependent).
|
40
|
+
has_one_options = localization_table_options.except(:association_name, :required, :optional, :dependent, :has_one).
|
35
41
|
merge({ conditions: Proc.new { "#{table_name}.#{foreign_key} = #{HasLocalizationTable.current_locale.id}"} })
|
36
42
|
|
37
|
-
self.has_one
|
43
|
+
self.has_one association_name, has_one_options
|
44
|
+
self.has_one(:localization, has_one_options) unless association_name == :localization
|
38
45
|
end
|
39
46
|
|
40
47
|
# Collect all localizations for the object
|
41
48
|
def create_has_many_association
|
42
|
-
self.has_many localization_association_name, localization_table_options.except(:association_name, :required, :optional)
|
49
|
+
self.has_many localization_association_name, localization_table_options.except(:association_name, :required, :optional, :has_one)
|
43
50
|
end
|
44
51
|
|
45
52
|
public
|
@@ -27,6 +27,7 @@ module HasLocalizationTable
|
|
27
27
|
config_accessor :all_locales
|
28
28
|
config_accessor :class_suffix
|
29
29
|
config_accessor :default_association_name
|
30
|
+
config_accessor :create_has_one_by_default # Should a has_one association be automatically created?
|
30
31
|
end
|
31
32
|
|
32
33
|
# this is ugly. why can't we pass the default value to config_accessor...?
|
@@ -38,5 +39,6 @@ module HasLocalizationTable
|
|
38
39
|
config.primary_locale = ->{ config.locale_class.constantize.first }
|
39
40
|
config.current_locale = ->{ config.locale_class.constantize.first }
|
40
41
|
config.all_locales = ->{ config.locale_class.constantize.all }
|
42
|
+
config.create_has_one_by_default = true
|
41
43
|
end
|
42
44
|
end
|
@@ -15,6 +15,7 @@ describe HasLocalizationTable do
|
|
15
15
|
def self.localization_association_name; :strings; end
|
16
16
|
def self.localization_table_options; {}; end
|
17
17
|
def self.localization_class; ArticleLocalization; end
|
18
|
+
def self.localized_attributes; []; end
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -39,6 +40,41 @@ describe HasLocalizationTable do
|
|
39
40
|
reflection.macro.must_equal :has_one
|
40
41
|
end
|
41
42
|
|
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
|
48
|
+
|
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)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
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)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
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
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
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)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
42
78
|
it "should use the current locale for the has_one association" do
|
43
79
|
locale = MiniTest::Mock.new
|
44
80
|
locale.expect :id, 2
|
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.3.
|
4
|
+
version: 0.3.4
|
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: 2013-
|
12
|
+
date: 2013-06-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|