mobility 0.5.1 → 0.6.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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +2 -2
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +41 -1
  5. data/Gemfile.lock +3 -58
  6. data/README.md +22 -21
  7. data/lib/mobility.rb +3 -2
  8. data/lib/mobility/accumulator.rb +1 -2
  9. data/lib/mobility/active_model/backend_resetter.rb +1 -1
  10. data/lib/mobility/active_record.rb +12 -9
  11. data/lib/mobility/active_record/backend_resetter.rb +6 -7
  12. data/lib/mobility/active_record/uniqueness_validator.rb +12 -2
  13. data/lib/mobility/adapter.rb +1 -0
  14. data/lib/mobility/attributes.rb +3 -13
  15. data/lib/mobility/backends/active_record/column.rb +1 -0
  16. data/lib/mobility/backends/active_record/column/query_methods.rb +25 -20
  17. data/lib/mobility/backends/active_record/container/json_query_methods.rb +22 -16
  18. data/lib/mobility/backends/active_record/container/jsonb_query_methods.rb +19 -19
  19. data/lib/mobility/backends/active_record/hstore.rb +14 -12
  20. data/lib/mobility/backends/active_record/hstore/query_methods.rb +14 -5
  21. data/lib/mobility/backends/active_record/json.rb +21 -19
  22. data/lib/mobility/backends/active_record/json/query_methods.rb +16 -11
  23. data/lib/mobility/backends/active_record/jsonb.rb +21 -19
  24. data/lib/mobility/backends/active_record/jsonb/query_methods.rb +14 -5
  25. data/lib/mobility/backends/active_record/key_value.rb +9 -9
  26. data/lib/mobility/backends/active_record/key_value/query_methods.rb +53 -46
  27. data/lib/mobility/backends/active_record/pg_hash.rb +29 -25
  28. data/lib/mobility/backends/active_record/pg_query_methods.rb +76 -40
  29. data/lib/mobility/backends/active_record/query_methods.rb +17 -10
  30. data/lib/mobility/backends/active_record/serialized.rb +4 -2
  31. data/lib/mobility/backends/active_record/serialized/query_methods.rb +18 -15
  32. data/lib/mobility/backends/active_record/table.rb +21 -12
  33. data/lib/mobility/backends/active_record/table/query_methods.rb +82 -83
  34. data/lib/mobility/backends/hash_valued.rb +19 -0
  35. data/lib/mobility/backends/hstore.rb +3 -1
  36. data/lib/mobility/backends/json.rb +3 -1
  37. data/lib/mobility/backends/jsonb.rb +3 -1
  38. data/lib/mobility/backends/key_value.rb +32 -15
  39. data/lib/mobility/backends/sequel/column/query_methods.rb +16 -12
  40. data/lib/mobility/backends/sequel/container/json_query_methods.rb +25 -18
  41. data/lib/mobility/backends/sequel/container/jsonb_query_methods.rb +25 -18
  42. data/lib/mobility/backends/sequel/hstore.rb +14 -12
  43. data/lib/mobility/backends/sequel/hstore/query_methods.rb +18 -11
  44. data/lib/mobility/backends/sequel/json.rb +21 -19
  45. data/lib/mobility/backends/sequel/json/query_methods.rb +18 -11
  46. data/lib/mobility/backends/sequel/jsonb.rb +21 -19
  47. data/lib/mobility/backends/sequel/jsonb/query_methods.rb +18 -11
  48. data/lib/mobility/backends/sequel/key_value.rb +10 -11
  49. data/lib/mobility/backends/sequel/key_value/query_methods.rb +39 -34
  50. data/lib/mobility/backends/sequel/pg_hash.rb +37 -25
  51. data/lib/mobility/backends/sequel/pg_query_methods.rb +45 -20
  52. data/lib/mobility/backends/sequel/query_methods.rb +5 -0
  53. data/lib/mobility/backends/sequel/serialized.rb +18 -13
  54. data/lib/mobility/backends/sequel/serialized/query_methods.rb +10 -7
  55. data/lib/mobility/backends/sequel/table.rb +1 -1
  56. data/lib/mobility/backends/sequel/table/query_methods.rb +40 -35
  57. data/lib/mobility/plugins/cache/translation_cacher.rb +15 -15
  58. data/lib/mobility/plugins/default.rb +0 -7
  59. data/lib/mobility/plugins/fallbacks.rb +4 -0
  60. data/lib/mobility/sequel.rb +11 -5
  61. data/lib/mobility/sequel/backend_resetter.rb +6 -7
  62. data/lib/mobility/sequel/column_changes.rb +4 -4
  63. data/lib/mobility/version.rb +1 -1
  64. data/lib/rails/generators/mobility/backend_generators/base.rb +4 -0
  65. data/lib/rails/generators/mobility/backend_generators/table_backend.rb +0 -12
  66. data/lib/rails/generators/mobility/templates/column_translations.rb +2 -2
  67. data/lib/rails/generators/mobility/templates/create_string_translations.rb +5 -5
  68. data/lib/rails/generators/mobility/templates/create_text_translations.rb +5 -5
  69. data/lib/rails/generators/mobility/templates/initializer.rb +8 -0
  70. data/lib/rails/generators/mobility/templates/table_migration.rb +2 -3
  71. data/lib/rails/generators/mobility/templates/table_translations.rb +3 -4
  72. data/lib/rails/generators/mobility/translations_generator.rb +6 -5
  73. metadata +2 -3
  74. metadata.gz.sig +0 -0
  75. data/lib/mobility/backend/stringify_locale.rb +0 -18
