globalize 5.1.0.beta1 → 5.1.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a08ebfb553ad5faf8e55d1b120bbafd8571327e2
4
- data.tar.gz: b30cca9abefcb65933dcbff2ace3040f40cbfea7
3
+ metadata.gz: 022f5a7b02ac3094cda16a9b96865dc8400fe8d3
4
+ data.tar.gz: cf25f8aba6cfe3d10b2ecab5da50f7cb4f9701fe
5
5
  SHA512:
6
- metadata.gz: ead976f8983d5477cce5d9e518f48997c7fdedb3abd057a79946fda0828943672069e286c3c9deff5d5ef53f8faebf24a3081f4e4bb323f9e9e5375fe95af5a8
7
- data.tar.gz: 7dd8d873d8492109cffa6561d6a57829f8b7d737f8125dd200a853ac11bcb6f8774c615b5cdd3c024223d81a6841c1c0d9d79481f6a77c11328d4ad363e9c4de
6
+ metadata.gz: 1a6e00bb94a8f1880a340e6d2172dbdb5fee942f99ee9cb09f02cfcc947ff7004945b5c714c10ab8bcf9993a6b3763407acb697a63bb59293e685402e941d22d
7
+ data.tar.gz: 0a56bb170ef716dc6c9cef4b98209407ec007786245b223be226dfd43637321c1064af4aaa7d9523d2ffb7c8e13b1032ee6f633251f2a50f60460d60354286b5
data/README.md CHANGED
@@ -239,6 +239,12 @@ Because globalize uses the `:locale` key to specify the locale during
239
239
  mass-assignment, you should avoid having a `locale` attribute on the parent
240
240
  model.
241
241
 
242
+ If you like your translated model to update if a translation changes, use the `touch: true` option together with `translates`:
243
+
244
+ ```ruby
245
+ translates :name, touch: true
246
+ ```
247
+
242
248
  ## Known Issues
243
249
 
244
250
  If you're getting the `ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "column_name" violates not-null constraint` error, the only known way to deal with it as of now is to remove not-null constraint for the globalized columns:
@@ -40,16 +40,19 @@ module Globalize
40
40
  self.translated_attribute_names << attr_name
41
41
  end
42
42
 
43
- if ::ActiveRecord::VERSION::STRING > "5.0" && table_exists? && translation_class.table_exists?
44
- self.ignored_columns += translated_attribute_names.map(&:to_s)
45
- reset_column_information
43
+ begin
44
+ if ::ActiveRecord::VERSION::STRING > "5.0" && table_exists? &&translation_class.table_exists?
45
+ self.ignored_columns += translated_attribute_names.map(&:to_s)
46
+ reset_column_information
47
+ end
48
+ rescue ::ActiveRecord::NoDatabaseError
49
+ warn 'Unable to connect to a database. Globalize skipped ignoring columns of translated attributes.'
46
50
  end
47
51
  end
48
52
 
49
53
  def check_columns!(attr_names)
50
- # If tables do not exist, do not warn about conflicting columns
51
- return unless table_exists? && translation_class.table_exists?
52
-
54
+ # If tables do not exist or Rails version is greater than 5, do not warn about conflicting columns
55
+ return unless ::ActiveRecord::VERSION::STRING < "5.0" && table_exists? && translation_class.table_exists?
53
56
  if (overlap = attr_names.map(&:to_s) & column_names).present?
54
57
  ActiveSupport::Deprecation.warn(
55
58
  ["You have defined one or more translated attributes with names that conflict with column(s) on the model table. ",
@@ -58,6 +61,8 @@ module Globalize
58
61
  "Attribute name(s): #{overlap.join(', ')}\n"].join
59
62
  )
60
63
  end
64
+ rescue ::ActiveRecord::NoDatabaseError
65
+ warn 'Unable to connect to a database. Globalize skipped checking attributes with conflicting column names.'
61
66
  end
62
67
 
63
68
  def apply_globalize_options(options)
@@ -56,7 +56,11 @@ module Globalize
56
56
  klass = self.const_set(:Translation, Class.new(Globalize::ActiveRecord::Translation))
57
57
  end
58
58
 
59
- klass.belongs_to :globalized_model, :class_name => self.name, :foreign_key => translation_options[:foreign_key], inverse_of: :translations
59
+ klass.belongs_to :globalized_model,
60
+ class_name: self.name,
61
+ foreign_key: translation_options[:foreign_key],
62
+ inverse_of: :translations,
63
+ touch: translation_options.fetch(:touch, false)
60
64
  klass
61
65
  end
62
66
  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 = {})
@@ -86,7 +86,7 @@ module Globalize
86
86
  # Inject `full_column` to the select values to avoid
87
87
  # PG::InvalidColumnReference errors with distinct queries on Postgres
88
88
  if select_values.empty?
89
- self.select_values = [Arel.star, full_column]
89
+ self.select_values = [self.arel_table[Arel.star], full_column]
90
90
  else
91
91
  self.select_values << full_column
92
92
  end
@@ -1,3 +1,3 @@
1
1
  module Globalize
2
- Version = '5.1.0.beta1'
2
+ Version = '5.1.0.beta2'
3
3
  end
@@ -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,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.1.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2017-03-10 00:00:00.000000000 Z
17
+ date: 2017-07-25 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: '5.2'
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: '5.2'
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: '5.2'
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: '5.2'
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: request_store
61
61
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +174,7 @@ files:
174
174
  - lib/patches/active_record/rails4/query_method.rb
175
175
  - lib/patches/active_record/rails4/uniqueness_validator.rb
176
176
  - lib/patches/active_record/rails5/uniqueness_validator.rb
177
+ - lib/patches/active_record/rails5_1/uniqueness_validator.rb
177
178
  - lib/patches/active_record/relation.rb
178
179
  - lib/patches/active_record/serialization.rb
179
180
  - lib/patches/active_record/uniqueness_validator.rb
@@ -198,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  version: 1.3.1
199
200
  requirements: []
200
201
  rubyforge_project: "[none]"
201
- rubygems_version: 2.6.8
202
+ rubygems_version: 2.6.11
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Rails I18n de-facto standard library for ActiveRecord model/data translation