from_clause_translate 0.1.0 → 0.1.1
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0ae09207f8f74865af8d0cef1824a3c2da59ec0
|
4
|
+
data.tar.gz: 69b959f76d9cd39ef6b53948e10ae823bb3e21ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb49f4190302bb4680bf8fcc7c210a0c9a48bdb13d4c9077609fc84739e5787c04fd8afd3e70213c29557f2e610467c53de62bff8ed20f27a21a7f09a2c5903f
|
7
|
+
data.tar.gz: d392a9299bad1574e6a343e5f992360ea15bc0e6ad677f3e88a6a5dadc64c3c1ff7fd2174f0ffec3ce001eaa9922a0046e48ba786a6dfd42695cecea04bba9e4
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module ActiveRecord::Relation::Extended
|
2
|
+
def to_sql
|
3
|
+
apply_translates unless eager_loading?
|
4
|
+
super
|
5
|
+
end
|
6
|
+
|
7
|
+
def exec_queries &block
|
8
|
+
apply_translates
|
9
|
+
super(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def pluck *columns
|
13
|
+
if !@translated_extra_columns && @klass.const_defined?(:TRANSLATED)
|
14
|
+
@translated_extra_columns = columns
|
15
|
+
end
|
16
|
+
super(*columns)
|
17
|
+
end
|
18
|
+
|
19
|
+
def arel aliases = nil
|
20
|
+
arel = super aliases
|
21
|
+
if @translated_extra_columns
|
22
|
+
add_translated_columns @translated_extra_columns
|
23
|
+
@translated_extra_columns = nil
|
24
|
+
apply_translates arel
|
25
|
+
end
|
26
|
+
arel
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_translated_columns columns
|
30
|
+
(@values[:translated] ||= []).concat columns
|
31
|
+
end
|
32
|
+
|
33
|
+
def apply_translates arel = nil
|
34
|
+
arel ||= self.arel
|
35
|
+
return if @translates_applied || !@klass.const_defined?(:TRANSLATED)
|
36
|
+
|
37
|
+
@translates_applied = true
|
38
|
+
columns = translated_columns_get
|
39
|
+
return unless columns
|
40
|
+
|
41
|
+
from = @klass.select(columns).arel.to_sql
|
42
|
+
as = @klass.table_name
|
43
|
+
arel.from("(#{from}) #{as}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def translated_columns_get without_projections = false
|
47
|
+
FromClauseTranslate::TranslatedColumnsBuilder.new(
|
48
|
+
self, without_projections
|
49
|
+
).perform
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def perform_calculation operation, column_name
|
55
|
+
if column_name && !@translated_extra_columns &&
|
56
|
+
@klass.const_defined?(:TRANSLATED)
|
57
|
+
@translated_extra_columns = [column_name]
|
58
|
+
end
|
59
|
+
super operation, column_name
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class ActiveRecord::Relation
|
64
|
+
prepend ActiveRecord::Relation::Extended
|
65
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class FromClauseTranslate::TranslatedColumnsBuilder
|
2
|
+
def initialize relation, without_projections
|
3
|
+
@relation = relation
|
4
|
+
@klass = relation.instance_variable_get :@klass
|
5
|
+
@arel = relation.arel
|
6
|
+
@without_projections = without_projections
|
7
|
+
@columns = []
|
8
|
+
@has_translated = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform
|
12
|
+
add_translated_columns_from_values
|
13
|
+
add_translated_columns_from_projections
|
14
|
+
add_translated_columns_from_cores
|
15
|
+
add_translated_colunms_from_orders
|
16
|
+
|
17
|
+
@columns if @has_translated
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def add_translated_columns_from_values
|
23
|
+
translated = @relation.instance_variable_get(:@values)[:translated]
|
24
|
+
return unless translated
|
25
|
+
|
26
|
+
translated.each do |column|
|
27
|
+
if column.is_a? Hash
|
28
|
+
@has_translated = true
|
29
|
+
column.each do |key, value|
|
30
|
+
@columns << "#{value} AS #{key}"
|
31
|
+
end
|
32
|
+
elsif (selection = translated_selection(column))
|
33
|
+
@columns << selection
|
34
|
+
else
|
35
|
+
@columns << column
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_translated_columns_from_projections
|
41
|
+
return if @without_projections
|
42
|
+
|
43
|
+
@arel.projections.each do |column|
|
44
|
+
selection = arel_projection_translation(column)
|
45
|
+
@columns << selection if selection
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def arel_projection_translation column
|
50
|
+
return column.name if column.is_a? Arel::Attributes::Attribute
|
51
|
+
|
52
|
+
return unless column.is_a? String
|
53
|
+
|
54
|
+
name = column.first == '"' ? column[1...-1] : column
|
55
|
+
selection = translated_selection name
|
56
|
+
return column unless selection
|
57
|
+
|
58
|
+
plural = @klass::TRANSLATED_PLURALS[name.to_sym]
|
59
|
+
column.replace plural if plural
|
60
|
+
selection
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_translated_columns_from_cores
|
64
|
+
@arel.ast.cores.each do |core|
|
65
|
+
add_translated_columns_from_groups core
|
66
|
+
add_translated_columns_from_nodes core.wheres
|
67
|
+
add_translated_columns_from_nodes core.havings
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_translated_columns_from_groups core
|
72
|
+
core.groups.each do |group|
|
73
|
+
expr = group.expr
|
74
|
+
name = expr[0] == '"' ? expr[1...-1] : expr
|
75
|
+
selection = translated_selection name
|
76
|
+
@columns << selection if selection
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def add_translated_columns_from_nodes nodes
|
81
|
+
nodes.each do |node|
|
82
|
+
node.children.each do |child|
|
83
|
+
next unless child.respond_to? :left
|
84
|
+
|
85
|
+
name = child.left.name
|
86
|
+
selection = translated_selection name
|
87
|
+
@columns << selection if selection
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def add_translated_colunms_from_orders
|
93
|
+
@arel.ast.orders.each do |name|
|
94
|
+
name = name.respond_to?(:expr) ? name.expr.name : name.to_sym
|
95
|
+
selection = translated_selection name
|
96
|
+
@columns << selection if selection
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def translated_selection name
|
101
|
+
translates = @klass.translates? name
|
102
|
+
@has_translated = true if translates
|
103
|
+
translates ? @klass::TRANSLATED_SELECTION[I18n.locale][name.to_sym] : false
|
104
|
+
end
|
105
|
+
end
|
@@ -1,148 +1,4 @@
|
|
1
|
-
|
2
|
-
def to_sql
|
3
|
-
apply_translates unless eager_loading?
|
4
|
-
super
|
5
|
-
end
|
6
|
-
|
7
|
-
def exec_queries &block
|
8
|
-
apply_translates
|
9
|
-
super(&block)
|
10
|
-
end
|
11
|
-
|
12
|
-
def pluck *columns
|
13
|
-
if !@translated_extra_columns && @klass.const_defined?(:TRANSLATED)
|
14
|
-
@translated_extra_columns = columns
|
15
|
-
end
|
16
|
-
super(*columns)
|
17
|
-
end
|
18
|
-
|
19
|
-
def arel aliases = nil
|
20
|
-
arel = super aliases
|
21
|
-
if @translated_extra_columns
|
22
|
-
add_translated_columns @translated_extra_columns
|
23
|
-
@translated_extra_columns = nil
|
24
|
-
apply_translates arel
|
25
|
-
end
|
26
|
-
arel
|
27
|
-
end
|
28
|
-
|
29
|
-
def add_translated_columns columns
|
30
|
-
(@values[:translated] ||= []).concat columns
|
31
|
-
end
|
32
|
-
|
33
|
-
def apply_translates arel = nil
|
34
|
-
arel ||= self.arel
|
35
|
-
return if @translates_applied || !@klass.const_defined?(:TRANSLATED)
|
36
|
-
@translates_applied = true
|
37
|
-
columns = translated_columns_get
|
38
|
-
return unless columns
|
39
|
-
from = @klass.select(columns).arel.to_sql
|
40
|
-
as = @klass.table_name
|
41
|
-
arel.from("(#{from}) #{as}")
|
42
|
-
end
|
43
|
-
|
44
|
-
def translated_columns_get without_projections = false
|
45
|
-
columns = []
|
46
|
-
has_translated = false
|
47
|
-
|
48
|
-
translated = @values[:translated]
|
49
|
-
if translated
|
50
|
-
translated.each do |column|
|
51
|
-
if column.is_a? Hash
|
52
|
-
has_translated = true
|
53
|
-
column.each do |key, value|
|
54
|
-
columns << "#{value} AS #{key}"
|
55
|
-
end
|
56
|
-
elsif @klass.translates? column
|
57
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][column.to_sym]
|
58
|
-
has_translated = true
|
59
|
-
else
|
60
|
-
columns << column
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
unless without_projections
|
66
|
-
arel.projections.each do |column|
|
67
|
-
if column.is_a? Arel::Attributes::Attribute
|
68
|
-
columns << column.name
|
69
|
-
elsif column.is_a? String
|
70
|
-
name = column.first == '"' ? column[1...-1] : column
|
71
|
-
if @klass.translates? name
|
72
|
-
has_translated = true
|
73
|
-
sym = name.to_sym
|
74
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][sym]
|
75
|
-
plural = @klass::TRANSLATED_PLURALS[sym]
|
76
|
-
if plural
|
77
|
-
column.replace plural
|
78
|
-
end
|
79
|
-
else
|
80
|
-
columns << column
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
arel.ast.cores.each do |core|
|
87
|
-
core.wheres.each do |where|
|
88
|
-
where.children.each do |child|
|
89
|
-
if child.respond_to? :left
|
90
|
-
name = child.left.name
|
91
|
-
if @klass.translates? name
|
92
|
-
has_translated = true
|
93
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][name.to_sym]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
core.groups.each do |group|
|
100
|
-
expr = group.expr
|
101
|
-
name = expr[0] == '"' ? expr[1...-1] : expr
|
102
|
-
if @klass.translates? name
|
103
|
-
has_translated = true
|
104
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][name.to_sym]
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
core.havings.each do |having|
|
109
|
-
having.children.each do |child|
|
110
|
-
if child.respond_to? :left
|
111
|
-
name = child.left.name
|
112
|
-
if @klass.translates? name
|
113
|
-
has_translated = true
|
114
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][name.to_sym]
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
arel.ast.orders.each do |name|
|
122
|
-
name = name.respond_to?(:expr) ? name.expr.name : name.to_sym
|
123
|
-
if @klass.translates? name
|
124
|
-
has_translated = true
|
125
|
-
columns << @klass::TRANSLATED_SELECTION[I18n.locale][name.to_sym]
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
columns if has_translated
|
130
|
-
end
|
131
|
-
|
132
|
-
private
|
133
|
-
|
134
|
-
def perform_calculation operation, column_name
|
135
|
-
if column_name && !@translated_extra_columns &&
|
136
|
-
@klass.const_defined?(:TRANSLATED)
|
137
|
-
@translated_extra_columns = [column_name]
|
138
|
-
end
|
139
|
-
super operation, column_name
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
class ActiveRecord::Relation
|
144
|
-
prepend ActiveRecord::Relation::Extended
|
145
|
-
end
|
1
|
+
require 'from_clause_translate/active_record_relation'
|
146
2
|
|
147
3
|
module FromClauseTranslate
|
148
4
|
def self.included model
|
@@ -166,19 +22,22 @@ module FromClauseTranslate
|
|
166
22
|
end
|
167
23
|
define_translated_column_methods column
|
168
24
|
end
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
25
|
+
translates_plurals options[:plurals]
|
26
|
+
end
|
27
|
+
|
28
|
+
def translates_plurals plurals
|
29
|
+
return unless plurals
|
30
|
+
|
31
|
+
plurals.each do |plural|
|
32
|
+
plural = plural.to_sym
|
33
|
+
self::TRANSLATED[plural] = true
|
34
|
+
column = plural.to_s.singularize.to_sym
|
35
|
+
selection = I18n.available_locales.map do |locale|
|
36
|
+
"#{column}_#{locale}"
|
37
|
+
end.join ','
|
38
|
+
self::TRANSLATED_PLURALS[plural] = selection
|
39
|
+
I18n.available_locales.each do |locale|
|
40
|
+
self::TRANSLATED_SELECTION[locale][plural] = selection
|
182
41
|
end
|
183
42
|
end
|
184
43
|
end
|
@@ -202,48 +61,38 @@ module FromClauseTranslate
|
|
202
61
|
end
|
203
62
|
end
|
204
63
|
|
205
|
-
def define_translated_column_methods
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
self[column]
|
212
|
-
else
|
213
|
-
send(with_dash + I18n.locale.to_s)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
define_method column + '=' do |value|
|
218
|
-
send "#{with_dash}#{I18n.locale}=", value
|
219
|
-
end
|
220
|
-
|
221
|
-
define_method column + '_changed?' do
|
222
|
-
send "#{with_dash}#{I18n.locale}_changed?"
|
223
|
-
end
|
224
|
-
|
225
|
-
define_method 'saved_change_to_' + column do
|
226
|
-
send "saved_change_to_#{with_dash}#{I18n.locale}"
|
227
|
-
end
|
64
|
+
def define_translated_column_methods col
|
65
|
+
col = col.to_s
|
66
|
+
dashed = "#{col}_"
|
67
|
+
define_translated_column_getter_and_setter col, dashed
|
68
|
+
define_translated_column_changing_methods col, dashed
|
69
|
+
end
|
228
70
|
|
229
|
-
|
230
|
-
|
71
|
+
def define_translated_column_getter_and_setter col, dashed
|
72
|
+
define_method col do
|
73
|
+
@attributes.key?(col) ? self[col] : send("#{dashed}#{I18n.locale}")
|
231
74
|
end
|
232
75
|
|
233
|
-
define_method
|
234
|
-
send "#{
|
76
|
+
define_method col + '=' do |val|
|
77
|
+
send "#{dashed}#{I18n.locale}=", val
|
235
78
|
end
|
79
|
+
end
|
236
80
|
|
237
|
-
|
238
|
-
|
81
|
+
def define_translated_column_changing_methods col, dashed
|
82
|
+
define_method 'saved_change_to_' + col do
|
83
|
+
send "saved_change_to_#{dashed}#{I18n.locale}"
|
239
84
|
end
|
240
85
|
|
241
|
-
|
242
|
-
|
86
|
+
%w[changed? before_last_save change_to_be_saved in_database].each do |suf|
|
87
|
+
define_method dashed + suf do
|
88
|
+
send "#{dashed}#{I18n.locale}_#{suf}"
|
89
|
+
end
|
243
90
|
end
|
244
91
|
|
245
|
-
|
246
|
-
|
92
|
+
%w[saved_change_to will_save_change_to].each do |prefix|
|
93
|
+
define_method "#{prefix}_#{col}?" do
|
94
|
+
send "#{prefix}_#{dashed}#{I18n.locale}?"
|
95
|
+
end
|
247
96
|
end
|
248
97
|
end
|
249
98
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: from_clause_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Kushin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -34,6 +34,8 @@ files:
|
|
34
34
|
- MIT-LICENSE
|
35
35
|
- README.md
|
36
36
|
- lib/from_clause_translate.rb
|
37
|
+
- lib/from_clause_translate/active_record_relation.rb
|
38
|
+
- lib/from_clause_translate/translated_columns_builder.rb
|
37
39
|
- lib/from_clause_translate/version.rb
|
38
40
|
homepage:
|
39
41
|
licenses:
|