mobility 0.1.20 → 0.2.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -0
- data/CHANGELOG.md +17 -0
- data/CONTRIBUTING.md +55 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +2 -56
- data/README.md +64 -15
- data/Rakefile +17 -17
- data/lib/mobility.rb +43 -40
- data/lib/mobility/active_model.rb +0 -1
- data/lib/mobility/active_model/backend_resetter.rb +1 -1
- data/lib/mobility/active_record.rb +8 -15
- data/lib/mobility/active_record/backend_resetter.rb +2 -0
- data/lib/mobility/active_record/model_translation.rb +1 -1
- data/lib/mobility/active_record/string_translation.rb +2 -0
- data/lib/mobility/active_record/text_translation.rb +2 -0
- data/lib/mobility/attributes.rb +74 -71
- data/lib/mobility/backend.rb +64 -25
- data/lib/mobility/backend/orm_delegator.rb +17 -7
- data/lib/mobility/backend/stringify_locale.rb +18 -0
- data/lib/mobility/backend_resetter.rb +8 -5
- data/lib/mobility/backends.rb +4 -0
- data/lib/mobility/backends/active_record.rb +20 -0
- data/lib/mobility/{backend → backends}/active_record/column.rb +25 -13
- data/lib/mobility/{backend → backends}/active_record/column/query_methods.rb +5 -3
- data/lib/mobility/backends/active_record/hstore.rb +29 -0
- data/lib/mobility/{backend → backends}/active_record/hstore/query_methods.rb +2 -2
- data/lib/mobility/{backend → backends}/active_record/jsonb.rb +6 -6
- data/lib/mobility/{backend → backends}/active_record/jsonb/query_methods.rb +4 -2
- data/lib/mobility/{backend → backends}/active_record/key_value.rb +10 -44
- data/lib/mobility/{backend → backends}/active_record/key_value/query_methods.rb +4 -2
- data/lib/mobility/{backend/active_record/hash_valued.rb → backends/active_record/pg_hash.rb} +13 -21
- data/lib/mobility/{backend → backends}/active_record/query_methods.rb +2 -2
- data/lib/mobility/{backend → backends}/active_record/serialized.rb +12 -28
- data/lib/mobility/{backend → backends}/active_record/serialized/query_methods.rb +5 -8
- data/lib/mobility/{backend → backends}/active_record/table.rb +11 -60
- data/lib/mobility/{backend → backends}/active_record/table/query_methods.rb +5 -3
- data/lib/mobility/{backend → backends}/column.rb +8 -4
- data/lib/mobility/backends/hash_valued.rb +29 -0
- data/lib/mobility/{backend → backends}/hstore.rb +4 -4
- data/lib/mobility/{backend → backends}/jsonb.rb +4 -4
- data/lib/mobility/backends/key_value.rb +111 -0
- data/lib/mobility/{backend → backends}/null.rb +4 -4
- data/lib/mobility/backends/sequel.rb +20 -0
- data/lib/mobility/backends/sequel/column.rb +52 -0
- data/lib/mobility/{backend → backends}/sequel/column/query_methods.rb +5 -3
- data/lib/mobility/backends/sequel/hstore.rb +29 -0
- data/lib/mobility/{backend → backends}/sequel/hstore/query_methods.rb +4 -3
- data/lib/mobility/{backend → backends}/sequel/jsonb.rb +6 -6
- data/lib/mobility/{backend → backends}/sequel/jsonb/query_methods.rb +4 -3
- data/lib/mobility/{backend → backends}/sequel/key_value.rb +28 -39
- data/lib/mobility/{backend → backends}/sequel/key_value/query_methods.rb +4 -5
- data/lib/mobility/backends/sequel/pg_hash.rb +46 -0
- data/lib/mobility/{backend → backends}/sequel/postgres_query_methods.rb +1 -2
- data/lib/mobility/{backend → backends}/sequel/query_methods.rb +6 -4
- data/lib/mobility/{backend → backends}/sequel/serialized.rb +17 -38
- data/lib/mobility/backends/sequel/serialized/query_methods.rb +17 -0
- data/lib/mobility/{backend → backends}/sequel/table.rb +29 -60
- data/lib/mobility/{backend → backends}/sequel/table/query_methods.rb +5 -3
- data/lib/mobility/{backend → backends}/serialized.rb +27 -5
- data/lib/mobility/{backend → backends}/table.rb +69 -29
- data/lib/mobility/configuration.rb +40 -0
- data/lib/mobility/{orm.rb → loaded.rb} +0 -0
- data/lib/mobility/plugins.rb +35 -0
- data/lib/mobility/plugins/active_model.rb +6 -0
- data/lib/mobility/plugins/active_model/dirty.rb +81 -0
- data/lib/mobility/plugins/active_record.rb +6 -0
- data/lib/mobility/plugins/active_record/dirty.rb +59 -0
- data/lib/mobility/plugins/cache.rb +54 -0
- data/lib/mobility/plugins/cache/translation_cacher.rb +40 -0
- data/lib/mobility/plugins/default.rb +73 -0
- data/lib/mobility/plugins/dirty.rb +61 -0
- data/lib/mobility/{backend → plugins}/fallbacks.rb +36 -31
- data/lib/mobility/plugins/fallthrough_accessors.rb +66 -0
- data/lib/mobility/plugins/locale_accessors.rb +84 -0
- data/lib/mobility/{backend → plugins}/presence.rb +15 -6
- data/lib/mobility/plugins/sequel.rb +6 -0
- data/lib/mobility/plugins/sequel/dirty.rb +59 -0
- data/lib/mobility/sequel.rb +5 -14
- data/lib/mobility/sequel/backend_resetter.rb +4 -6
- data/lib/mobility/sequel/column_changes.rb +4 -4
- data/lib/mobility/sequel/model_translation.rb +1 -1
- data/lib/mobility/sequel/string_translation.rb +2 -0
- data/lib/mobility/sequel/text_translation.rb +2 -0
- data/lib/mobility/translates.rb +1 -5
- data/lib/mobility/util.rb +126 -0
- data/lib/mobility/version.rb +1 -1
- data/lib/mobility/wrapper.rb +1 -1
- data/lib/rails/generators/mobility/translations_generator.rb +7 -3
- metadata +85 -55
- metadata.gz.sig +0 -0
- data/lib/mobility/backend/active_model.rb +0 -7
- data/lib/mobility/backend/active_model/dirty.rb +0 -95
- data/lib/mobility/backend/active_record.rb +0 -29
- data/lib/mobility/backend/active_record/dirty.rb +0 -54
- data/lib/mobility/backend/active_record/hstore.rb +0 -29
- data/lib/mobility/backend/cache.rb +0 -117
- data/lib/mobility/backend/dirty.rb +0 -38
- data/lib/mobility/backend/key_value.rb +0 -85
- data/lib/mobility/backend/sequel.rb +0 -29
- data/lib/mobility/backend/sequel/column.rb +0 -39
- data/lib/mobility/backend/sequel/dirty.rb +0 -57
- data/lib/mobility/backend/sequel/hash_valued.rb +0 -51
- data/lib/mobility/backend/sequel/hstore.rb +0 -29
- data/lib/mobility/backend/sequel/serialized/query_methods.rb +0 -20
- data/lib/mobility/core_ext/object.rb +0 -30
- data/lib/mobility/core_ext/string.rb +0 -16
- data/lib/mobility/fallthrough_accessors.rb +0 -57
- data/lib/mobility/locale_accessors.rb +0 -55
data/lib/mobility/backend.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# frozen-string-literal: true
|
|
2
|
+
require "mobility/backend/orm_delegator"
|
|
2
3
|
|
|
3
4
|
module Mobility
|
|
4
5
|
=begin
|
|
@@ -8,9 +9,8 @@ Defines a minimum set of shared components included in any backend. These are:
|
|
|
8
9
|
- a reader returning the +model+ on which the backend is defined ({#model})
|
|
9
10
|
- a reader returning the +attribute+ for which the backend is defined
|
|
10
11
|
({#attribute})
|
|
11
|
-
- a
|
|
12
|
-
|
|
13
|
-
and extracting fallbacks from the options hash ({#initialize})
|
|
12
|
+
- a constructor setting these two elements (+model+, +attribute+), and
|
|
13
|
+
extracting fallbacks from the options hash ({#initialize})
|
|
14
14
|
- a +setup+ method adding any configuration code to the model class
|
|
15
15
|
({Setup#setup})
|
|
16
16
|
|
|
@@ -18,6 +18,8 @@ On top of this, a backend will normally:
|
|
|
18
18
|
|
|
19
19
|
- implement a +read+ instance method to read from the backend
|
|
20
20
|
- implement a +write+ instance method to write to the backend
|
|
21
|
+
- implement an +each_locale+ instance method to iterate through available locales
|
|
22
|
+
(used to define other +Enumerable+ traversal and search methods)
|
|
21
23
|
- implement a +configure+ class method to apply any normalization to the
|
|
22
24
|
options hash
|
|
23
25
|
- call the +setup+ method yielding attributes and options to configure the
|
|
@@ -27,11 +29,15 @@ On top of this, a backend will normally:
|
|
|
27
29
|
class MyBackend
|
|
28
30
|
include Mobility::Backend
|
|
29
31
|
|
|
30
|
-
def read(locale,
|
|
32
|
+
def read(locale, options = {})
|
|
31
33
|
# ...
|
|
32
34
|
end
|
|
33
35
|
|
|
34
|
-
def write(locale, value,
|
|
36
|
+
def write(locale, value, options = {})
|
|
37
|
+
# ...
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def each_locale
|
|
35
41
|
# ...
|
|
36
42
|
end
|
|
37
43
|
|
|
@@ -49,21 +55,7 @@ On top of this, a backend will normally:
|
|
|
49
55
|
=end
|
|
50
56
|
|
|
51
57
|
module Backend
|
|
52
|
-
|
|
53
|
-
autoload :ActiveRecord, 'mobility/backend/active_record'
|
|
54
|
-
autoload :Cache, 'mobility/backend/cache'
|
|
55
|
-
autoload :Column, 'mobility/backend/column'
|
|
56
|
-
autoload :Dirty, 'mobility/backend/dirty'
|
|
57
|
-
autoload :Fallbacks, 'mobility/backend/fallbacks'
|
|
58
|
-
autoload :Hstore, 'mobility/backend/hstore'
|
|
59
|
-
autoload :Jsonb, 'mobility/backend/jsonb'
|
|
60
|
-
autoload :KeyValue, 'mobility/backend/key_value'
|
|
61
|
-
autoload :Null, 'mobility/backend/null'
|
|
62
|
-
autoload :OrmDelegator, 'mobility/backend/orm_delegator'
|
|
63
|
-
autoload :Presence, 'mobility/backend/presence'
|
|
64
|
-
autoload :Sequel, 'mobility/backend/sequel'
|
|
65
|
-
autoload :Serialized, 'mobility/backend/serialized'
|
|
66
|
-
autoload :Table, 'mobility/backend/table'
|
|
58
|
+
include Enumerable
|
|
67
59
|
|
|
68
60
|
# @return [String] Backend attribute
|
|
69
61
|
attr_reader :attribute
|
|
@@ -80,29 +72,57 @@ On top of this, a backend will normally:
|
|
|
80
72
|
end
|
|
81
73
|
|
|
82
74
|
# @!macro [new] backend_reader
|
|
75
|
+
# Gets the translated value for provided locale from configured backend.
|
|
83
76
|
# @param [Symbol] locale Locale to read
|
|
84
77
|
# @return [Object] Value of translation
|
|
85
78
|
#
|
|
86
79
|
# @!macro [new] backend_writer
|
|
80
|
+
# Updates translation for provided locale without calling backend's methods to persist the changes.
|
|
87
81
|
# @param [Symbol] locale Locale to write
|
|
88
82
|
# @param [Object] value Value to write
|
|
89
83
|
# @return [Object] Updated value
|
|
90
84
|
|
|
85
|
+
# @!macro [new] backend_iterator
|
|
86
|
+
# Yields locales available for this attribute.
|
|
87
|
+
# @yieldparam [Symbol] Locale
|
|
88
|
+
def each_locale
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Yields translations to block
|
|
92
|
+
# @yieldparam [Mobility::Backend::Translation] Translation
|
|
93
|
+
def each
|
|
94
|
+
each_locale { |locale| yield Translation.new(self, locale) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# List locales available for this backend.
|
|
98
|
+
# @return [Array<Symbol>] Array of available locales
|
|
99
|
+
def locales
|
|
100
|
+
map(&:locale)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @param [Symbol] locale Locale to read
|
|
104
|
+
# @return [TrueClass,FalseClass] Whether translation is present for locale
|
|
105
|
+
def present?(locale, options = {})
|
|
106
|
+
Util.present?(read(locale, options))
|
|
107
|
+
end
|
|
108
|
+
|
|
91
109
|
# Extend included class with +setup+ method
|
|
92
110
|
def self.included(base)
|
|
93
111
|
base.extend(Setup)
|
|
112
|
+
base.extend(ClassMethods)
|
|
94
113
|
end
|
|
95
114
|
|
|
96
115
|
# @param [String] attribute
|
|
97
116
|
# @return [String] name of backend reader method
|
|
98
117
|
def self.method_name(attribute)
|
|
99
|
-
|
|
118
|
+
@backend_method_names ||= {}
|
|
119
|
+
@backend_method_names[attribute] ||= "#{attribute}_backend".freeze
|
|
100
120
|
end
|
|
101
121
|
|
|
102
122
|
# Defines setup hooks for backend to customize model class.
|
|
103
123
|
module Setup
|
|
104
124
|
# Assign block to be called on model class.
|
|
105
|
-
# @yield [
|
|
125
|
+
# @yield [attribute_names, options]
|
|
106
126
|
# @note When called multiple times, setup blocks will be appended
|
|
107
127
|
# so that they are run together consecutively on class.
|
|
108
128
|
def setup &block
|
|
@@ -123,13 +143,15 @@ On top of this, a backend will normally:
|
|
|
123
143
|
|
|
124
144
|
# Call setup block on a class with attributes and options.
|
|
125
145
|
# @param model_class Class to be setup-ed
|
|
126
|
-
# @param [Array<String>]
|
|
146
|
+
# @param [Array<String>] attribute_names
|
|
127
147
|
# @param [Hash] options
|
|
128
|
-
def setup_model(model_class,
|
|
148
|
+
def setup_model(model_class, attribute_names, **options)
|
|
129
149
|
return unless setup_block = @setup_block
|
|
130
|
-
model_class.class_exec(
|
|
150
|
+
model_class.class_exec(attribute_names, options, &setup_block)
|
|
131
151
|
end
|
|
152
|
+
end
|
|
132
153
|
|
|
154
|
+
module ClassMethods
|
|
133
155
|
# {Attributes} uses this method to get a backend class specific to the
|
|
134
156
|
# model using the backend. Backend classes can override this method to
|
|
135
157
|
# return a class specific to the model class using the backend (e.g.
|
|
@@ -141,6 +163,23 @@ On top of this, a backend will normally:
|
|
|
141
163
|
def for(_)
|
|
142
164
|
self
|
|
143
165
|
end
|
|
166
|
+
|
|
167
|
+
# Called from plugins to apply custom processing for this backend.
|
|
168
|
+
# Name is the name of the plugin.
|
|
169
|
+
# @param [Symbol] name Name of plugin
|
|
170
|
+
# @return [Boolean] Whether the plugin was applied
|
|
171
|
+
# @note This is currently only called by Plugins::Cache.
|
|
172
|
+
def apply_plugin(_)
|
|
173
|
+
false
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
Translation = Struct.new(:backend, :locale) do
|
|
178
|
+
%w[read write].each do |accessor|
|
|
179
|
+
define_method accessor do |*args|
|
|
180
|
+
backend.send(accessor, locale, *args)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
144
183
|
end
|
|
145
184
|
end
|
|
146
185
|
end
|
|
@@ -8,25 +8,35 @@ Adds {#for} method to backend to return ORM-specific backend.
|
|
|
8
8
|
class Post < ActiveRecord::Base
|
|
9
9
|
# ...
|
|
10
10
|
end
|
|
11
|
-
Mobility::
|
|
12
|
-
#=> Mobility::
|
|
11
|
+
Mobility::Backends::KeyValue.for(Post)
|
|
12
|
+
#=> Mobility::Backends::ActiveRecord::KeyValue
|
|
13
13
|
|
|
14
14
|
=end
|
|
15
15
|
module OrmDelegator
|
|
16
16
|
# @param [Class] model_class Class of model
|
|
17
17
|
# @return [Class] Class of backend to use for model
|
|
18
18
|
def for(model_class)
|
|
19
|
+
namespace = name.split('::'.freeze)
|
|
19
20
|
if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
|
|
20
|
-
|
|
21
|
+
require_backend("active_record", namespace.last.underscore)
|
|
22
|
+
const_get(namespace.insert(-2, "ActiveRecord".freeze).join("::".freeze))
|
|
21
23
|
elsif Loaded::Sequel && model_class < ::Sequel::Model
|
|
22
|
-
|
|
24
|
+
require_backend("sequel", namespace.last.underscore)
|
|
25
|
+
const_get(namespace.insert(-2, "Sequel".freeze).join("::".freeze))
|
|
23
26
|
else
|
|
24
|
-
raise ArgumentError, "#{
|
|
27
|
+
raise ArgumentError, "#{namespace.last} backend can only be used by ActiveRecord or Sequel models".freeze
|
|
25
28
|
end
|
|
26
29
|
end
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def require_backend(orm, backend)
|
|
34
|
+
begin
|
|
35
|
+
orm_backend = "mobility/backends/#{orm}/#{backend}"
|
|
36
|
+
require orm_backend
|
|
37
|
+
rescue LoadError => e
|
|
38
|
+
raise unless e.message =~ /#{orm_backend}/
|
|
39
|
+
end
|
|
30
40
|
end
|
|
31
41
|
end
|
|
32
42
|
end
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
|
@@ -13,15 +13,15 @@ Resets backend cache when reset events occur.
|
|
|
13
13
|
|
|
14
14
|
=end
|
|
15
15
|
class BackendResetter < Module
|
|
16
|
-
# @param [Array<String>] attributes
|
|
16
|
+
# @param [Array<String>] attribute_names Names of attributes whose backends should be reset
|
|
17
17
|
# @yield Backend to reset as context for block
|
|
18
18
|
# @raise [ArgumentError] if no block is provided.
|
|
19
|
-
def initialize(
|
|
19
|
+
def initialize(attribute_names, &block)
|
|
20
20
|
raise ArgumentError, "block required" unless block_given?
|
|
21
21
|
@model_reset_method = Proc.new do
|
|
22
|
-
|
|
23
|
-
if @mobility_backends && @mobility_backends[
|
|
24
|
-
@mobility_backends[
|
|
22
|
+
attribute_names.each do |name|
|
|
23
|
+
if @mobility_backends && @mobility_backends[name]
|
|
24
|
+
@mobility_backends[name].instance_eval(&block)
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
end
|
|
@@ -31,10 +31,13 @@ Resets backend cache when reset events occur.
|
|
|
31
31
|
# @param [Class] model_class Class of model to which backend resetter will be applied
|
|
32
32
|
def self.for(model_class)
|
|
33
33
|
if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
|
|
34
|
+
require "mobility/active_record/backend_resetter"
|
|
34
35
|
ActiveRecord::BackendResetter
|
|
35
36
|
elsif Loaded::ActiveRecord && model_class.ancestors.include?(::ActiveModel::Dirty)
|
|
37
|
+
require "mobility/active_model/backend_resetter"
|
|
36
38
|
ActiveModel::BackendResetter
|
|
37
39
|
elsif Loaded::Sequel && model_class < ::Sequel::Model
|
|
40
|
+
require "mobility/sequel/backend_resetter"
|
|
38
41
|
Sequel::BackendResetter
|
|
39
42
|
else
|
|
40
43
|
self
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Mobility
|
|
2
|
+
module Backends
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
def setup_query_methods(query_methods)
|
|
5
|
+
setup do |attributes, options|
|
|
6
|
+
extend(Module.new do
|
|
7
|
+
define_method ::Mobility.query_method do
|
|
8
|
+
super().extending(query_methods.new(attributes, options))
|
|
9
|
+
end
|
|
10
|
+
end)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.included(backend_class)
|
|
15
|
+
backend_class.include(Backend)
|
|
16
|
+
backend_class.extend(self)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
require "mobility/backends/active_record"
|
|
2
|
+
require "mobility/backends/column"
|
|
3
|
+
|
|
1
4
|
module Mobility
|
|
2
|
-
module
|
|
5
|
+
module Backends
|
|
3
6
|
=begin
|
|
4
7
|
|
|
5
|
-
Implements the {Mobility::
|
|
8
|
+
Implements the {Mobility::Backends::Column} backend for ActiveRecord models.
|
|
6
9
|
|
|
7
10
|
You can use the +mobility:translations+ generator to create a migration adding
|
|
8
11
|
translatable columns to the model table with:
|
|
@@ -13,9 +16,6 @@ The generated migration will add columns +title_<locale>+ for every locale in
|
|
|
13
16
|
+I18n.available_locales+. (The generator can be run again to add new attributes
|
|
14
17
|
or locales.)
|
|
15
18
|
|
|
16
|
-
@note This backend disables the +locale_accessors+ option, which would
|
|
17
|
-
otherwise interfere with column methods.
|
|
18
|
-
|
|
19
19
|
@example
|
|
20
20
|
class Post < ActiveRecord::Base
|
|
21
21
|
translates :title, backend: :column
|
|
@@ -32,27 +32,39 @@ or locales.)
|
|
|
32
32
|
include ActiveRecord
|
|
33
33
|
include Column
|
|
34
34
|
|
|
35
|
-
require 'mobility/
|
|
35
|
+
require 'mobility/backends/active_record/column/query_methods'
|
|
36
36
|
|
|
37
37
|
# @!group Backend Accessors
|
|
38
38
|
# @!macro backend_reader
|
|
39
|
-
def read(locale,
|
|
39
|
+
def read(locale, _ = {})
|
|
40
40
|
model.read_attribute(column(locale))
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
# @!group Backend Accessors
|
|
44
43
|
# @!macro backend_writer
|
|
45
|
-
def write(locale, value,
|
|
44
|
+
def write(locale, value, _ = {})
|
|
46
45
|
model.send(:write_attribute, column(locale), value)
|
|
47
46
|
end
|
|
47
|
+
# @!endgroup
|
|
48
48
|
|
|
49
|
-
# @!
|
|
50
|
-
def
|
|
51
|
-
|
|
49
|
+
# @!macro backend_iterator
|
|
50
|
+
def each_locale
|
|
51
|
+
available_locales.each { |l| yield(l) if present?(l) }
|
|
52
52
|
end
|
|
53
|
-
# @!endgroup
|
|
54
53
|
|
|
55
54
|
setup_query_methods(QueryMethods)
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def available_locales
|
|
59
|
+
@available_locales ||= get_column_locales
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def get_column_locales
|
|
63
|
+
column_name_regex = /\A#{attribute}_([a-z]{2}(_[a-z]{2})?)\z/.freeze
|
|
64
|
+
model.class.columns.map do |c|
|
|
65
|
+
(match = c.name.match(column_name_regex)) && match[1].to_sym
|
|
66
|
+
end.compact
|
|
67
|
+
end
|
|
56
68
|
end
|
|
57
69
|
end
|
|
58
70
|
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
require "mobility/backends/active_record/query_methods"
|
|
2
|
+
|
|
1
3
|
module Mobility
|
|
2
|
-
module
|
|
3
|
-
class ActiveRecord::Column::QueryMethods <
|
|
4
|
-
def initialize(attributes,
|
|
4
|
+
module Backends
|
|
5
|
+
class ActiveRecord::Column::QueryMethods < ActiveRecord::QueryMethods
|
|
6
|
+
def initialize(attributes, _)
|
|
5
7
|
super
|
|
6
8
|
attributes_extractor = @attributes_extractor
|
|
7
9
|
@opts_converter = opts_converter = lambda do |opts|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'mobility/backends/active_record/pg_hash'
|
|
2
|
+
|
|
3
|
+
module Mobility
|
|
4
|
+
module Backends
|
|
5
|
+
=begin
|
|
6
|
+
|
|
7
|
+
Implements the {Mobility::Backends::Hstore} backend for ActiveRecord models.
|
|
8
|
+
|
|
9
|
+
@see Mobility::Backends::ActiveRecord::HashValued
|
|
10
|
+
|
|
11
|
+
=end
|
|
12
|
+
class ActiveRecord::Hstore < ActiveRecord::PgHash
|
|
13
|
+
require 'mobility/backends/active_record/hstore/query_methods'
|
|
14
|
+
|
|
15
|
+
# @!group Backend Accessors
|
|
16
|
+
# @!macro backend_reader
|
|
17
|
+
# @!method read(locale, **options)
|
|
18
|
+
|
|
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
|
|
25
|
+
|
|
26
|
+
setup_query_methods(QueryMethods)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
require 'mobility/
|
|
1
|
+
require 'mobility/backends/active_record/pg_hash'
|
|
2
2
|
|
|
3
3
|
module Mobility
|
|
4
|
-
module
|
|
4
|
+
module Backends
|
|
5
5
|
=begin
|
|
6
6
|
|
|
7
|
-
Implements the {Mobility::
|
|
7
|
+
Implements the {Mobility::Backends::Jsonb} backend for ActiveRecord models.
|
|
8
8
|
|
|
9
|
-
@see Mobility::
|
|
9
|
+
@see Mobility::Backends::ActiveRecord::HashValued
|
|
10
10
|
|
|
11
11
|
=end
|
|
12
|
-
class ActiveRecord::Jsonb < ActiveRecord::
|
|
13
|
-
require 'mobility/
|
|
12
|
+
class ActiveRecord::Jsonb < ActiveRecord::PgHash
|
|
13
|
+
require 'mobility/backends/active_record/jsonb/query_methods'
|
|
14
14
|
|
|
15
15
|
# @!group Backend Accessors
|
|
16
16
|
#
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
require "mobility/backends/active_record/query_methods"
|
|
2
|
+
|
|
1
3
|
module Mobility
|
|
2
|
-
module
|
|
4
|
+
module Backends
|
|
3
5
|
class ActiveRecord::Jsonb::QueryMethods < ActiveRecord::QueryMethods
|
|
4
|
-
def initialize(attributes,
|
|
6
|
+
def initialize(attributes, _)
|
|
5
7
|
super
|
|
6
8
|
attributes_extractor = @attributes_extractor
|
|
7
9
|
|