mobility 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -16,4 +16,5 @@ Provides interface to access attributes across all backends of an instance.
16
16
  @model.send(Backend.method_name(attribute))
17
17
  end
18
18
  end
19
+ private_constant :Adapter
19
20
  end
@@ -153,7 +153,7 @@ with other backends.
153
153
  plugin.apply(self, options[name])
154
154
  end
155
155
 
156
- names.each do |name|
156
+ each do |name|
157
157
  define_backend(name)
158
158
  define_reader(name) if %i[accessor reader].include?(method)
159
159
  define_writer(name) if %i[accessor writer].include?(method)
@@ -169,18 +169,8 @@ with other backends.
169
169
  names.each(&block)
170
170
  end
171
171
 
172
- # Process options passed into accessor method before calling backend, and
173
- # return locale
174
- # @deprecated This method was mainly used internally but is no longer
175
- # needed. It will be removed in the next major release.
176
- # @param [Hash] options Options hash passed to accessor method
177
- # @return [Symbol] locale
178
- # TODO: Remove in v1.0
179
- def self.process_options!(options)
180
- (options[:locale] || Mobility.locale).tap { |locale|
181
- Mobility.enforce_available_locales!(locale)
182
- options[:locale] &&= !!locale
183
- }.to_sym
172
+ def inspect
173
+ "#<Attributes (#{backend_name}) @names=#{names.join(", ")}>"
184
174
  end
185
175
 
186
176
  private
@@ -19,6 +19,7 @@ or locales.)
19
19
 
20
20
  @example
21
21
  class Post < ActiveRecord::Base
22
+ extend Mobility
22
23
  translates :title, backend: :column
23
24
  end
24
25
 
@@ -3,35 +3,40 @@ require "mobility/backends/active_record/query_methods"
3
3
 
4
4
  module Mobility
5
5
  module Backends
6
- class ActiveRecord::Column::QueryMethods < ActiveRecord::QueryMethods
7
- def initialize(attributes, _)
8
- super
9
- q = self
6
+ module ActiveRecord
7
+ class Column::QueryMethods < QueryMethods
8
+ def initialize(attributes, _)
9
+ super
10
+ q = self
10
11
 
11
- define_method :where! do |opts, *rest|
12
- super(q.convert_opts(opts), *rest)
12
+ define_method :where! do |opts, *rest|
13
+ super(q.convert_opts(opts), *rest)
14
+ end
13
15
  end
14
- end
15
16
 
16
- def extended(relation)
17
- super
18
- q = self
17
+ def extended(relation)
18
+ super
19
+ q = self
19
20
 
20
- mod = Module.new do
21
- define_method :not do |opts, *rest|
22
- super(q.convert_opts(opts), *rest)
21
+ mod = Module.new do
22
+ define_method :not do |opts, *rest|
23
+ super(q.convert_opts(opts), *rest)
24
+ end
23
25
  end
26
+ relation.mobility_where_chain.include(mod)
24
27
  end
25
- relation.mobility_where_chain.include(mod)
26
- end
27
28
 
28
- def convert_opts(opts)
29
- if i18n_keys = extract_attributes(opts)
30
- opts = opts.with_indifferent_access
31
- i18n_keys.each { |attr| opts[Column.column_name_for(attr)] = opts.delete(attr) }
29
+ def convert_opts(opts)
30
+ if i18n_keys = extract_attributes(opts)
31
+ opts = opts.with_indifferent_access
32
+ i18n_keys.each do |attr|
33
+ opts[Backends::Column.column_name_for(attr)] = collapse opts.delete(attr)
34
+ end
35
+ end
36
+ opts
32
37
  end
33
- opts
34
38
  end
39
+ Column.private_constant :QueryMethods
35
40
  end
36
41
  end
37
42
  end
@@ -4,27 +4,33 @@ require "mobility/backends/active_record/query_methods"
4
4
 
5
5
  module Mobility
6
6
  module Backends
7
- class ActiveRecord::Container::JsonQueryMethods < ActiveRecord::QueryMethods
8
- include ActiveRecord::PgQueryMethods
9
- attr_reader :column_name, :column
7
+ module ActiveRecord
8
+ class Container::JsonQueryMethods < QueryMethods
9
+ include PgQueryMethods
10
+ attr_reader :column_name, :column
10
11
 
