globalize 5.1.0.beta1 → 5.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,10 +17,24 @@ module Globalize
17
17
  with_given_locale(attributes) { super(attributes.except("locale"), *options) }
18
18
  end
19
19
 
20
- def assign_attributes(new_attributes, *options)
21
- super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present?
22
- attributes = new_attributes.stringify_keys
23
- with_given_locale(attributes) { super(attributes.except("locale"), *options) }
20
+ if Globalize.rails_52?
21
+
22
+ # In Rails 5.2 we need to override *_assign_attributes* as it's called earlier
23
+ # in the stack (before *assign_attributes*)
24
+ # See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L11
25
+ def _assign_attributes(new_attributes)
26
+ attributes = new_attributes.stringify_keys
27
+ with_given_locale(attributes) { super(attributes.except("locale")) }
28
+ end
29
+
30
+ else
31
+
32
+ def assign_attributes(new_attributes, *options)
33
+ super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present?
34
+ attributes = new_attributes.stringify_keys
35
+ with_given_locale(attributes) { super(attributes.except("locale"), *options) }
36
+ end
37
+
24
38
  end
25
39
 
26
40
  def write_attribute(name, value, *args, &block)
@@ -39,19 +53,21 @@ module Globalize
39
53
  end
40
54
  end
41
55
 
42
- def read_attribute(name, options = {}, &block)
43
- options = {:translated => true, :locale => nil}.merge(options)
44
- return super(name, &block) unless options[:translated]
56
+ def read_attribute(attr_name, options = {}, &block)
57
+ name = if self.class.attribute_alias?(attr_name)
58
+ self.class.attribute_alias(attr_name).to_s
59
+ else
60
+ attr_name.to_s
61
+ end
45
62
 
46
- if translated?(name)
47
- if !(value = globalize.fetch(options[:locale] || Globalize.locale, name)).nil?
48
- value
49
- else
50
- super(name, &block)
51
- end
52
- else
53
- super(name, &block)
54
- end
63
+ name = self.class.primary_key if name == "id".freeze && self.class.primary_key
64
+
65
+ _read_attribute(name, options, &block)
66
+ end
67
+
68
+ def _read_attribute(attr_name, options = {}, &block)
69
+ translated_value = read_translated_attribute(attr_name, options)
70
+ translated_value.nil? ? super(attr_name, &block) : translated_value
55
71
  end
56
72
 
57
73
  def attribute_names
@@ -213,6 +229,15 @@ module Globalize
213
229
  ensure
214
230
  self.fallbacks_for_empty_translations = before
215
231
  end
232
+
233
+ # nil or value
234
+ def read_translated_attribute(name, options)
235
+ options = {:translated => true, :locale => nil}.merge(options)
236
+ return nil unless options[:translated]
237
+ return nil unless translated?(name)
238
+
239
+ globalize.fetch(options[:locale] || Globalize.locale, name)
240
+ end
216
241
  end
217
242
  end
218
243
  end
@@ -53,7 +53,12 @@ module Globalize
53
53
  end
54
54
 
55
55
  def remove_source_columns
56
- connection.remove_columns(table_name, *fields.keys)
56
+ column_names = *fields.keys
57
+ column_names.each do |column|
58
+ if connection.column_exists?(table_name, column)
59
+ connection.remove_column(table_name, column)
60
+ end
61
+ end
57
62
  end
58
63
 
59
64
  def drop_translation_table!(options = {})
@@ -73,7 +78,11 @@ module Globalize
73
78
 
74
79
  def create_translation_table
75
80
  connection.create_table(translations_table_name) do |t|
