localizable_db 1.0.9 → 2.0.0
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 +4 -4
- data/README.md +71 -38
- data/lib/generators/localizable_db/templates/initializer.rb +6 -5
- data/lib/localizable_db/languages.rb +19 -0
- data/lib/localizable_db/localizable.rb +122 -189
- data/lib/localizable_db/localizable_config.rb +32 -0
- data/lib/localizable_db/version.rb +1 -1
- data/lib/localizable_db.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a99073b188d1f8dca333d7d3f44e6ac410bfa910b2cfb599a295a560ae1253f
|
4
|
+
data.tar.gz: bd9fef76db1684201bf7a886be643fd7978d584302b524fdaded7f2a76a38a8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 342b8a4fed0728288bc2efbd50f9bf1d9bf14916f5ed9221f31f5c41ebf110d32c204399f85add85760792f85cea3caeacd4ab1b1cd23da1d35fd63456439a3a
|
7
|
+
data.tar.gz: 7c0d50374228b52e77278fc867ee68fa56da487428ae637966197b8113d04fc0ee7496cfd520c53bd3b67a576f5536a090530e0eaab693cb142b2646de4ca118
|
data/README.md
CHANGED
@@ -13,6 +13,10 @@ Product.where(id: 1)
|
|
13
13
|
|
14
14
|
I18n.locale = :es
|
15
15
|
|
16
|
+
Product.find(1) #=> <Product id: 1, name: "suerte">
|
17
|
+
Product.where(id: 1)
|
18
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "suerte">]>
|
19
|
+
|
16
20
|
Product.l.find(1) #=> <Product id: 1, name: "suerte">
|
17
21
|
Product.l.where(id: 1)
|
18
22
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "suerte">]>
|
@@ -26,17 +30,14 @@ Product.where(id: 1)
|
|
26
30
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck">]>
|
27
31
|
|
28
32
|
Product.l(:es).find(1) #=> <Product id: 1, name: "suerte">
|
29
|
-
Product.find(1).l(:es) #=> <Product id: 1, name: "suerte">
|
30
33
|
Product.l(:es).where(id: 1)
|
31
34
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "suerte">]>
|
32
35
|
|
33
36
|
Product.l(:pt).find(1) #=> <Product id: 1, name: "sortudo">
|
34
|
-
Product.find(1).l(:pt) #=> <Product id: 1, name: "sortudo">
|
35
37
|
Product.l(:pt).where(id: 1)
|
36
38
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "sortudo">]>
|
37
39
|
|
38
40
|
Product.l(:fr).find(1) #=> <Product id: 1, name: "heureux">
|
39
|
-
Product.find(1).l(:fr) #=> <Product id: 1, name: "heureux">
|
40
41
|
Product.l(:fr).where(id: 1)
|
41
42
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "heureux">]>
|
42
43
|
````
|
@@ -45,39 +46,43 @@ Localize multiple languages
|
|
45
46
|
products = Product.where(id: 1)
|
46
47
|
products.inspect
|
47
48
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck">]>
|
48
|
-
products = Product.
|
49
|
+
products = Product.l(:es,:pt,:fr).where(id: 1)
|
49
50
|
products.inspect
|
50
51
|
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck", es_name: "suerte", pt_name: "sortudo", fr_name: "heureux">]>
|
51
52
|
|
52
53
|
products.first.name #=> luck
|
54
|
+
products.first.get_name #=> luck
|
55
|
+
products.first.get_name(:es) #=> suerte
|
56
|
+
products.first.get_name(:pt) #=> sortudo
|
57
|
+
products.first.get_name(:fr) #=> heureux
|
53
58
|
products.first.attributes["es_name"] #=> suerte
|
54
59
|
products.first.attributes["pt_name"] #=> sortudo
|
55
60
|
products.first.attributes["fr_name"] #=> heureux
|
56
61
|
````
|
57
62
|
Creating
|
58
63
|
````ruby
|
59
|
-
Product.create(name: "something",
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
})
|
64
|
+
Product.create(name: "something", product_languages_attributes: [{
|
65
|
+
{name: "algo", locale: "es"},
|
66
|
+
{name: "alguma cosia", locale: "pt"},
|
67
|
+
{name: "quelque chose", locale: "fr"}
|
68
|
+
}])
|
64
69
|
#=> #<Product id:2, name: "something">
|
65
70
|
````
|
66
71
|
Saving
|
67
72
|
````ruby
|
68
|
-
Product.new(name: "love",
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
}).save
|
73
|
+
Product.new(name: "love", product_languages_attributes: [{
|
74
|
+
{name: "algo", locale: "es"},
|
75
|
+
{name: "alguma cosia", locale: "pt"},
|
76
|
+
{name: "quelque chose", locale: "fr"}
|
77
|
+
}]).save
|
73
78
|
#=> #<Product id:3, name: "love">
|
74
79
|
|
75
80
|
love = Product.last
|
76
|
-
love.
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
+
love.product_languages.build([
|
82
|
+
{name: "algo", locale: "es"},
|
83
|
+
{name: "alguma cosia", locale: "pt"},
|
84
|
+
{name: "quelque chose", locale: "fr"}
|
85
|
+
])
|
81
86
|
love.save
|
82
87
|
#=> #<Product id: 3, name: "love">
|
83
88
|
love = Product.l(:fr).find(3)
|
@@ -87,11 +92,11 @@ love.inspect
|
|
87
92
|
Updating
|
88
93
|
````ruby
|
89
94
|
product = Product.find(3)
|
90
|
-
product.update(name: "the love",
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
})
|
95
|
+
product.update(name: "the love", product_languages_attributes: [{
|
96
|
+
{name: "algo", locale: "es"},
|
97
|
+
{name: "alguma cosia", locale: "pt"},
|
98
|
+
{name: "quelque chose", locale: "fr"}
|
99
|
+
}])
|
95
100
|
#=> #<Product id:3, name: "the love">
|
96
101
|
|
97
102
|
product = Product.l(:fr).find(3)
|
@@ -113,9 +118,7 @@ products = Product.includes(:features).where(id: 1)
|
|
113
118
|
products.first.features.inspect
|
114
119
|
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Makes people happy">]>
|
115
120
|
|
116
|
-
products = Product.l(:es)
|
117
|
-
products.includes(:features)
|
118
|
-
end.where(id: 1)
|
121
|
+
products = Product.l(:es).includes.where(id: 1)
|
119
122
|
products.first.features.inspect
|
120
123
|
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Hace a la gente feliz">]>
|
121
124
|
````
|
@@ -125,15 +128,18 @@ products = Product.includes(:features).where(id: 1)
|
|
125
128
|
products.first.features.inspect
|
126
129
|
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Makes people happy">]>
|
127
130
|
|
128
|
-
products = Product.
|
129
|
-
products.includes(:features)
|
130
|
-
end.where(id: 1)
|
131
|
+
products = Product.l(:es,:pt,:fr).includes(:features).where(id: 1)
|
131
132
|
products.first.features.inspect
|
132
133
|
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Makes people happy", es_desc: "Hace a la gente feliz", pt_desc: "Faz as pessoas felizes", fr_desc: "Rend les gens heureux">]>
|
133
134
|
````
|
134
|
-
|
135
|
-
|
135
|
+
Aggregation and grouping stuff operations work as usual
|
136
|
+
````ruby
|
137
|
+
Product.count
|
138
|
+
# => 3
|
136
139
|
|
140
|
+
Product.l(:es,:pt,:fr).count
|
141
|
+
# => 3
|
142
|
+
````
|
137
143
|
## Installation
|
138
144
|
Add this line to your application's Gemfile:
|
139
145
|
```ruby
|
@@ -150,14 +156,41 @@ Then Install it!
|
|
150
156
|
$ rails g localizable_db:install
|
151
157
|
````
|
152
158
|
|
153
|
-
Configure your supported languages
|
159
|
+
Configure your supported languages, default language and other the behavior of the gem.
|
154
160
|
````ruby
|
155
161
|
# config/initializers/localizable_db_initializer_.rb
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
162
|
+
LocalizableDb.config do |config|
|
163
|
+
# REQUIRED
|
164
|
+
|
165
|
+
config.supported_languages = [:en, :es, :pt, :fr]
|
166
|
+
config.default_language = :en
|
167
|
+
|
168
|
+
# OPTIONAL
|
169
|
+
|
170
|
+
# config.enable_i18n_integration = true
|
171
|
+
# enable_i18n_integration Allow you to use the eager load methods (includes, preload and eager_load)
|
172
|
+
# as in the examples. If you set this option to false then eager load methods will not work and
|
173
|
+
# you will have to call the ::l method to localize the language you want.
|
174
|
+
|
175
|
+
# config.enable_getters = true
|
176
|
+
# enable_getters Allow you to use the getters methods to access the localized attributes.
|
177
|
+
|
178
|
+
# config.attributes_integration = true
|
179
|
+
# attributes_integration overrides the attributes of the model with the localized attributes.
|
180
|
+
# Example:
|
181
|
+
# Imagine you have a Product like this one
|
182
|
+
# #<Product id: 1, name: "love">
|
183
|
+
# with this configuration
|
184
|
+
# Product < ApplicationRecord
|
185
|
+
# localize :name
|
186
|
+
# end
|
187
|
+
# When you retrieve the product of name love in spanish from your database, and this option is set to true
|
188
|
+
# it will return
|
189
|
+
# #<Product id: 1, name: "amor">
|
190
|
+
# If this option is set to false, the retrieved object will be
|
191
|
+
# #<Product id: 1, name: "love">
|
192
|
+
# an you will find the "es_name" into the #attributes hash
|
193
|
+
|
161
194
|
end
|
162
195
|
````
|
163
196
|
|
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
LocalizableDb.config do |config|
|
2
|
+
# config.supported_languages = [I18n.default_locale]
|
3
|
+
# config.default_language = I18n.default_locale
|
4
|
+
# config.enable_i18n_integration = true
|
5
|
+
# config.enable_getters = true
|
6
|
+
# config.attributes_integration = true
|
6
7
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module LocalizableDb
|
2
|
+
module Languages
|
3
|
+
|
4
|
+
def config
|
5
|
+
const_set("DEFAULT", LocalizableDb.configuration.default_language.to_sym || I18n.default_locale )
|
6
|
+
const_set(
|
7
|
+
"SUPPORTED",
|
8
|
+
(LocalizableDb.configuration.supported_languages.map do |language|
|
9
|
+
language.to_sym
|
10
|
+
end | [LocalizableDb.configuration.default_language.to_sym].flatten)
|
11
|
+
)
|
12
|
+
const_set(
|
13
|
+
"NOT_DEFAULT",
|
14
|
+
(SUPPORTED - [DEFAULT])
|
15
|
+
)
|
16
|
+
end
|
17
|
+
module_function :config
|
18
|
+
end
|
19
|
+
end
|
@@ -1,231 +1,164 @@
|
|
1
1
|
module LocalizableDb
|
2
2
|
module Localizable
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
5
4
|
included do
|
6
5
|
|
7
|
-
def
|
8
|
-
self.class.
|
6
|
+
def is_localizable?
|
7
|
+
self.class.is_localizable?
|
9
8
|
end
|
10
9
|
|
11
|
-
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
attr_accessor :is_localizable
|
12
15
|
|
13
|
-
def
|
14
|
-
self.
|
16
|
+
def is_localizable?
|
17
|
+
self.is_localizable
|
15
18
|
end
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
20
|
+
def localize(*localizable_attributes)
|
21
|
+
self.is_localizable = true
|
22
|
+
class_eval do
|
23
|
+
default_scope(if: LocalizableDb.configuration.enable_i18n_integration) do
|
24
|
+
localized
|
25
|
+
end
|
26
|
+
mattr_accessor :localized_table_name
|
27
|
+
mattr_accessor :normalized_table_name
|
28
|
+
mattr_accessor :localizable_attributes
|
29
|
+
end
|
30
|
+
self.localized_table_name = "#{self.table_name.singularize}_languages"
|
31
|
+
self.normalized_table_name = self.table_name
|
32
|
+
self.localizable_attributes = localizable_attributes
|
33
|
+
aux_localized_table_name = self.localized_table_name
|
34
|
+
aux_model_name = self.name
|
35
|
+
self.const_set("#{self.name}Language", Class.new(ApplicationRecord) do
|
36
|
+
self.table_name = aux_localized_table_name
|
37
|
+
belongs_to aux_model_name.downcase.to_sym,
|
38
|
+
class_name: aux_model_name,
|
39
|
+
foreign_key: "localizable_object_id",
|
40
|
+
inverse_of: aux_localized_table_name.to_sym
|
41
|
+
end)
|
42
|
+
class_eval do
|
43
|
+
has_many self.localized_table_name.to_sym,
|
44
|
+
class_name: "#{self.name}Language",
|
45
|
+
foreign_key: "localizable_object_id",
|
46
|
+
inverse_of: aux_model_name.downcase.to_sym,
|
47
|
+
dependent: :destroy
|
48
|
+
|
49
|
+
accepts_nested_attributes_for self.localized_table_name.to_sym
|
50
|
+
end
|
51
|
+
if LocalizableDb.configuration.enable_getters
|
52
|
+
self.localizable_attributes.each do |attribute|
|
53
|
+
class_eval %Q{
|
54
|
+
def get_#{attribute}(language = LocalizableDb::Languages::DEFAULT)
|
55
|
+
if language == LocalizableDb::Languages::DEFAULT
|
56
|
+
self.attributes["#{attribute.to_s}"]
|
57
|
+
else
|
58
|
+
self.attributes[language.to_s + "_#{attribute.to_s}"]
|
48
59
|
end
|
49
60
|
end
|
50
|
-
|
51
|
-
self.errors.add("#{language_key}_#{err_key}", err_message)
|
52
|
-
end if aux_object.errors.any?
|
53
|
-
aux_object
|
61
|
+
}
|
54
62
|
end
|
55
|
-
self.class.table_name = self.class.name.pluralize.dasherize.downcase
|
56
|
-
raise ActiveRecord::Rollback, "Languages error" if self.errors.any?
|
57
63
|
end
|
58
|
-
|
59
|
-
self.class.table_name = self.class.name.pluralize.dasherize.downcase
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
module ClassMethods
|
64
|
+
class << self
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
singularized_model_name = self.name.downcase.singularize.dasherize
|
69
|
-
class_eval %Q{
|
70
|
-
|
71
|
-
default_scope { localize_eager_load }
|
72
|
-
default_scope { with_languages_eager_load }
|
73
|
-
|
74
|
-
attr_accessor :languages, :_locale, :destroying_languages
|
75
|
-
|
76
|
-
after_save :save_languages
|
77
|
-
before_destroy do
|
78
|
-
if self.class.table_name != self.class.localized_table_name
|
79
|
-
self.destroying_languages = true
|
80
|
-
self.class.table_name = self.class.localized_table_name
|
81
|
-
self.class.where(localizable_object_id: self.id).destroy_all
|
82
|
-
self.class.table_name = self.class.name.pluralize.dasherize
|
83
|
-
end
|
84
|
-
self.class.table_name = self.class.name.pluralize.dasherize.downcase if self.destroying_languages
|
85
|
-
self.destroying_languages = false if self.destroying_languages
|
86
|
-
ensure
|
87
|
-
end
|
88
|
-
after_commit do
|
89
|
-
self.class.table_name = self.class.name.pluralize.dasherize.downcase
|
66
|
+
def get_locale
|
67
|
+
I18n.locale
|
90
68
|
end
|
91
69
|
|
92
|
-
|
93
|
-
|
70
|
+
def localized(*languages)
|
71
|
+
if(defined? LocalizableDb::Localizable and
|
72
|
+
((LocalizableDb.configuration.enable_i18n_integration and self.get_locale != LocalizableDb::Languages::DEFAULT) or
|
73
|
+
(languages.any? and (languages-[LocalizableDb::Languages::DEFAULT]).any?)))
|
74
|
+
languages = LocalizableDb::Languages::SUPPORTED if(
|
75
|
+
languages.any? and languages.first == :all)
|
76
|
+
languages = [self.get_locale] if languages.empty? and LocalizableDb.configuration.enable_i18n_integration
|
77
|
+
languages.map!{|language| language.to_sym}
|
78
|
+
if languages.size == 1
|
79
|
+
language = languages.first
|
80
|
+
return one_language(language)
|
81
|
+
else
|
82
|
+
languages = languages.keep_if do |language|
|
83
|
+
LocalizableDb::Languages::NOT_DEFAULT.include? language
|
84
|
+
end
|
85
|
+
return multiple_languages(languages)
|
86
|
+
end
|
87
|
+
else
|
88
|
+
ActiveRecord::Relation.new(self, self.table_name,self.predicate_builder)
|
89
|
+
end
|
94
90
|
end
|
95
91
|
|
96
|
-
|
97
|
-
self.languages = languages
|
98
|
-
end
|
92
|
+
alias_method :l, :localized
|
99
93
|
|
100
94
|
private
|
101
95
|
|
102
|
-
def
|
103
|
-
|
96
|
+
def one_language(language)
|
97
|
+
attrs_to_select = single_languege_attrs_to_select_conf(language)
|
98
|
+
from("#{self.localized_table_name}").joins("
|
99
|
+
JOIN (
|
100
|
+
SELECT #{self.table_name}.id, #{attrs_to_select.join(',')}
|
101
|
+
FROM #{self.localized_table_name}, #{self.table_name}
|
102
|
+
WHERE #{self.localized_table_name}.locale = '#{language.to_s}'
|
103
|
+
AND #{self.localized_table_name}.localizable_object_id = #{self.table_name}.id
|
104
|
+
) AS #{self.table_name}
|
105
|
+
ON #{self.localized_table_name}.locale = '#{language.to_s}'
|
106
|
+
AND #{self.localized_table_name}.localizable_object_id = #{self.table_name}.id
|
107
|
+
")
|
104
108
|
end
|
105
109
|
|
106
|
-
def
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
def self.get_locale
|
111
|
-
I18n.locale
|
112
|
-
end
|
113
|
-
|
114
|
-
}
|
115
|
-
|
116
|
-
class << self
|
117
|
-
|
118
|
-
def localized(language = nil)
|
119
|
-
if defined? LocalizableDb::Localizable and
|
120
|
-
(self.get_locale != I18n.default_locale or
|
121
|
-
(language and language != I18n.default_locale))
|
122
|
-
|
123
|
-
language = self.get_locale unless language
|
124
|
-
raise "The locale :#{language} is not defined in the initialization file, please check config/initializers/localizable_db.rb to add it." if !LocalizableDb::Languages::SUPPORTED.include? language
|
125
|
-
attrs_to_select = ""
|
126
|
-
self.attribute_names.each do |attribute|
|
127
|
-
attrs_to_select += "#{self.table_name}.#{attribute}"
|
128
|
-
attrs_to_select += ", " if attribute != self.attribute_names.last
|
129
|
-
end
|
130
|
-
self.localized_attributes.each do |a|
|
131
|
-
attrs_to_select += ", " if a == self.localized_attributes.first
|
132
|
-
attrs_to_select += "#{self.localized_table_name}.#{a} as #{a}"
|
133
|
-
attrs_to_select += ", " if a != self.localized_attributes.last
|
134
|
-
end
|
135
|
-
aux_select_values = joins("
|
136
|
-
JOIN #{self.localized_table_name}
|
137
|
-
ON locale = '#{language.to_s}'
|
138
|
-
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
139
|
-
").select_values.map{|select_value| select_value.to_s }.join(' ')
|
140
|
-
localized_chain = (aux_select_values.scan(/#{self.localized_table_name}/).any? ? true : false)
|
141
|
-
result = joins("
|
142
|
-
JOIN #{self.localized_table_name}
|
143
|
-
ON locale = '#{language.to_s}'
|
144
|
-
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
145
|
-
").select(attrs_to_select) if !localized_chain
|
146
|
-
result = unscope(:joins, :select).joins("
|
147
|
-
JOIN #{self.localized_table_name}
|
148
|
-
ON locale = '#{language.to_s}'
|
149
|
-
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
150
|
-
").select(attrs_to_select) if localized_chain
|
151
|
-
if block_given?
|
152
|
-
ActiveRecord::Base._localized_eager_load = true
|
153
|
-
result = yield(result).reload
|
154
|
-
ActiveRecord::Base._localized_eager_load = false
|
155
|
-
end
|
156
|
-
result
|
157
|
-
else
|
158
|
-
return where(id: nil).unscope(where: :id)
|
110
|
+
def multiple_languages(languages)
|
111
|
+
attrs_to_select = (self.attribute_names).map do |attribute|
|
112
|
+
"#{self.table_name}.#{attribute.to_s}"
|
159
113
|
end
|
160
|
-
|
161
|
-
ActiveRecord::Base._localized_eager_load = false
|
162
|
-
end
|
163
|
-
|
164
|
-
def with_languages(*with_languages)
|
165
|
-
with_languages.flatten!
|
166
|
-
with_languages = with_languages - [LocalizableDb::Languages::DEFAULT] if with_languages.any?
|
167
|
-
with_languages = (LocalizableDb::Languages::SUPPORTED - [LocalizableDb::Languages::DEFAULT]) if with_languages.empty?
|
168
|
-
attrs_to_select = "#{self.table_name}.*, "
|
169
|
-
tables_to_select = "#{self.table_name}, "
|
114
|
+
tables_to_select = ""
|
170
115
|
conditions_to_select = ""
|
171
|
-
|
172
|
-
self.
|
173
|
-
|
174
|
-
conditions_to_select += "#{language.to_s}_#{self.localized_table_name}.locale = '#{language.to_s}' AND "
|
175
|
-
conditions_to_select += "#{language.to_s}_#{self.localized_table_name}.localizable_object_id = #{self.table_name}.id "
|
176
|
-
attrs_to_select += ", " if localized_attribute != self.localized_attributes.last
|
177
|
-
conditions_to_select += "AND " if language != with_languages.last or localized_attribute != self.localized_attributes.last
|
116
|
+
languages.each do |language|
|
117
|
+
attrs_to_select = attrs_to_select | self.localizable_attributes.map do |attribute|
|
118
|
+
"#{language.to_s}_#{self.localized_table_name}.#{attribute.to_s} as #{language.to_s}_#{attribute.to_s}"
|
178
119
|
end
|
120
|
+
tables_to_select += "," if language != languages.first
|
179
121
|
tables_to_select += "#{self.localized_table_name} as #{language.to_s}_#{self.localized_table_name}"
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
result = unscope(:joins).unscope(:select)
|
184
|
-
.select(attrs_to_select)
|
185
|
-
.from(tables_to_select)
|
186
|
-
.where(conditions_to_select)
|
187
|
-
# result = unscope(:joins).joins("
|
188
|
-
# JOIN #{self.localized_table_name}
|
189
|
-
# ON locale IN (#{with_languages.map{|l| '"' + l.to_s + '"' }.join(', ')})
|
190
|
-
# AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
191
|
-
# ").select("products.*, product_languages.*")
|
192
|
-
if block_given?
|
193
|
-
ActiveRecord::Base._with_languages_eager_load = true
|
194
|
-
result = yield(result).reload
|
195
|
-
ActiveRecord::Base._with_languages_eager_load = false
|
122
|
+
conditions_to_select += "#{language.to_s}_#{self.localized_table_name}.locale = '#{language.to_s}'"
|
123
|
+
conditions_to_select += " AND #{language.to_s}_#{self.localized_table_name}.localizable_object_id = #{self.table_name}.id"
|
124
|
+
conditions_to_select += " AND " if language != languages.last
|
196
125
|
end
|
197
|
-
|
198
|
-
|
199
|
-
|
126
|
+
from("#{tables_to_select}").joins("
|
127
|
+
JOIN (
|
128
|
+
SELECT #{self.table_name}.id, #{attrs_to_select.join(',')}
|
129
|
+
FROM #{self.table_name}, #{tables_to_select}
|
130
|
+
WHERE #{conditions_to_select}
|
131
|
+
) AS #{self.table_name}
|
132
|
+
ON #{conditions_to_select}
|
133
|
+
")
|
200
134
|
end
|
201
135
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
ActiveRecord::Relation.new(self, self.table_name,self.predicate_builder)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
def with_languages_eager_load(with_languages = [])
|
216
|
-
if ActiveRecord::Base._with_languages_eager_load
|
217
|
-
wl(with_languages)
|
136
|
+
def single_languege_attrs_to_select_conf(language = nil)
|
137
|
+
if LocalizableDb.configuration.attributes_integration
|
138
|
+
attrs_to_select = self.attribute_names - ["id"] - self.localizable_attributes.map do |attribute|
|
139
|
+
attribute.to_s
|
140
|
+
end
|
141
|
+
attrs_to_select = attrs_to_select | self.localizable_attributes.map do |attribute|
|
142
|
+
["#{self.localized_table_name}.#{attribute.to_s}","#{self.localized_table_name}.#{attribute.to_s} as #{language}_#{attribute}"]
|
143
|
+
end
|
144
|
+
attrs_to_select.flatten!
|
218
145
|
else
|
219
|
-
|
146
|
+
attrs_to_select = self.attribute_names - ["id"]
|
147
|
+
attrs_to_select = attrs_to_select.map do |attribute|
|
148
|
+
"#{self.table_name}.#{attribute}"
|
149
|
+
end | self.localizable_attributes.map do |attribute|
|
150
|
+
"#{self.localized_table_name}.#{attribute.to_s} as #{language}_#{attribute}"
|
151
|
+
end
|
220
152
|
end
|
153
|
+
attrs_to_select
|
221
154
|
end
|
222
155
|
|
223
156
|
end
|
224
|
-
end
|
225
157
|
|
158
|
+
end
|
226
159
|
end
|
227
160
|
end
|
228
161
|
end
|
229
162
|
|
163
|
+
|
230
164
|
ActiveRecord::Base.send(:include, LocalizableDb::Localizable)
|
231
|
-
ActiveRecord::Base.class_eval{ mattr_accessor :_localized_eager_load, :_with_languages_eager_load}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module LocalizableDb
|
2
|
+
|
3
|
+
mattr_accessor :configuration
|
4
|
+
|
5
|
+
def config
|
6
|
+
@@configuration ||= LocalizableConfig.new
|
7
|
+
yield(@@configuration) if block_given?
|
8
|
+
LocalizableDb::Languages.config if block_given?
|
9
|
+
@@configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
module_function :config
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
class LocalizableConfig
|
17
|
+
|
18
|
+
attr_accessor :attributes_integration
|
19
|
+
attr_accessor :enable_getters
|
20
|
+
attr_accessor :enable_i18n_integration
|
21
|
+
attr_accessor :supported_languages
|
22
|
+
attr_accessor :default_language
|
23
|
+
|
24
|
+
def initialize()
|
25
|
+
@attributes_integration = true
|
26
|
+
@enable_getters = true
|
27
|
+
@enable_i18n_integration = true
|
28
|
+
@supported_languages = [I18n.default_locale]
|
29
|
+
@default_language = I18n.default_locale
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/localizable_db.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: localizable_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yonga9121
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-02-
|
12
|
+
date: 2018-02-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -62,7 +62,9 @@ files:
|
|
62
62
|
- lib/generators/localizable_db/templates/initializer.rb
|
63
63
|
- lib/generators/localizable_db/templates/migration_for.rb
|
64
64
|
- lib/localizable_db.rb
|
65
|
+
- lib/localizable_db/languages.rb
|
65
66
|
- lib/localizable_db/localizable.rb
|
67
|
+
- lib/localizable_db/localizable_config.rb
|
66
68
|
- lib/localizable_db/version.rb
|
67
69
|
- lib/tasks/localizable_db_tasks.rake
|
68
70
|
homepage: https://yonga9121.github.io/localizable_db
|