11
- def initialize(_attributes, options)
12
- super
13
- @column_name = options[:column_name]
14
- @column = arel_table[@column_name]
15
- end
12
+ def initialize(_attributes, options)
13
+ super
14
+ @column = arel_table[options[:column_name]]
15
+ end
16
16
 
17
- private
17
+ def matches(key, locale)
18
+ build_infix(:'->>', build_infix(:'->', column, build_quoted(locale)), build_quoted(key))
19
+ end
18
20
 
19
- def matches(key, value, locale)
20
- build_infix(:'->>',
21
- build_infix(:'->', column, quote(locale)),
22
- quote(key)).eq(value && value.to_s)
23
- end
21
+ def exists(key, locale)
22
+ matches(key, locale).eq(nil).not
23
+ end
24
+
25
+ def absent(key, locale)
26
+ matches(key, locale).eq(nil)
27
+ end
24
28
 
25
- def has_locale(key, locale)
26
- matches(key, nil, locale).not
29
+ def quote(value)
30
+ value.to_s
31
+ end
27
32
  end
33
+ Container.private_constant :JsonQueryMethods
28
34
  end
29
35
  end
30
36
  end
@@ -4,30 +4,30 @@ require "mobility/backends/active_record/query_methods"
4
4
 
5
5
  module Mobility
6
6
  module Backends
7
- class ActiveRecord::Container::JsonbQueryMethods < ActiveRecord::QueryMethods
8
- include ActiveRecord::PgQueryMethods
9
- attr_reader :column_name, :column
7
+ module ActiveRecord
8
+ class Container::JsonbQueryMethods < QueryMethods
9
+ include PgQueryMethods
10
+ attr_reader :column
10
11
 
11
- def initialize(_attributes, options)
12
- super
13
- @column_name = options[:column_name]
14
- @column = arel_table[@column_name]
15
- end
12
+ def initialize(_attributes, options)
13
+ super
14
+ @column = arel_table[options[:column_name]]
15
+ end
16
16
 
17
- private
17
+ def matches(key, locale)
18
+ build_infix(:'->', build_infix(:'->', column, build_quoted(locale)), build_quoted(key))
19
+ end
18
20
 
19
- def matches(key, value, locale)
20
- build_infix(:'->',
21
- build_infix(:'->', column, quote(locale)),
22
- quote(key)).eq(quote(value.to_json))
23
- end
21
+ def exists(key, locale)
22
+ build_infix(:'?', column, build_quoted(locale)).and(
23
+ build_infix(:'?', build_infix(:'->', column, build_quoted(locale)), build_quoted(key)))
24
+ end
24
25
 
25
- def has_locale(key, locale)
26
- build_infix(:'?', column, quote(locale)).and(
27
- build_infix(:'?',
28
- build_infix(:'->', column, quote(locale)),
29
- quote(key)))
26
+ def quote(value)
27
+ build_quoted(value.to_json)
28
+ end
30
29
  end
30
+ Container.private_constant :JsonbQueryMethods
31
31
  end
32
32
  end
33
33
  end
@@ -9,21 +9,23 @@ Implements the {Mobility::Backends::Hstore} backend for ActiveRecord models.
9
9
  @see Mobility::Backends::ActiveRecord::HashValued
10
10
 
11
11
  =end
12
- class ActiveRecord::Hstore < ActiveRecord::PgHash
13
- require 'mobility/backends/active_record/hstore/query_methods'
12
+ module ActiveRecord
13
+ class Hstore < PgHash
14
+ require 'mobility/backends/active_record/hstore/query_methods'
14
15
 
15
- # @!group Backend Accessors
16
- # @!macro backend_reader
17
- # @!method read(locale, **options)
16
+ # @!group Backend Accessors
17
+ # @!macro backend_reader
18
+ # @!method read(locale, **options)
18
19
 