76
- t.references table_name.sub(/^#{table_name_prefix}/, '').singularize, :null => false, :index => false, :type => column_type(model.primary_key).to_sym
81
+ t.references table_name.sub(/^#{table_name_prefix}/, '').singularize,
82
+ :null => false,
83
+ :index => false,
84
+ :type => column_type(model.primary_key).try(:to_sym),
85
+ :limit => model.columns.detect { |c| c.name == model.primary_key }.try(:limit)
77
86
  t.string :locale, :null => false
78
87
  t.timestamps :null => false
79
88
  end
@@ -161,7 +170,7 @@ module Globalize
161
170
  end
162
171
 
163
172
  def column_type(name)
164
- columns.detect { |c| c.name == name.to_s }.try(:type)
173
+ columns.detect { |c| c.name == name.to_s }.try(:type) || :string
165
174
  end
166
175
 
167
176
  def valid_field_name?(name)
@@ -1,6 +1,6 @@
1
1
  module Globalize
2
2
  module ActiveRecord
3
- module QueryMethods
3
+ module TranslatedAttributesQuery
4
4
  class WhereChain < ::ActiveRecord::QueryMethods::WhereChain
5
5
  def not(opts, *rest)
6
6
  if parsed = @scope.clone.parse_translated_conditions(opts)
@@ -21,6 +21,14 @@ module Globalize
21
21
  end
22
22
  end
23
23
 
24
+ def having(opts, *rest)
25
+ if parsed = parse_translated_conditions(opts)
26
+ join_translations(super(parsed, *rest))
27
+ else
28
+ super
29
+ end
30
+ end
31
+
24
32
  def order(opts, *rest)
25
33
  if respond_to?(:translated_attribute_names) && parsed = parse_translated_order(opts)
26
34
  join_translations super(parsed)
@@ -29,6 +37,30 @@ module Globalize
29
37
  end
30
38
  end
31
39
 
40
+ def reorder(opts, *rest)
41
+ if respond_to?(:translated_attribute_names) && parsed = parse_translated_order(opts)
42
+ join_translations super(parsed)
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ def group(*columns)
49
+ if respond_to?(:translated_attribute_names) && parsed = parse_translated_columns(columns)
50
+ join_translations super(parsed)
51
+ else
52
+ super
53
+ end
54
+ end
55
+
56
+ def select(*columns)
57
+ if respond_to?(:translated_attribute_names) && parsed = parse_translated_columns(columns)
58
+ join_translations super(parsed)
59
+ else
60
+ super
61
+ end
62
+ end
63
+
32
64
  def exists?(conditions = :none)
33
65
  if parsed = parse_translated_conditions(conditions)
34
66
  with_translations_in_fallbacks.exists?(parsed)
@@ -37,6 +69,24 @@ module Globalize
37
69
  end
38
70
  end
39
71
 
72
+ def calculate(*args)
73
+ column_name = args[1]
74
+ if respond_to?(:translated_attribute_names) && translated_column?(column_name)
75
+ args[1] = translated_column_name(column_name)
76
+ join_translations.calculate(*args)
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ def pluck(*column_names)
83
+ if respond_to?(:translated_attribute_names) && parsed = parse_translated_columns(column_names)
84
+ join_translations.pluck(*parsed)
85
+ else
86
+ super
87
+ end
88
+ end
89
+
40
90
  def with_translations_in_fallbacks
41
91
  with_translations(Globalize.fallbacks)
42
92
  end
@@ -86,7 +136,7 @@ module Globalize
86
136
  # Inject `full_column` to the select values to avoid
87
137
  # PG::InvalidColumnReference errors with distinct queries on Postgres
88
138
  if select_values.empty?
89
- self.select_values = [Arel.star, full_column]
139
+ self.select_values = [self.arel_table[Arel.star], full_column]
90
140
  else
91
141
  self.select_values << full_column
92
142
  end
@@ -110,11 +160,19 @@ module Globalize
110
160
  order(ordering).order_values
111
161
  when Symbol
112
162
  parse_translated_order({ opts => :asc })
163
+ when Array
164
+ parse_translated_order(Hash[opts.collect { |opt| [opt, :asc] } ])
113
165
  else # failsafe returns nothing
114
166
  nil
115
167
  end
116
168
  end
117
169
 
170
+ def parse_translated_columns(columns)
171
+ if columns.is_a?(Array) && (columns.flatten & translated_attribute_names).present?
172
+ columns.flatten.map { |column| translated_column?(column) ? translated_column_name(column) : column }
173
+ end
174
+ end
175
+
118
176
  def translated_column?(column)
119
177
  translated_attribute_names.include?(column)
120
178
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Globalize
2
- Version = '5.1.0.beta1'
4
+ Version = '5.3.1'
3
5
  end
@@ -0,0 +1,22 @@
1
+ module Globalize
2
+ module AttributeMethods
3
+ module Serialization
4
+ def serialize(attr_name, class_name_or_coder = Object)
5
+ super(attr_name, class_name_or_coder)
6
+
7
+ coder = if class_name_or_coder == ::JSON
8
+ ::ActiveRecord::Coders::JSON
9
+ elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
10
+ class_name_or_coder
11
+ else
12
+ ::ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
13
+ end
14
+
15
+ self.globalize_serialized_attributes = globalize_serialized_attributes.dup
16
+ self.globalize_serialized_attributes[attr_name] = coder
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::AttributeMethods::Serialization::ClassMethods.send(:prepend, Globalize::AttributeMethods::Serialization)
@@ -0,0 +1,22 @@
1
+ module Globalize
2
+ module AttributeMethods
3
+ module Serialization
4
+ def serialize(attr_name, class_name_or_coder = Object)
5
+ super(attr_name, class_name_or_coder)
6
+
7
+ coder = if class_name_or_coder == ::JSON
8
+ ::ActiveRecord::Coders::JSON
9
+ elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
10
+ class_name_or_coder
11
+ else
12
+ ::ActiveRecord::Coders::YAMLColumn.new(attr_name, class_name_or_coder)
13
+ end
14
+
15
+ self.globalize_serialized_attributes = globalize_serialized_attributes.dup
16
+ self.globalize_serialized_attributes[attr_name] = coder
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::AttributeMethods::Serialization::ClassMethods.send(:prepend, Globalize::AttributeMethods::Serialization)
@@ -0,0 +1,45 @@
1
+ module Globalize
2
+ module Validations
3
+ module UniquenessValidator
4
+ def validate_each(record, attribute, value)
5
+ klass = record.class
6
+ if klass.translates? && klass.translated?(attribute)
7
+ finder_class = klass.translation_class
8
+ relation = build_relation(finder_class, attribute, value).where(locale: Globalize.locale)
9
+ relation = relation.where.not(klass.reflect_on_association(:translations).foreign_key => record.send(:id)) if record.persisted?
10
+
11
+
12
+ translated_scopes = Array(options[:scope]) & klass.translated_attribute_names
13
+ untranslated_scopes = Array(options[:scope]) - translated_scopes
14
+
15
+ relation = relation.joins(:globalized_model) if untranslated_scopes.present?
16
+ untranslated_scopes.each do |scope_item|
17
+ scope_value = record.send(scope_item)
18
+ reflection = klass.reflect_on_association(scope_item)
19
+ if reflection
20
+ scope_value = record.send(reflection.foreign_key)
21
+ scope_item = reflection.foreign_key
22
+ end
23
+ relation = relation.where(find_finder_class_for(record).table_name => { scope_item => scope_value })
24
+ end
25
+
26
+ translated_scopes.each do |scope_item|
27
+ scope_value = record.send(scope_item)
28
+ relation = relation.where(scope_item => scope_value)
29
+ end
30
+ relation = relation.merge(options[:conditions]) if options[:conditions]
31
+
32
+ if relation.exists?
33
+ error_options = options.except(:case_sensitive, :scope, :conditions)
34
+ error_options[:value] = value
35
+ record.errors.add(attribute, :taken, error_options)
36
+ end
37
+ else
38
+ super(record, attribute, value)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ ActiveRecord::Validations::UniquenessValidator.prepend Globalize::Validations::UniquenessValidator
@@ -1,21 +1,5 @@
1
- module Globalize
2
- module AttributeMethods
3
- module Serialization
4
- def serialize(attr_name, class_name_or_coder = Object)
5
- super(attr_name, class_name_or_coder)
6
-
7
- coder = if class_name_or_coder == ::JSON
8
- ::ActiveRecord::Coders::JSON
9
- elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
10
- class_name_or_coder
11
- else
12
- ::ActiveRecord::Coders::YAMLColumn.new(class_name_or_coder)
13
- end
14
-
15
- self.globalize_serialized_attributes[attr_name] = coder
16
- end
17
- end
18
- end
19
- end
20
-
21
- ActiveRecord::AttributeMethods::Serialization::ClassMethods.send(:prepend, Globalize::AttributeMethods::Serialization)
1
+ if ::ActiveRecord::VERSION::STRING < "5.1.0"
2
+ require_relative 'rails4/serialization'
3
+ else
4
+ require_relative 'rails5_1/serialization'
5
+ end
@@ -1,5 +1,7 @@
1
1
  if ::ActiveRecord::VERSION::STRING < "5.0.0"
2
2
  require_relative 'rails4/uniqueness_validator'
3
- else
3
+ elsif ::ActiveRecord::VERSION::STRING < "5.1.0"
4
4
  require_relative 'rails5/uniqueness_validator'
5
+ else
6
+ require_relative 'rails5_1/uniqueness_validator'
5
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.0.beta1
4
+ version: 5.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -11,10 +11,10 @@ authors:
11
11
  - Tomasz Stachewicz
12
12
  - Philip Arndt
13
13
  - Chris Salzberg
14
- autorequire:
14
+ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2017-03-10 00:00:00.000000000 Z
17
+ date: 2021-01-11 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -25,7 +25,7 @@ dependencies:
25
25
  version: '4.2'
26
26
  - - "<"
27
27
  - !ruby/object:Gem::Version
28
- version: '5.1'
28
+ version: '6.1'
29
29
  type: :runtime
30
30
  prerelease: false
31
31
  version_requirements: !ruby/object:Gem::Requirement
@@ -35,7 +35,7 @@ dependencies:
35
35
  version: '4.2'
36
36
  - - "<"
37
37
  - !ruby/object:Gem::Version
38
- version: '5.1'
38
+ version: '6.1'
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: activemodel
41
41
  requirement: !ruby/object:Gem::Requirement
@@ -45,7 +45,7 @@ dependencies:
45
45
  version: '4.2'
46
46
  - - "<"
47
47
  - !ruby/object:Gem::Version
48
- version: '5.1'
48
+ version: '6.1'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '4.2'
56
56
  - - "<"
57
57
  - !ruby/object:Gem::Version
58
- version: '5.1'
58
+ version: '6.1'
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: request_store
61
61
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +70,20 @@ dependencies:
70
70
  - - "~>"
71
71
  - !ruby/object:Gem::Version
72
72
  version: '1.0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: appraisal
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
73
87
  - !ruby/object:Gem::Dependency
74
88
  name: database_cleaner
75
89
  requirement: !ruby/object:Gem::Requirement
@@ -84,6 +98,20 @@ dependencies:
84
98
  - - ">="
85
99
  - !ruby/object:Gem::Version
86
100
  version: '0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: m
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
87
115
  - !ruby/object:Gem::Dependency
88
116
  name: minitest
89
117
  requirement: !ruby/object:Gem::Requirement
@@ -113,7 +141,7 @@ dependencies:
113
141
  - !ruby/object:Gem::Version
114
142
  version: '0'
115
143
  - !ruby/object:Gem::Dependency
116
- name: rdoc
144
+ name: pry
117
145
  requirement: !ruby/object:Gem::Requirement
118
146
  requirements:
119
147
  - - ">="
@@ -140,19 +168,50 @@ dependencies:
140
168
  - - ">="
141
169
  - !ruby/object:Gem::Version
142
170
  version: '0'
171
+ - !ruby/object:Gem::Dependency
172
+ name: rdoc
173
+ requirement: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ type: :development
179
+ prerelease: false
180
+ version_requirements: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ - !ruby/object:Gem::Dependency
186
+ name: sqlite3
187
+ requirement: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ type: :development
193
+ prerelease: false
194
+ version_requirements: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
143
199
  description: Rails I18n de-facto standard library for ActiveRecord model/data translation.
144
200
  email: nobody@globalize-rails.org
145
201
  executables: []
146
202
  extensions: []
147
203
  extra_rdoc_files: []
148
204
  files:
205
+ - Appraisals
149
206
  - CHANGELOG.md
150
207
  - CONTRIBUTING.md
151
208
  - Gemfile
152
- - Gemfile.lock
153
209
  - LICENSE
154
210
  - README.md
155
211
  - Rakefile
212
+ - docker-compose.yml
213
+ - globalize.gemspec
214
+ - issue_template.rb
156
215
  - lib/globalize.rb
157
216
  - lib/globalize/active_record.rb
158
217
  - lib/globalize/active_record/act_macro.rb
@@ -163,7 +222,7 @@ files:
163
222
  - lib/globalize/active_record/exceptions.rb
164
223
  - lib/globalize/active_record/instance_methods.rb
165
224
  - lib/globalize/active_record/migration.rb
166
- - lib/globalize/active_record/query_methods.rb
225
+ - lib/globalize/active_record/translated_attributes_query.rb
167
226
  - lib/globalize/active_record/translation.rb
168
227
  - lib/globalize/interpolation.rb
169
228
  - lib/globalize/version.rb
@@ -172,8 +231,11 @@ files:
172
231
  - lib/patches/active_record/persistence.rb
173
232
  - lib/patches/active_record/query_method.rb
174
233
  - lib/patches/active_record/rails4/query_method.rb
234
+ - lib/patches/active_record/rails4/serialization.rb
175
235
  - lib/patches/active_record/rails4/uniqueness_validator.rb
176
236
  - lib/patches/active_record/rails5/uniqueness_validator.rb
237
+ - lib/patches/active_record/rails5_1/serialization.rb
238
+ - lib/patches/active_record/rails5_1/uniqueness_validator.rb
177
239
  - lib/patches/active_record/relation.rb
178
240
  - lib/patches/active_record/serialization.rb
179
241
  - lib/patches/active_record/uniqueness_validator.rb
@@ -182,7 +244,7 @@ homepage: http://github.com/globalize/globalize
182
244
  licenses:
183
245
  - MIT
184
246
  metadata: {}
185
- post_install_message:
247
+ post_install_message:
186
248
  rdoc_options: []
187
249
  require_paths:
188
250
  - lib
@@ -190,16 +252,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
252
  requirements:
191
253
  - - ">="
192
254
  - !ruby/object:Gem::Version
193
- version: 2.0.0
255
+ version: 2.4.6
194
256
  required_rubygems_version: !ruby/object:Gem::Requirement
195
257
  requirements:
196
- - - ">"
258
+ - - ">="
197
259
  - !ruby/object:Gem::Version
198
- version: 1.3.1
260
+ version: '0'
199
261
  requirements: []
200
- rubyforge_project: "[none]"
201
- rubygems_version: 2.6.8
202
- signing_key:
262
+ rubygems_version: 3.2.3
263
+ signing_key:
203
264
  specification_version: 4
204
265
  summary: Rails I18n de-facto standard library for ActiveRecord model/data translation
205
266
  test_files: []