@@ -15,24 +15,24 @@ and also to cache translation *records* in {Mobility::Backends::Table} and
15
15
  class TranslationCacher < Module
16
16
  # @param [Symbol] fetch_method Name of translation fetch method to cache
17
17
  def initialize(fetch_method)
18
- define_method fetch_method do |locale, **options|
19
- return super(locale, options) if options.delete(:cache) == false
20
- if cache.has_key?(locale)
21
- cache[locale]
22
- else
23
- cache[locale] = super(locale, options)
18
+ class_eval <<-EOM, __FILE__, __LINE__ + 1
19
+ def #{fetch_method} locale, **options
20
+ return super(locale, options) if options.delete(:cache) == false
21
+ if cache.has_key?(locale)
22
+ cache[locale]
23
+ else
24
+ cache[locale] = super(locale, options)
25
+ end
24
26
  end
25
- end
27
+ EOM
26
28
 
27
- define_method :cache do
28
- @cache ||= {}
29
- end
30
-
31
- define_method :clear_cache do
32
- @cache = {}
33
- end
29
+ include CacheMethods
30
+ end
34
31
 
35
- private :cache, :clear_cache
32
+ module CacheMethods
33
+ private
34
+ def cache; @cache ||= {}; end
35
+ def clear_cache; @cache = {}; end
36
36
  end
37
37
  end
38
38
  end
@@ -73,13 +73,6 @@ The proc can accept zero to three arguments (see examples below)
73
73
  default = options.has_key?(:default) ? options.delete(:default) : default_option
74
74
  if (value = super(locale, options)).nil?
75
75
  return default unless default.is_a?(Proc)
76
- # TODO: Remove in v1.0
77
- if default.parameters.any? { |n, v| [:keyreq, :keyopt].include?(n) && [:model, :attribute, :locale, :options].include?(v) }
78
- warn %{
79
- WARNING: Passing keyword arguments to a Proc in the Default plugin is
80
- deprecated. See the API documentation for details.}
81
- return default.call(model: model, attribute: attribute, locale: locale, options: options)
82
- end
83
76
  args = [attribute, locale, options]
84
77
  args = args.first(default.arity) unless default.arity < 0
85
78
  model.instance_exec(*args, &default)
@@ -37,6 +37,7 @@ the current locale was +nil+.
37
37
 
38
38
  @example With default fallbacks enabled (falls through to default locale)
39
39
  class Post
40
+ extend Mobility
40
41
  translates :title, fallbacks: true
41
42
  end
42
43
 
@@ -54,6 +55,7 @@ the current locale was +nil+.
54
55
 
55
56
  @example With additional fallbacks enabled
56
57
  class Post
