localizable_db 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +180 -0
- data/Rakefile +34 -0
- data/lib/generators/localizable_db/USAGE +8 -0
- data/lib/generators/localizable_db/install_generator.rb +13 -0
- data/lib/generators/localizable_db/localizable_db_generator.rb +7 -0
- data/lib/generators/localizable_db/migration_generator.rb +58 -0
- data/lib/generators/localizable_db/model_generator.rb +60 -0
- data/lib/generators/localizable_db/orm_helper.rb +39 -0
- data/lib/generators/localizable_db/templates/create_table_migration.rb +24 -0
- data/lib/generators/localizable_db/templates/initializer.rb +6 -0
- data/lib/generators/localizable_db/templates/migration_for.rb +18 -0
- data/lib/localizable_db/localizable.rb +192 -0
- data/lib/localizable_db/version.rb +3 -0
- data/lib/localizable_db.rb +4 -0
- data/lib/tasks/localizable_db_tasks.rake +4 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d5997e3a00846975be28b21d00633fd0164a8bbca7ec0b533ec7eb6e036c54d
|
4
|
+
data.tar.gz: 3ad8259df4d0e02b08c64c7dfdbd4716141bd91478cfbdda57e304a5e3a1fc4a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d68002cc1a572adf60cee459ef1e8f0432cedc4fbd33db18976954b01aaae59b80f9987104c48703f2709a5e110eca016358eb99d5228714a0010aff5c7af08
|
7
|
+
data.tar.gz: 2506d5072abed1ec00b48d849b367f645970e6bfe03edc3b3951e6a604aa4db11873cdfc2f7f7f9229e131bb7556b9eb03922cb7fa0ab2af55ad007e78be7eeb
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Juan-Felipe-Forero
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# LocalizableDb
|
2
|
+
Rails gem to localize your database.
|
3
|
+
|
4
|
+
If your application manage something like products or services that can be created dynamically, and you have to support multiple languages you may need to localize your database. LocalizableDb allow you to do that in a simple way.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
I18n Integration.
|
9
|
+
````ruby
|
10
|
+
Product.find(1) #=> #<Product id: 1, name: "luck">
|
11
|
+
Product.where(id: 1)
|
12
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck">]>
|
13
|
+
|
14
|
+
I18n.locale = :es
|
15
|
+
|
16
|
+
Product.l.find(1) #=> <Product id: 1, name: "suerte">
|
17
|
+
Product.l.where(id: 1)
|
18
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "suerte">]>
|
19
|
+
|
20
|
+
````
|
21
|
+
Specify the lenguage you want
|
22
|
+
|
23
|
+
````ruby
|
24
|
+
Product.find(1) #=> <Product id: 1, name: "luck">
|
25
|
+
Product.where(id: 1)
|
26
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck">]>
|
27
|
+
|
28
|
+
Product.l(:es).find(1) #=> <Product id: 1, name: "suerte">
|
29
|
+
Product.l(:es).where(id: 1)
|
30
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "suerte">]>
|
31
|
+
|
32
|
+
Product.l(:pt).find(1) #=> <Product id: 1, name: "sortudo">
|
33
|
+
Product.l(:pt).where(id: 1)
|
34
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "sortudo">]>
|
35
|
+
|
36
|
+
Product.l(:fr).find(1) #=> <Product id: 1, name: "heureux">
|
37
|
+
Product.l(:fr).where(id: 1)
|
38
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "heureux">]>
|
39
|
+
````
|
40
|
+
Localize multiple languages
|
41
|
+
````ruby
|
42
|
+
products = Product.where(id: 1)
|
43
|
+
products.inspect
|
44
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck">]>
|
45
|
+
products = Product.wl(:es,:pt,:fr).where(id: 1)
|
46
|
+
products.inspect
|
47
|
+
#=> <ActiveRecord::Relation [#<Product id: 1, name: "luck", es_name: "suerte", pt_name: "sortudo", fr_name: "heureux">]>
|
48
|
+
|
49
|
+
products.first.name #=> luck
|
50
|
+
products.first.attributes["es_name"] #=> suerte
|
51
|
+
products.first.attributes["pt_name"] #=> sortudo
|
52
|
+
products.first.attributes["fr_name"] #=> heureux
|
53
|
+
````
|
54
|
+
Creating
|
55
|
+
````ruby
|
56
|
+
Product.create(name: "something", languages: {
|
57
|
+
es: {name: "algo"},
|
58
|
+
pt: {name: "alguma cosia"},
|
59
|
+
fr: {name: "quelque chose"}
|
60
|
+
})
|
61
|
+
#=> #<Product id:2, name: "something">
|
62
|
+
````
|
63
|
+
Saving
|
64
|
+
````ruby
|
65
|
+
Product.new(name: "love", languages: {
|
66
|
+
es: {name: "amor"},
|
67
|
+
pt: {name: "amor"},
|
68
|
+
fr: {name: "amour"}
|
69
|
+
}).save
|
70
|
+
#=> #<Product id:3, name: "love">
|
71
|
+
|
72
|
+
love = Product.last
|
73
|
+
love.set_languages({
|
74
|
+
es: {name: "amor :D"},
|
75
|
+
pt: {name: "amor :D"},
|
76
|
+
fr: {name: "amouuurt"}
|
77
|
+
})
|
78
|
+
love.save
|
79
|
+
#=> #<Product id: 3, name: "love">
|
80
|
+
love = Product.l(:fr).find(3)
|
81
|
+
love.inspect
|
82
|
+
#=> #<Product id: 3, name: "amouuurt">
|
83
|
+
````
|
84
|
+
Updating
|
85
|
+
````ruby
|
86
|
+
product = Product.find(3)
|
87
|
+
product.update(name: "the love", languages: {
|
88
|
+
es: {name: "el amor"},
|
89
|
+
pt: {name: "o amor"},
|
90
|
+
fr: {name: "l'amour"}
|
91
|
+
})
|
92
|
+
#=> #<Product id:3, name: "the love">
|
93
|
+
|
94
|
+
product = Product.l(:fr).find(3)
|
95
|
+
product.inspect
|
96
|
+
#=> #<Product id: 3, name: "l'amour">
|
97
|
+
````
|
98
|
+
Eager loading support
|
99
|
+
````ruby
|
100
|
+
products = Product.includes(:features).where(id: 1)
|
101
|
+
products.first.features.inspect
|
102
|
+
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Makes people happy">]>
|
103
|
+
|
104
|
+
products = Product.l(:es) do |products|
|
105
|
+
products.includes(:features)
|
106
|
+
end.where(id: 1)
|
107
|
+
products.first.features.inspect
|
108
|
+
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Hace a la gente feliz">]>
|
109
|
+
````
|
110
|
+
Eager loading support for multiple languages
|
111
|
+
````ruby
|
112
|
+
products = Product.includes(:features).where(id: 1)
|
113
|
+
products.first.features.inspect
|
114
|
+
#=> <ActiveRecord::Relation [#<Feature id: 1, desc: "Makes people happy">]>
|
115
|
+
|
116
|
+
products = Product.wl(:es,:pt,:fr) do |products|
|
117
|
+
products.includes(:features)
|
118
|
+
end.where(id: 1)
|
119
|
+
products.first.features.inspect
|
120
|
+
#=> <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">]>
|
121
|
+
````
|
122
|
+
#### NOTE:
|
123
|
+
If you need to use aggregation functions or grouping stuff, use the raw model without calling the localize method (.l), otherwise a syntax error will be raised from ActiveRecord. We still working on this feature, if you want to help us, feel free to make a pull request :)
|
124
|
+
|
125
|
+
## Installation
|
126
|
+
Add this line to your application's Gemfile:
|
127
|
+
```ruby
|
128
|
+
gem 'localizable_db'
|
129
|
+
```
|
130
|
+
|
131
|
+
And then execute:
|
132
|
+
```bash
|
133
|
+
$ bundle
|
134
|
+
```
|
135
|
+
|
136
|
+
Then Install it!
|
137
|
+
````bash
|
138
|
+
$ rails g localizable_db:install
|
139
|
+
````
|
140
|
+
|
141
|
+
Configure your supported languages
|
142
|
+
````ruby
|
143
|
+
# config/initializers/localizable_db_initializer_.rb
|
144
|
+
module LocalizableDb
|
145
|
+
module Languages
|
146
|
+
DEFAULT = :en
|
147
|
+
SUPPORTED = [:en, :es] #=> Add your locales to this array.
|
148
|
+
end
|
149
|
+
end
|
150
|
+
````
|
151
|
+
|
152
|
+
## Generating
|
153
|
+
Generate a localizable model.
|
154
|
+
````bash
|
155
|
+
rails g localizable_db:model Product name:string desc:text other:string
|
156
|
+
````
|
157
|
+
|
158
|
+
Generate a migration for a localizable model.
|
159
|
+
````bash
|
160
|
+
rails g localizable_db:migration Product name:string desc:text other:string
|
161
|
+
````
|
162
|
+
|
163
|
+
|
164
|
+
## Setting up your models
|
165
|
+
You need to call the localize method on your models, so localizable_db knows which attributes are localizable. Notice that the localizable attributes that you define in the model must have a column in the related localized table.
|
166
|
+
|
167
|
+
````ruby
|
168
|
+
# app/models/product.rb
|
169
|
+
class Product < ApplicationRecord
|
170
|
+
localize :name, :desc
|
171
|
+
end
|
172
|
+
````
|
173
|
+
|
174
|
+
## Authors
|
175
|
+
[yonga9121](github.com/yonga9121)
|
176
|
+
[Nevinyrral](github.com/Nevinyrral)
|
177
|
+
|
178
|
+
|
179
|
+
## License
|
180
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'LocalizableDb'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module LocalizableDb
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.join(File.expand_path('../templates', __FILE__))
|
6
|
+
|
7
|
+
def copy_initializer_file
|
8
|
+
copy_file "initializer.rb", "config/initializers/localizable_db_initializer.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'generators/localizable_db/orm_helper'
|
2
|
+
require 'rails/generators/active_record/migration/migration_generator'
|
3
|
+
|
4
|
+
module LocalizableDb
|
5
|
+
module Generators
|
6
|
+
class MigrationGenerator < ActiveRecord::Generators::MigrationGenerator
|
7
|
+
|
8
|
+
include LocalizableDb::Generators::OrmHelper
|
9
|
+
|
10
|
+
source_root File.join(
|
11
|
+
File.dirname(
|
12
|
+
ActiveRecord::Generators::MigrationGenerator
|
13
|
+
.instance_method(:create_migration_file).source_location.first
|
14
|
+
), 'templates'
|
15
|
+
)
|
16
|
+
|
17
|
+
def create_migration_file
|
18
|
+
if !model_exists? and behavior == :invoke
|
19
|
+
raise "Model #{table_name.singularize.camelize} doesn't exist," +
|
20
|
+
" please run 'bundle exec rails g localizable_db:model #{table_name.
|
21
|
+
singularize.camelize}' or create the model #{table_name
|
22
|
+
.singularize.camelize} and run localizable_db:migration again."
|
23
|
+
return
|
24
|
+
end
|
25
|
+
|
26
|
+
attributes.each { |a|
|
27
|
+
a.attr_options.delete(:index) if a.reference? && !a.has_index?
|
28
|
+
} if options[:indexes] == false
|
29
|
+
|
30
|
+
template = "#{__FILE__}/../templates/migration_for.rb"
|
31
|
+
migration_name =
|
32
|
+
"db/migrate/create_#{table_name.singularize}_languages.rb"
|
33
|
+
migration_template(
|
34
|
+
template, migration_name, migration_version: migration_version
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_localizable_model
|
39
|
+
if !model_exists? and behavior == :invoke
|
40
|
+
raise "Model #{table_name.singularize.camelize} doesn't exist"
|
41
|
+
return
|
42
|
+
end
|
43
|
+
|
44
|
+
case behavior
|
45
|
+
when :invoke
|
46
|
+
inject_into_class(
|
47
|
+
Rails.root.join("app", "models", "#{table_name.singularize}.rb"),
|
48
|
+
Object.const_get(table_name.singularize.camelize)
|
49
|
+
) do
|
50
|
+
%Q{\tlocalize\n}
|
51
|
+
end
|
52
|
+
when :revoke
|
53
|
+
# Do nothing
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'generators/localizable_db/orm_helper'
|
2
|
+
require 'rails/generators/active_record/model/model_generator'
|
3
|
+
|
4
|
+
module LocalizableDb
|
5
|
+
module Generators
|
6
|
+
class ModelGenerator < ActiveRecord::Generators::ModelGenerator
|
7
|
+
|
8
|
+
include LocalizableDb::Generators::OrmHelper
|
9
|
+
# include ActiveRecord::Generators::MigrationGenerator
|
10
|
+
# include ActiveRecord::Generators::ModelGenerator
|
11
|
+
|
12
|
+
source_root File.join(
|
13
|
+
File.dirname(
|
14
|
+
ActiveRecord::Generators::ModelGenerator
|
15
|
+
.instance_method(:create_migration_file).source_location.first
|
16
|
+
), "templates"
|
17
|
+
)
|
18
|
+
|
19
|
+
def create_migration_file
|
20
|
+
return unless options[:migration] && options[:parent].nil?
|
21
|
+
|
22
|
+
attributes.each { |a|
|
23
|
+
a.attr_options.delete(:index) if a.reference? && !a.has_index?
|
24
|
+
} if options[:indexes] == false
|
25
|
+
|
26
|
+
model_template = "#{__FILE__}/../templates/create_table_migration.rb"
|
27
|
+
model_migration_name =
|
28
|
+
"db/migrate/create_#{table_name}.rb"
|
29
|
+
|
30
|
+
migration_template(
|
31
|
+
model_template, model_migration_name,
|
32
|
+
migration_version: migration_version
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def generate_migration_for_localizable
|
37
|
+
case behavior
|
38
|
+
when :invoke
|
39
|
+
invoke(
|
40
|
+
"localizable_db:migration",
|
41
|
+
[
|
42
|
+
table_name.singularize.camelize
|
43
|
+
] +
|
44
|
+
attributes.map { |attribute|
|
45
|
+
if attribute.type.eql? :string
|
46
|
+
"#{attribute.name}:#{attribute.type.to_s}"
|
47
|
+
end
|
48
|
+
}
|
49
|
+
)
|
50
|
+
when :revoke
|
51
|
+
invoke(
|
52
|
+
"localizable_db:migration",
|
53
|
+
[ table_name ], behavior: :revoke
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module LocalizableDb
|
2
|
+
module Generators
|
3
|
+
module OrmHelper
|
4
|
+
|
5
|
+
include LocalizableDb::Languages
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def model_exists?
|
10
|
+
File.exist?(File.join(destination_root, model_path))
|
11
|
+
end
|
12
|
+
|
13
|
+
def migration_exists?(table_name)
|
14
|
+
Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb")
|
15
|
+
.grep(/\d+_add_localizable_to_#{table_name}.rb$/).first
|
16
|
+
end
|
17
|
+
|
18
|
+
def migration_path
|
19
|
+
@migration_path ||= File.join("db", "migrate")
|
20
|
+
end
|
21
|
+
|
22
|
+
def model_path
|
23
|
+
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
def migration_version
|
27
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_data
|
31
|
+
{
|
32
|
+
localizable_object_id: true,
|
33
|
+
locale: true
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
def change
|
3
|
+
create_table :<%= table_name %><%= primary_key_type %> do |t|
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
<% if attribute.password_digest? -%>
|
6
|
+
t.string :password_digest<%= attribute.inject_options %>
|
7
|
+
<% elsif attribute.token? -%>
|
8
|
+
t.string :<%= attribute.name %><%= attribute.inject_options %>
|
9
|
+
<% else -%>
|
10
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
11
|
+
<% end -%>
|
12
|
+
<% end -%>
|
13
|
+
<% if options[:timestamps] %>
|
14
|
+
t.timestamps
|
15
|
+
<% end -%>
|
16
|
+
end
|
17
|
+
<% attributes.select(&:token?).each do |attribute| -%>
|
18
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
|
19
|
+
<% end -%>
|
20
|
+
<% attributes_with_index.each do |attribute| -%>
|
21
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
22
|
+
<% end -%>
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Create<%= table_name.singularize.camelize %>Languages < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
|
3
|
+
def change
|
4
|
+
create_table :<%= table_name.singularize %>_languages do |t|
|
5
|
+
<% migration_data.each do | key, value | -%>
|
6
|
+
<% if key.eql? :localizable_object_id -%>
|
7
|
+
t.integer :localizable_object_id, index: true
|
8
|
+
<% elsif key.eql? :locale -%>
|
9
|
+
t.string :locale, null: false, default: "<%= LocalizableDb::Languages::DEFAULT %>"
|
10
|
+
<% end -%>
|
11
|
+
<% end -%>
|
12
|
+
<% attributes.each do |attribute| -%>
|
13
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
14
|
+
<% end -%>
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
module LocalizableDb
|
2
|
+
module Localizable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
|
7
|
+
def save_languages
|
8
|
+
if !self._locale and self.languages
|
9
|
+
self.languages.each do |language_key, language_values|
|
10
|
+
self.class.table_name = self.class.localized_table_name
|
11
|
+
aux_object = nil
|
12
|
+
aux_object = self.class.find_by(locale: language_key.to_s, localizable_object_id: self.id) if self.id
|
13
|
+
aux_object = self.class.new if !self.id or !aux_object
|
14
|
+
aux_object.locale = language_key.to_s
|
15
|
+
self.class.localized_attributes.each do |attribute|
|
16
|
+
aux_object.send(:"#{attribute}=", language_values[attribute.to_sym])
|
17
|
+
end
|
18
|
+
aux_object.localizable_object_id = self.id
|
19
|
+
aux_object.save
|
20
|
+
aux_object.errors.messages.each do |err_key,err_message|
|
21
|
+
self.errors.add("#{language_key}_#{err_key}", err_message)
|
22
|
+
end if aux_object.errors.any?
|
23
|
+
aux_object
|
24
|
+
end
|
25
|
+
self.class.table_name = self.class.name.pluralize.dasherize.downcase
|
26
|
+
end
|
27
|
+
ensure
|
28
|
+
self.class.table_name = self.class.name.pluralize.dasherize.downcase
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
|
35
|
+
def localize(*localizable_attributes)
|
36
|
+
localizable_attributes.flatten!
|
37
|
+
singularized_model_name = self.name.downcase.singularize.dasherize
|
38
|
+
class_eval %Q{
|
39
|
+
|
40
|
+
default_scope { localize_eager_load }
|
41
|
+
default_scope { with_languages_eager_load }
|
42
|
+
|
43
|
+
attr_accessor :languages, :_locale, :destroying_languages
|
44
|
+
|
45
|
+
after_save :save_languages
|
46
|
+
before_destroy do
|
47
|
+
if self.class.table_name != self.class.localized_table_name
|
48
|
+
self.destroying_languages = true
|
49
|
+
self.class.table_name = self.class.localized_table_name
|
50
|
+
self.class.where(localizable_object_id: self.id).destroy_all
|
51
|
+
self.class.table_name = self.class.name.pluralize.dasherize
|
52
|
+
end
|
53
|
+
ensure
|
54
|
+
self.class.table_name = self.class.name.pluralize.dasherize.downcase if self.destroying_languages
|
55
|
+
self.destroying_languages = false if self.destroying_languages
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_languages(languages = {})
|
59
|
+
self.languages = languages
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def self.localized_table_name
|
65
|
+
"#{singularized_model_name}_languages".freeze
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.localized_attributes
|
69
|
+
#{localizable_attributes}.map{|a| a.to_s}.freeze
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.get_locale
|
73
|
+
I18n.locale
|
74
|
+
end
|
75
|
+
|
76
|
+
}
|
77
|
+
|
78
|
+
class << self
|
79
|
+
|
80
|
+
def localized(language = nil)
|
81
|
+
if defined? LocalizableDb::Localizable and
|
82
|
+
(self.get_locale != I18n.default_locale or
|
83
|
+
(language and language != I18n.default_locale))
|
84
|
+
|
85
|
+
language = self.get_locale unless language
|
86
|
+
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
|
87
|
+
attrs_to_select = ""
|
88
|
+
self.attribute_names.each do |attribute|
|
89
|
+
attrs_to_select += "#{self.table_name}.#{attribute}"
|
90
|
+
attrs_to_select += ", " if attribute != self.attribute_names.last
|
91
|
+
end
|
92
|
+
self.localized_attributes.each do |a|
|
93
|
+
attrs_to_select += ", " if a == self.localized_attributes.first
|
94
|
+
attrs_to_select += "#{self.localized_table_name}.#{a} as #{a}"
|
95
|
+
attrs_to_select += ", " if a != self.localized_attributes.last
|
96
|
+
end
|
97
|
+
aux_select_values = joins("
|
98
|
+
JOIN #{self.localized_table_name}
|
99
|
+
ON locale = '#{language.to_s}'
|
100
|
+
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
101
|
+
").select_values.map{|select_value| select_value.to_s }.join(' ')
|
102
|
+
localized_chain = (aux_select_values.scan(/#{self.localized_table_name}/).any? ? true : false)
|
103
|
+
result = joins("
|
104
|
+
JOIN #{self.localized_table_name}
|
105
|
+
ON locale = '#{language.to_s}'
|
106
|
+
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
107
|
+
").select(attrs_to_select) if !localized_chain
|
108
|
+
result = unscope(:joins, :select).joins("
|
109
|
+
JOIN #{self.localized_table_name}
|
110
|
+
ON locale = '#{language.to_s}'
|
111
|
+
AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
112
|
+
").select(attrs_to_select) if localized_chain
|
113
|
+
if block_given?
|
114
|
+
ActiveRecord::Base._localized_eager_load = true
|
115
|
+
result = yield(result).reload
|
116
|
+
ActiveRecord::Base._localized_eager_load = false
|
117
|
+
end
|
118
|
+
result
|
119
|
+
else
|
120
|
+
return where(id: nil).unscope(where: :id)
|
121
|
+
end
|
122
|
+
ensure
|
123
|
+
ActiveRecord::Base._localized_eager_load = false
|
124
|
+
end
|
125
|
+
|
126
|
+
def with_languages(*with_languages)
|
127
|
+
with_languages.flatten!
|
128
|
+
with_languages = LocalizableDb::Languages::SUPPORTED if with_languages.empty?
|
129
|
+
attrs_to_select = "#{self.table_name}.*, "
|
130
|
+
tables_to_select = "#{self.table_name}, "
|
131
|
+
conditions_to_select = ""
|
132
|
+
with_languages.each do |language|
|
133
|
+
self.localized_attributes.each do |localized_attribute|
|
134
|
+
attrs_to_select += "#{language.to_s}_#{self.localized_table_name}.#{localized_attribute} as #{language.to_s}_#{localized_attribute}"
|
135
|
+
conditions_to_select += "#{language.to_s}_#{self.localized_table_name}.locale = '#{language.to_s}' AND "
|
136
|
+
conditions_to_select += "#{language.to_s}_#{self.localized_table_name}.localizable_object_id = #{self.table_name}.id "
|
137
|
+
attrs_to_select += ", " if localized_attribute != self.localized_attributes.last
|
138
|
+
conditions_to_select += "AND " if language != with_languages.last
|
139
|
+
end
|
140
|
+
tables_to_select += "#{self.localized_table_name} as #{language.to_s}_#{self.localized_table_name}"
|
141
|
+
attrs_to_select += ", " if language != with_languages.last
|
142
|
+
tables_to_select += ", " if language != with_languages.last
|
143
|
+
end
|
144
|
+
result = unscope(:joins)
|
145
|
+
.select(attrs_to_select)
|
146
|
+
.from(tables_to_select)
|
147
|
+
.where(conditions_to_select)
|
148
|
+
# result = unscope(:joins).joins("
|
149
|
+
# JOIN #{self.localized_table_name}
|
150
|
+
# ON locale IN (#{with_languages.map{|l| '"' + l.to_s + '"' }.join(', ')})
|
151
|
+
# AND #{self.table_name}.id = #{self.localized_table_name}.localizable_object_id
|
152
|
+
# ").select("products.*, product_languages.*")
|
153
|
+
if block_given?
|
154
|
+
ActiveRecord::Base._with_languages_eager_load = true
|
155
|
+
result = yield(result).reload
|
156
|
+
ActiveRecord::Base._with_languages_eager_load = false
|
157
|
+
end
|
158
|
+
result
|
159
|
+
ensure
|
160
|
+
ActiveRecord::Base._with_languages_eager_load = false
|
161
|
+
end
|
162
|
+
|
163
|
+
alias_method :l, :localized
|
164
|
+
alias_method :localized, :l
|
165
|
+
alias_method :wl, :with_languages
|
166
|
+
alias_method :with_languages, :wl
|
167
|
+
|
168
|
+
def localize_eager_load(language = nil)
|
169
|
+
if ActiveRecord::Base._localized_eager_load
|
170
|
+
l(language)
|
171
|
+
else
|
172
|
+
ActiveRecord::Relation.new(self, self.table_name,self.predicate_builder)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def with_languages_eager_load(with_languages = [])
|
177
|
+
if ActiveRecord::Base._with_languages_eager_load
|
178
|
+
wl(with_languages)
|
179
|
+
else
|
180
|
+
ActiveRecord::Relation.new(self, self.table_name,self.predicate_builder)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
ActiveRecord::Base.send(:include, LocalizableDb::Localizable)
|
192
|
+
ActiveRecord::Base.class_eval{ mattr_accessor :_localized_eager_load, :_with_languages_eager_load}
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: localizable_db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yonga9121
|
8
|
+
- Nevinyrral
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 5.0.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 5.0.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: sqlite3
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
description: If your application manage something like products or services that can
|
43
|
+
be created dynamically, and you have to support multiple languages you may need
|
44
|
+
to localize your database. LocalizableDb allow you to do that in a simple way.
|
45
|
+
email:
|
46
|
+
- jorgeggayon@gmail.com
|
47
|
+
- montanor@javeriana.edu.co
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- MIT-LICENSE
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/generators/localizable_db/USAGE
|
56
|
+
- lib/generators/localizable_db/install_generator.rb
|
57
|
+
- lib/generators/localizable_db/localizable_db_generator.rb
|
58
|
+
- lib/generators/localizable_db/migration_generator.rb
|
59
|
+
- lib/generators/localizable_db/model_generator.rb
|
60
|
+
- lib/generators/localizable_db/orm_helper.rb
|
61
|
+
- lib/generators/localizable_db/templates/create_table_migration.rb
|
62
|
+
- lib/generators/localizable_db/templates/initializer.rb
|
63
|
+
- lib/generators/localizable_db/templates/migration_for.rb
|
64
|
+
- lib/localizable_db.rb
|
65
|
+
- lib/localizable_db/localizable.rb
|
66
|
+
- lib/localizable_db/version.rb
|
67
|
+
- lib/tasks/localizable_db_tasks.rake
|
68
|
+
homepage: https://yonga9121.github.io/localizable_db/
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.7.3
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Rails gem to localize your database
|
92
|
+
test_files: []
|