19
- # @!group Backend Accessors
20
- # @!macro backend_writer
21
- def write(locale, value, options = {})
22
- super(locale, value && value.to_s, options)
23
- end
24
- # @!endgroup
20
+ # @!group Backend Accessors
21
+ # @!macro backend_writer
22
+ def write(locale, value, options = {})
23
+ super(locale, value && value.to_s, options)
24
+ end
25
+ # @!endgroup
25
26
 
26
- setup_query_methods(QueryMethods)
27
+ setup_query_methods(QueryMethods)
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -3,14 +3,23 @@ require 'mobility/backends/active_record/query_methods'
3
3
 
4
4
  module Mobility
5
5
  module Backends
6
- class ActiveRecord::Hstore::QueryMethods < ActiveRecord::QueryMethods
7
- include ActiveRecord::PgQueryMethods
6
+ module ActiveRecord
7
+ class Hstore::QueryMethods < QueryMethods
8
+ include PgQueryMethods
8
9
 
9
- private
10
+ def matches(key, locale)
11
+ build_infix(:'->', arel_table[column_name(key)], build_quoted(locale))
12
+ end
10
13
 
11
- def matches(key, value, locale)
12
- build_infix(:'->', arel_table[key], quote(locale)).eq(quote(value.to_s))
14
+ def exists(key, locale)
15
+ build_infix(:'?', arel_table[column_name(key)], build_quoted(locale))
16
+ end
17
+
18
+ def quote(value)
19
+ build_quoted(value)
20
+ end
13
21
  end
22
+ Hstore.private_constant :QueryMethods
14
23
  end
15
24
  end
16
25
  end
@@ -9,28 +9,30 @@ Implements the {Mobility::Backends::Json} backend for ActiveRecord models.
9
9
  @see Mobility::Backends::ActiveRecord::HashValued
10
10
 
11
11
  =end
12
- class ActiveRecord::Json < ActiveRecord::PgHash
13
- require 'mobility/backends/active_record/json/query_methods'
12
+ module ActiveRecord
13
+ class Json < PgHash
14
+ require 'mobility/backends/active_record/json/query_methods'
14
15
 
15
- # @!group Backend Accessors
16
- #
17
- # @note Translation may be string, integer or boolean-valued since
18
- # value is stored on a JSON hash.
19
- # @param [Symbol] locale Locale to read
20
- # @param [Hash] options
21
- # @return [String,Integer,Boolean] Value of translation
22
- # @!method read(locale, **options)
16
+ # @!group Backend Accessors
17
+ #
18
+ # @note Translation may be string, integer or boolean-valued since
19
+ # value is stored on a JSON hash.
20
+ # @param [Symbol] locale Locale to read
21
+ # @param [Hash] options
22
+ # @return [String,Integer,Boolean] Value of translation
23
+ # @!method read(locale, **options)
23
24
 
24
- # @!group Backend Accessors
25
- # @note Translation may be string, integer or boolean-valued since
26
- # value is stored on a JSON hash.
27
- # @param [Symbol] locale Locale to write
28
- # @param [String,Integer,Boolean] value Value to write
29
- # @param [Hash] options
30
- # @return [String,Integer,Boolean] Updated value
31
- # @!method write(locale, value, **options)
25
+ # @!group Backend Accessors
26
+ # @note Translation may be string, integer or boolean-valued since
27
+ # value is stored on a JSON hash.
28
+ # @param [Symbol] locale Locale to write
29
+ # @param [String,Integer,Boolean] value Value to write
30
+ # @param [Hash] options
31
+ # @return [String,Integer,Boolean] Updated value
32
+ # @!method write(locale, value, **options)
32
33
 
33
- setup_query_methods(QueryMethods)
34
+ setup_query_methods(QueryMethods)
35
+ end
34
36
  end
35
37
  end
36
38
  end
@@ -4,22 +4,27 @@ require "mobility/backends/active_record/query_methods"
4
4
 
5
5
  module Mobility
6
6
  module Backends
7
- class ActiveRecord::Json::QueryMethods < ActiveRecord::QueryMethods
8
- include ActiveRecord::PgQueryMethods
7
+ module ActiveRecord
8
+ class Json::QueryMethods < QueryMethods
9
+ include PgQueryMethods
9
10
 