58
+ extend Mobility
57
59
  translates :title, fallbacks: { :'en-US' => 'de-DE', :pt => 'de-DE' }
58
60
  end
59
61
 
@@ -70,6 +72,7 @@ the current locale was +nil+.
70
72
 
71
73
  @example Passing fallback option when reading value
72
74
  class Post
75
+ extend Mobility
73
76
  translates :title, fallbacks: true
74
77
  end
75
78
 
@@ -88,6 +91,7 @@ the current locale was +nil+.
88
91
 
89
92
  @example Fallbacks disabled
90
93
  class Post
94
+ extend Mobility
91
95
  translates :title, fallbacks: { :'fr' => 'en' }, locale_accessors: true
92
96
  end
93
97
 
@@ -6,12 +6,18 @@ Module loading Sequel-specific classes for Mobility models.
6
6
  =end
7
7
  module Sequel
8
8
  def self.included(model_class)
9
- query_method = Module.new do
10
- define_method Mobility.query_method do
11
- dataset
12
- end
9
+ model_class.extend DatasetMethod.new(Mobility.query_method)
10
+ end
11
+
12
+ class DatasetMethod < Module
13
+ def initialize(query_method)
14
+ module_eval <<-EOM, __FILE__, __LINE__ + 1
15
+ def #{query_method}
16
+ dataset
17
+ end
18
+ EOM
13
19
  end
14
- model_class.extend query_method
15
20
  end
21
+ private_constant :DatasetMethod
16
22
  end
17
23
  end
@@ -8,16 +8,15 @@ method is called.
8
8
  =end
9
9
  class BackendResetter < Mobility::BackendResetter
10
10
 
11
- # @param [Class] model_class Class of model to which backend resetter will be applied
12
- def included(model_class)
11
+ # (see Mobility::BackendResetter#initialize)
12
+ def initialize(attribute_names, &block)
13
+ super
14
+
13
15
  model_reset_method = @model_reset_method
14
16
 
15
- mod = Module.new do
16
- define_method :refresh do
17
- super().tap { instance_eval(&model_reset_method) }
18
- end
17
+ define_method :refresh do
18
+ super().tap { instance_eval(&model_reset_method) }
19
19
  end
20
- model_class.include mod
21
20
  end
22
21
  end
23
22
  end
@@ -10,14 +10,14 @@ setter method is called.
10
10
  =end
11
11
  class ColumnChanges < Module
12
12
  # @param [Array<String>] attributes Backend attributes
13
- def initialize(*attributes)
13
+ def initialize(attributes, column_affix: "%s")
14
14
  attributes.each do |attribute|
15
15
  define_method "#{attribute}=" do |value, **options|
16
16
  if !options[:super] && send(attribute) != value
17
17
  locale = options[:locale] || Mobility.locale
18
- column = attribute.to_sym
19
- column_with_locale = :"#{attribute}_#{Mobility.normalize_locale(locale)}"
20
- @changed_columns = changed_columns | [column, column_with_locale]
18
+ column = (column_affix % attribute).to_sym
19
+ attribute_with_locale = :"#{attribute}_#{Mobility.normalize_locale(locale)}"
20
+ @changed_columns = changed_columns | [column, attribute.to_sym, attribute_with_locale]
21
21
  end
22
22
  super(value, **options)
23
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mobility
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -30,6 +30,10 @@ module Mobility
30
30
  attributes.select { |a| !a.reference? && a.has_index? }
31
31
  end
32
32
 
33
+ def translation_index_name(column, *columns)
34
+ truncate_index_name("index_#{table_name}_on_#{[column, *columns].join('_and_')}")
35
+ end
36
+
33
37
  private
34
38
 
35
39
  def check_data_source!
@@ -25,18 +25,6 @@ module Mobility
25
25
  def foreign_key
26
26
  "#{model_table_name.singularize}_id"
27
27
  end
28
-
29
- def translation_index_name
30
- truncate_index_name("index_#{table_name}_on_#{foreign_key}")
31
- end
32
-
33
- def translation_locale_index_name
34
- truncate_index_name("index_#{table_name}_on_locale")
35
- end
36
-
37
- def translation_unique_index_name
38
- truncate_index_name("index_#{table_name}_on_#{foreign_key}_and_locale")
39
- end
40
28
  end
