txdb 2.1.0 → 3.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/lib/txdb/backends/globalize/helpers.rb +12 -2
- data/lib/txdb/backends/globalize/reader.rb +6 -2
- data/lib/txdb/backends/globalize/writer.rb +8 -8
- data/lib/txdb/iterators/auto_increment_iterator.rb +4 -3
- data/lib/txdb/iterators/globalize_iterator.rb +16 -2
- data/lib/txdb/version.rb +1 -1
- data/spec/backends/globalize/helpers_spec.rb +2 -2
- data/spec/backends/globalize/reader_spec.rb +6 -3
- data/spec/backends/globalize/writer_spec.rb +18 -8
- data/spec/iterators/globalize_iterator_spec.rb +5 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4a966477654b1b80565361f216d6a1a6a60fceb9
|
|
4
|
+
data.tar.gz: d8e8673ffc3fdd4852512555ca11dccb784a662b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6674b957a2fe939d2aa417f1c4ad33c232ebf8c33db1e3f62833cc531a6ce94797e84bacd8d34c99ca5d171d46ba780e01ee348c1b3e4ba4c49d666ba47fe18e
|
|
7
|
+
data.tar.gz: 49a34afa1522fa915039cf2c62efc3f0cde7418c7af0508ab7b8c19d3345b795276ff70243491eafcc4c0585f476e88f2dba20949428a3e2c9e06bfdea3b66f1
|
|
@@ -7,13 +7,23 @@ module Txdb
|
|
|
7
7
|
module Helpers
|
|
8
8
|
def origin_table_name(table_name)
|
|
9
9
|
ActiveSupport::Inflector.pluralize(
|
|
10
|
-
table_name
|
|
11
|
-
)
|
|
10
|
+
origin_table_name_singular(table_name)
|
|
11
|
+
).to_sym
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def origin_column_name(table_name)
|
|
15
|
+
"#{origin_table_name_singular(table_name)}_id".to_sym
|
|
12
16
|
end
|
|
13
17
|
|
|
14
18
|
def resource_slug_for(table)
|
|
15
19
|
Txgh::Utils.slugify("#{table.database.name}-#{table.name}")
|
|
16
20
|
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def origin_table_name_singular(table_name)
|
|
25
|
+
table_name.sub(/_translations\z/, '')
|
|
26
|
+
end
|
|
17
27
|
end
|
|
18
28
|
|
|
19
29
|
Helpers.extend(Helpers)
|
|
@@ -47,12 +47,12 @@ module Txdb
|
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def content
|
|
50
|
-
@content ||= {
|
|
50
|
+
@content ||= { table.name => content_for_records }
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
def content_for_records
|
|
54
54
|
iterator.each_with_object({}) do |record, ret|
|
|
55
|
-
ret[record[
|
|
55
|
+
ret[record[origin_column]] = content_for_record(record)
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
@@ -66,6 +66,10 @@ module Txdb
|
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
def origin_column
|
|
70
|
+
origin_column_name(table.name)
|
|
71
|
+
end
|
|
72
|
+
|
|
69
73
|
def iterator
|
|
70
74
|
@iterator ||= Iterators::GlobalizeIterator.new(table)
|
|
71
75
|
end
|
|
@@ -15,23 +15,23 @@ module Txdb
|
|
|
15
15
|
|
|
16
16
|
def write_content(resource, locale)
|
|
17
17
|
content = deserialize_content(resource.content)
|
|
18
|
-
content = content.fetch(
|
|
18
|
+
content = content.fetch(table.name, {})
|
|
19
19
|
|
|
20
|
-
content.each_pair do |
|
|
21
|
-
update_row(
|
|
20
|
+
content.each_pair do |parent_id, fields|
|
|
21
|
+
update_row(parent_id, fields, locale)
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
private
|
|
26
26
|
|
|
27
|
-
def update_row(
|
|
27
|
+
def update_row(parent_id, fields, locale)
|
|
28
28
|
row = table.connection.where(
|
|
29
|
-
|
|
29
|
+
origin_column => parent_id, locale: locale
|
|
30
30
|
)
|
|
31
31
|
|
|
32
32
|
if row.empty?
|
|
33
33
|
table.connection << fields
|
|
34
|
-
.merge(
|
|
34
|
+
.merge(origin_column => parent_id, locale: locale)
|
|
35
35
|
.merge(created_at)
|
|
36
36
|
.merge(updated_at)
|
|
37
37
|
else
|
|
@@ -57,8 +57,8 @@ module Txdb
|
|
|
57
57
|
YAML.load(content)
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
def
|
|
61
|
-
|
|
60
|
+
def origin_column
|
|
61
|
+
origin_column_name(table.name)
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
|
@@ -29,7 +29,7 @@ module Txdb
|
|
|
29
29
|
last_value = record[column]
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
counter = last_value + 1
|
|
32
|
+
counter = last_value + 1 if last_value
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
@@ -48,12 +48,13 @@ module Txdb
|
|
|
48
48
|
private
|
|
49
49
|
|
|
50
50
|
def records_since(counter)
|
|
51
|
-
sql_column = Sequel.
|
|
51
|
+
sql_column = Sequel.qualify(table_name, column)
|
|
52
52
|
|
|
53
53
|
table.connection
|
|
54
|
+
.select_all(table_name)
|
|
54
55
|
.from(table_name)
|
|
55
56
|
.where { sql_column >= counter }
|
|
56
|
-
.order(
|
|
57
|
+
.order(sql_column)
|
|
57
58
|
.limit(batch_size)
|
|
58
59
|
end
|
|
59
60
|
|
|
@@ -5,8 +5,22 @@ module Txdb
|
|
|
5
5
|
|
|
6
6
|
include Backends::Globalize::Helpers
|
|
7
7
|
|
|
8
|
-
def
|
|
9
|
-
|
|
8
|
+
def records_since(counter)
|
|
9
|
+
super
|
|
10
|
+
.join(origin_table, column => origin_column)
|
|
11
|
+
.where(locale_column => table.source_lang)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def locale_column
|
|
15
|
+
Sequel.qualify(table_name, :locale)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def origin_column
|
|
19
|
+
Sequel.qualify(table_name, origin_column_name(table_name))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def origin_table
|
|
23
|
+
Sequel.expr(origin_table_name(table_name))
|
|
10
24
|
end
|
|
11
25
|
end
|
|
12
26
|
end
|
data/lib/txdb/version.rb
CHANGED
|
@@ -7,12 +7,12 @@ describe Globalize::Helpers, test_config: true do
|
|
|
7
7
|
describe '.origin_table_name' do
|
|
8
8
|
it 'pluralizes and removes the translations suffix' do
|
|
9
9
|
origin = Globalize::Helpers.origin_table_name('widget_translations')
|
|
10
|
-
expect(origin).to eq(
|
|
10
|
+
expect(origin).to eq(:widgets)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
it 'handles unusual pluralizations (via ActiveSupport::Inflector)' do
|
|
14
14
|
origin = Globalize::Helpers.origin_table_name('ox_translations')
|
|
15
|
-
expect(origin).to eq(
|
|
15
|
+
expect(origin).to eq(:oxen)
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -11,6 +11,9 @@ describe Globalize::Reader, globalize_config: true do
|
|
|
11
11
|
it 'reads data from the given table and returns an array of resources' do
|
|
12
12
|
sprocket_id = widgets.connection.insert(name: 'sprocket')
|
|
13
13
|
flange_id = widgets.connection.insert(name: 'flange')
|
|
14
|
+
|
|
15
|
+
widget_translations.connection.insert(widget_id: sprocket_id, locale: 'en', name: 'sprocket')
|
|
16
|
+
widget_translations.connection.insert(widget_id: flange_id, locale: 'en', name: 'flange')
|
|
14
17
|
widget_translations.connection.insert(widget_id: sprocket_id, locale: 'es', name: 'sproqueta')
|
|
15
18
|
widget_translations.connection.insert(widget_id: flange_id, locale: 'es', name: 'flango')
|
|
16
19
|
|
|
@@ -27,9 +30,9 @@ describe Globalize::Reader, globalize_config: true do
|
|
|
27
30
|
|
|
28
31
|
expect(resource.content).to eq(
|
|
29
32
|
YAML.dump(
|
|
30
|
-
'
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
'widget_translations' => {
|
|
34
|
+
sprocket_id => { 'name' => 'sprocket' },
|
|
35
|
+
flange_id => { 'name' => 'flange' }
|
|
33
36
|
}
|
|
34
37
|
)
|
|
35
38
|
)
|
|
@@ -22,10 +22,15 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
22
22
|
context 'with some content' do
|
|
23
23
|
before(:each) do
|
|
24
24
|
@sprocket_id = widgets.connection.insert(name: 'sprocket')
|
|
25
|
+
|
|
26
|
+
sprocket_en_id = widget_translations.connection.insert(
|
|
27
|
+
widget_id: @sprocket_id, name: 'sprocket', locale: 'en'
|
|
28
|
+
)
|
|
29
|
+
|
|
25
30
|
@writer = Globalize::Writer.new(widget_translations)
|
|
26
31
|
|
|
27
32
|
@content = YAML.dump(
|
|
28
|
-
'
|
|
33
|
+
'widget_translations' => {
|
|
29
34
|
@sprocket_id => { name: 'sproqueta' }
|
|
30
35
|
}
|
|
31
36
|
)
|
|
@@ -35,10 +40,10 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
35
40
|
|
|
36
41
|
it 'inserts the translations into the database' do
|
|
37
42
|
expect { @writer.write_content(@resource, 'es') }.to(
|
|
38
|
-
change { widget_translations.connection.count }.from(
|
|
43
|
+
change { widget_translations.connection.count }.from(1).to(2)
|
|
39
44
|
)
|
|
40
45
|
|
|
41
|
-
translation = widget_translations.connection.
|
|
46
|
+
translation = widget_translations.connection.order(:id).last
|
|
42
47
|
|
|
43
48
|
expect(translation).to include(
|
|
44
49
|
widget_id: @sprocket_id, name: 'sproqueta', locale: 'es'
|
|
@@ -56,7 +61,7 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
56
61
|
it 'fills in the created_at and updated_at columns' do
|
|
57
62
|
Timecop.freeze(Time.now) do
|
|
58
63
|
@writer.write_content(@resource, 'es')
|
|
59
|
-
translation = widget_translations.connection.
|
|
64
|
+
translation = widget_translations.connection.order(:id).last
|
|
60
65
|
expect(translation[:created_at].to_i).to eq(Time.now.utc.to_i)
|
|
61
66
|
expect(translation[:updated_at].to_i).to eq(Time.now.utc.to_i)
|
|
62
67
|
end
|
|
@@ -73,7 +78,7 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
73
78
|
|
|
74
79
|
# record already exists, so should get updated with new timestamp
|
|
75
80
|
@writer.write_content(@resource, 'es')
|
|
76
|
-
translation = widget_translations.connection.
|
|
81
|
+
translation = widget_translations.connection.order(:id).last
|
|
77
82
|
expect(translation[:updated_at].to_i).to eq(today.utc.to_i)
|
|
78
83
|
end
|
|
79
84
|
end
|
|
@@ -82,14 +87,19 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
82
87
|
|
|
83
88
|
it 'updates the record if it already exists' do
|
|
84
89
|
sprocket_id = widgets.connection.insert(name: 'sprocket')
|
|
85
|
-
|
|
90
|
+
|
|
91
|
+
sprocket_en_id = widget_translations.connection.insert(
|
|
92
|
+
widget_id: sprocket_id, locale: 'en', name: 'sprocket'
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
sprocket_es_id = widget_translations.connection.insert(
|
|
86
96
|
widget_id: sprocket_id, locale: 'es', name: 'sproqueta'
|
|
87
97
|
)
|
|
88
98
|
|
|
89
99
|
writer = Globalize::Writer.new(widget_translations)
|
|
90
100
|
|
|
91
101
|
content = YAML.dump(
|
|
92
|
-
'
|
|
102
|
+
'widget_translations' => {
|
|
93
103
|
sprocket_id => { name: 'sproqueta2' }
|
|
94
104
|
}
|
|
95
105
|
)
|
|
@@ -100,7 +110,7 @@ describe Globalize::Writer, globalize_config: true do
|
|
|
100
110
|
change { widget_translations.connection.count }
|
|
101
111
|
)
|
|
102
112
|
|
|
103
|
-
translation = widget_translations.connection.where(id:
|
|
113
|
+
translation = widget_translations.connection.where(id: sprocket_es_id).first
|
|
104
114
|
expect(translation).to include(name: 'sproqueta2')
|
|
105
115
|
end
|
|
106
116
|
end
|
|
@@ -13,6 +13,7 @@ describe GlobalizeIterator, test_config: true do
|
|
|
13
13
|
|
|
14
14
|
create_table(:team_translations) do
|
|
15
15
|
primary_key :id
|
|
16
|
+
integer :team_id
|
|
16
17
|
string :name, translate: true
|
|
17
18
|
string :locale
|
|
18
19
|
source_lang 'en'
|
|
@@ -30,8 +31,10 @@ describe GlobalizeIterator, test_config: true do
|
|
|
30
31
|
|
|
31
32
|
it 'iterates over the items in the parent table' do
|
|
32
33
|
teams.each do |team|
|
|
33
|
-
teams_table.connection
|
|
34
|
-
team_translations_table.connection
|
|
34
|
+
team_id = teams_table.connection.insert(name: team)
|
|
35
|
+
team_translations_table.connection.insert(
|
|
36
|
+
name: team, team_id: team_id, locale: 'en'
|
|
37
|
+
)
|
|
35
38
|
end
|
|
36
39
|
|
|
37
40
|
entries = iterator.map { |entry| entry[:name] }
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: txdb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Cameron Dutro
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-07-
|
|
11
|
+
date: 2016-07-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|