10
- private
11
+ def matches(key, locale)
12
+ build_infix(:'->>', arel_table[column_name(key)], build_quoted(locale))
13
+ end
11
14
 
12
- def matches(key, value, locale)
13
- build_locale_infix(key, locale).eq(value.to_s)
14
- end
15
+ def exists(key, locale)
16
+ absent(key, locale).not
17
+ end
15
18
 
16
- def has_locale(key, locale)
17
- build_locale_infix(key, locale).eq(nil).not
18
- end
19
+ def absent(key, locale)
20
+ matches(key, locale).eq(nil)
21
+ end
19
22
 
20
- def build_locale_infix(key, locale)
21
- build_infix(:'->>', arel_table[key], quote(locale))
23
+ def quote(value)
24
+ value.to_s
25
+ end
22
26
  end
27
+ Json.private_constant :QueryMethods
23
28
  end
24
29
  end
25
30
  end
@@ -9,28 +9,30 @@ Implements the {Mobility::Backends::Jsonb} backend for ActiveRecord models.
9
9
  @see Mobility::Backends::ActiveRecord::HashValued
10
10
 
11
11
  =end
12
- class ActiveRecord::Jsonb < ActiveRecord::PgHash
13
- require 'mobility/backends/active_record/jsonb/query_methods'
12
+ module ActiveRecord
13
+ class Jsonb < PgHash
14
+ require 'mobility/backends/active_record/jsonb/query_methods'
14
15
 
15
- # @!group Backend Accessors
16
- #
17
- # @note Translation may be any json type, but querying will only work on
18
- # string-typed values.
19
- # @param [Symbol] locale Locale to read
20
- # @param [Hash] options
21
- # @return [String,Integer,Boolean] Value of translation
22
- # @!method read(locale, **options)
16
+ # @!group Backend Accessors
17
+ #
18
+ # @note Translation may be any json type, but querying will only work on
19
+ # string-typed values.
20
+ # @param [Symbol] locale Locale to read
21
+ # @param [Hash] options
22
+ # @return [String,Integer,Boolean] Value of translation
23
+ # @!method read(locale, **options)
23
24
 
24
- # @!group Backend Accessors
25
- # @note Translation may be any json type, but querying will only work on
26
- # string-typed values.
27
- # @param [Symbol] locale Locale to write
28
- # @param [String,Integer,Boolean] value Value to write
29
- # @param [Hash] options
30
- # @return [String,Integer,Boolean] Updated value
31
- # @!method write(locale, value, **options)
25
+ # @!group Backend Accessors
26
+ # @note Translation may be any json type, but querying will only work on
27
+ # string-typed values.
28
+ # @param [Symbol] locale Locale to write
29
+ # @param [String,Integer,Boolean] value Value to write
30
+ # @param [Hash] options
31
+ # @return [String,Integer,Boolean] Updated value
32
+ # @!method write(locale, value, **options)
32
33
 
33
- setup_query_methods(QueryMethods)
34
+ setup_query_methods(QueryMethods)
35
+ end
34
36
  end
35
37
  end
36
38
  end
@@ -4,14 +4,23 @@ require "mobility/backends/active_record/query_methods"
4
4
 
5
5
  module Mobility
6
6
  module Backends
7
- class ActiveRecord::Jsonb::QueryMethods < ActiveRecord::QueryMethods
8
- include ActiveRecord::PgQueryMethods
7
+ module ActiveRecord
8
+ class Jsonb::QueryMethods < QueryMethods
9
+ include PgQueryMethods
9
10
 
10
- private
11
+ def matches(key, locale)
12
+ build_infix(:'->', arel_table[column_name(key)], build_quoted(locale))
13
+ end
11
14
 
12
- def matches(key, value, locale)
13
- build_infix(:'->', arel_table[key], quote(locale)).eq(quote(value.to_json))
15
+ def exists(key, locale)
16
+ build_infix(:'?', arel_table[column_name(key)], build_quoted(locale))
17
+ end
18
+
19
+ def quote(value)
20
+ build_quoted(value.to_json)
21
+ end
14
22
  end
23
+ Jsonb.private_constant :QueryMethods
15
24
  end
16
25
  end
17
26
  end