41
29
  end
42
30
  end
@@ -4,11 +4,11 @@ class <%= migration_class_name %> < <%= activerecord_migration_class %>
4
4
  <% I18n.available_locales.each do |locale| -%>
5
5
  <% column_name = Mobility.normalize_locale_accessor(attribute.name, locale) -%>
6
6
  <% if connection.column_exists?(table_name, column_name) -%>
7
- <% warn "#{column_name} already exists, skipping." %>
7
+ <% warn "#{column_name} already exists, skipping." -%>
8
8
  <% else -%>
9
9
  add_column :<%= table_name %>, :<%= column_name %>, :<%= attribute.type %><%= attribute.inject_options %>
10
10
  <%- if attribute.has_index? -%>
11
- add_index :<%= table_name %>, :<%= column_name %><%= attribute.inject_index_options %>
11
+ add_index :<%= table_name %>, :<%= column_name %><%= attribute.inject_index_options %>, name: :<%= translation_index_name(column_name) %>
12
12
  <%- end -%>
13
13
  <% end -%>
14
14
  <% end -%>
@@ -2,12 +2,12 @@ class CreateStringTranslations < <%= activerecord_migration_class %>
2
2
 
3
3
  def change
4
4
  create_table :mobility_string_translations do |t|
5
- t.string :locale
6
- t.string :key
5
+ t.string :locale, null: false
6
+ t.string :key, null: false
7
7
  t.string :value
8
- t.integer :translatable_id
9
- t.string :translatable_type
10
- t.timestamps
8
+ t.integer :translatable_id, null: false
9
+ t.string :translatable_type, null: false
10
+ t.timestamps null: false
11
11
  end
12
12
  add_index :mobility_string_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_string_translations_on_keys
13
13
  add_index :mobility_string_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_string_translations_on_translatable_attribute
@@ -2,12 +2,12 @@ class CreateTextTranslations < <%= activerecord_migration_class %>
2
2
 
3
3
  def change
4
4
  create_table :mobility_text_translations do |t|
5
- t.string :locale
6
- t.string :key
5
+ t.string :locale, null: false
6
+ t.string :key, null: false
7
7
  t.text :value
8
- t.integer :translatable_id
9
- t.string :translatable_type
10
- t.timestamps
8
+ t.integer :translatable_id, null: false
9
+ t.string :translatable_type, null: false
10
+ t.timestamps null: false
11
11
  end
12
12
  add_index :mobility_text_translations, [:translatable_id, :translatable_type, :locale, :key], unique: true, name: :index_mobility_text_translations_on_keys
13
13
  add_index :mobility_text_translations, [:translatable_id, :translatable_type, :key], name: :index_mobility_text_translations_on_translatable_attribute
@@ -77,4 +77,12 @@ Mobility.configure do |config|
77
77
  # Note: The dirty plugin enables fallthrough_accessors by default.
78
78
  #
79
79
  # config.default_options[:fallthrough_accessors] = true
80
+
81
+ # You can also include backend-specific default options. For example, if you
82
+ # want to default to using the text-type translation table with the KeyValue
83
+ # backend, you can set that as a default by uncommenting this line, or change
84
+ # it to :string to default to the string-type translation table instead. (For
85
+ # other backends, this option is ignored.)
86
+ #
87
+ # config.default_options[:type] = :text
80
88
  end
@@ -5,11 +5,10 @@ class <%= migration_class_name %> < <%= activerecord_migration_class %>
5
5
  add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
6
6
  <%- elsif attribute.respond_to?(:token?) && attribute.token? -%>
7
7
  add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
8
- add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
9
- <%- else -%>
8
+ add_index :<%= table_name %>, [:<%= attribute.index_name %><%= attribute.inject_index_options %>, :locale], name: :<%= translation_index_name(attribute.index_name, "locale") %>, unique: true <%- else -%>
10
9
  add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
11
10
  <%- if attribute.has_index? -%>
12
- add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
11
+ add_index :<%= table_name %>, [:<%= attribute.index_name %><%= attribute.inject_index_options %>, :locale], name: :<%= translation_index_name(attribute.index_name, "locale") %>
13
12
  <%- end -%>
14
13
  <%- end -%>
15
14
  <% end -%>
@@ -17,12 +17,11 @@ class <%= migration_class_name %> < <%= activerecord_migration_class %>
17
17
  t.timestamps null: false
18
18
  end
19
19
 
20
- add_index :<%= table_name %>, :<%= foreign_key %>, name: :<%= translation_index_name %>
21
- add_index :<%= table_name %>, :locale, name: :<%= translation_locale_index_name %>
22
- add_index :<%= table_name %>, [:<%= foreign_key %>, :locale], name: :<%= translation_unique_index_name %>, unique: true
20
+ add_index :<%= table_name %>, :locale, name: :<%= translation_index_name("locale") %>
21
+ add_index :<%= table_name %>, [:<%= foreign_key %>, :locale], name: :<%= translation_index_name(foreign_key, "locale") %>, unique: true
23
22
 
24
23
  <%- attributes_with_index.each do |attribute| -%>
25
- add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
24
+ add_index :<%= table_name %>, [:<%= attribute.index_name %><%= attribute.inject_index_options %>, :locale], name: :<%= translation_index_name(attribute.index_name, "locale") %>
26
25
  <%- end -%>
27
26
  end
28
27
  end
@@ -29,16 +29,17 @@ skipped.
29
29
  Other backends are not supported, for obvious reasons:
30
30
  * the +key_value+ backend does not need any model-specific migrations, simply
31
31
  run the install generator.
32
- * +jsonb+, +hstore+ and +serialized+ backends simply require a single column on
33
- a model table, which can be added with the normal Rails migration generator.
32
+ * +json+, +jsonb+, +hstore+, +serialized+, and +container+ backends simply
33
+ require a single column on a model table, which can be added with the normal
34
+ Rails migration generator.
34
35
 
35
36
  =end
36
37
  class TranslationsGenerator < ::Rails::Generators::NamedBase
37
- SUPPORTED_BACKENDS = %w[column table]
38
- BACKEND_OPTIONS = { type: :string, desc: "Backend to use for translations (defaults to Mobility.default_backend)" }
38
+ SUPPORTED_BACKENDS = %w[column table].freeze
39
+ BACKEND_OPTIONS = { type: :string, desc: "Backend to use for translations (defaults to Mobility.default_backend)" }.freeze
39
40
  argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
40
41
 
41
- class_option(:backend, BACKEND_OPTIONS)
42
+ class_option(:backend, BACKEND_OPTIONS.dup)
42
43
  invoke_from_option :backend
43
44
 
44
45
  def self.class_options(options = nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
@@ -30,7 +30,7 @@ cert_chain:
30
30
  eGDROPZoL5RXwiOnRbexxa7dcAxMrDfGB/hpiunIPWPsi4n5P7K/6OO/sGVMl9xv
31
31
  SZBPXjzrHdyOFLBYXB+PG7s3F/4=
32
32
  -----END CERTIFICATE-----
33
- date: 2018-03-21 00:00:00.000000000 Z
33
+ date: 2018-04-26 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: request_store
@@ -180,7 +180,6 @@ files:
180
180
  - lib/mobility/attributes.rb
181
181
  - lib/mobility/backend.rb
182
182
  - lib/mobility/backend/orm_delegator.rb
183
- - lib/mobility/backend/stringify_locale.rb
184
183
  - lib/mobility/backend_resetter.rb
185
184
  - lib/mobility/backends.rb
186
185
  - lib/mobility/backends/active_record.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,18 +0,0 @@
1
- module Mobility
2
- module Backend
3
- =begin
4
-
5
- Module which stringifies the locale passed in to read and write methods.
6
-
7
- =end
8
- module StringifyLocale
9
- def read(locale, options = {})
10
- super(locale.to_s, options)
11
- end
12
-
13
- def write(locale, value, options = {})
14
- super(locale.to_s, value, options)
15
- end
16
- end
17
- end
18
- end