mobility 0.1.18 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/Gemfile +11 -8
  4. data/Gemfile.lock +19 -19
  5. data/README.md +4 -4
  6. data/lib/generators/rails/mobility/active_record_migration_compatibility.rb +14 -0
  7. data/lib/generators/rails/mobility/backend_generators/base.rb +1 -0
  8. data/lib/generators/rails/mobility/generators.rb +1 -0
  9. data/lib/generators/rails/mobility/install_generator.rb +1 -0
  10. data/lib/generators/rails/mobility/templates/column_translations.rb +1 -1
  11. data/lib/generators/rails/mobility/templates/create_string_translations.rb +1 -1
  12. data/lib/generators/rails/mobility/templates/create_text_translations.rb +1 -1
  13. data/lib/generators/rails/mobility/templates/table_migration.rb +2 -2
  14. data/lib/generators/rails/mobility/templates/table_translations.rb +3 -3
  15. data/lib/mobility.rb +7 -9
  16. data/lib/mobility/active_record/attribute_methods.rb +5 -3
  17. data/lib/mobility/active_record/backend_resetter.rb +4 -6
  18. data/lib/mobility/backend/active_record/dirty.rb +15 -6
  19. data/lib/mobility/backend/active_record/hstore/query_methods.rb +1 -1
  20. data/lib/mobility/backend/active_record/jsonb/query_methods.rb +1 -1
  21. data/lib/mobility/backend/sequel/column/query_methods.rb +9 -5
  22. data/lib/mobility/backend/sequel/hstore/query_methods.rb +5 -23
  23. data/lib/mobility/backend/sequel/jsonb/query_methods.rb +5 -23
  24. data/lib/mobility/backend/sequel/key_value/query_methods.rb +12 -12
  25. data/lib/mobility/backend/sequel/postgres_query_methods.rb +42 -0
  26. data/lib/mobility/backend/sequel/table/query_methods.rb +10 -8
  27. data/lib/mobility/version.rb +1 -1
  28. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 608d78d453d3a4a8c70d081851ad41b7469ea359
4
- data.tar.gz: 25a56ec92b1f17a64b0f7ff2305709ac461a0b21
3
+ metadata.gz: cfaed87c8d2aa5d4c794e315b8b4080f94101f9a
4
+ data.tar.gz: d5fd629b130d538e419ac31522b9f6441d9d85b4
5
5
  SHA512:
6
- metadata.gz: 0ee3ca1d2c69def85e662c064809c611cd1f93f4ec512ba85fdd011b4349de739175421cd6a1658060691e8ff8122c808056c254c24bfba2c7bfc803fd1fad7e
7
- data.tar.gz: 4d32541308d245ea15bc1a2867876448e5fc637c30a794127807c5f63789c21f0f221160a54264777866a07416c7e09ff6d1d5df4e223ad3de74459fd54d788b
6
+ metadata.gz: 05b16517848c01ddc40f836ca085883b986dcff714ed74787e920e11e8bd376eafa7bcaf26ef74957ed0052c9c74d11b9acdf03ce68c4512afbc0c284a74e8bd
7
+ data.tar.gz: d593ee3f8c8bdc1b7a8f5ebc2928677dd89d4a146b0463839cba92e4d4360ed246317b67593e7c7a83c0f96840ef83a499e3e8b6280a1b7dbdbf3e3706d0189b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## 0.1
4
4
 
5
+ ### 0.1.19 (July 16, 2017)
6
+ * Partial support for AR 4.2 ([#46](https://github.com/shioyama/mobility/pull/46))
7
+ * Fix issues with Sequel >= 4.46.0 ([#47](https://github.com/shioyama/mobility/pull/47))
8
+ * Include anonymous modules instead of defining methods directly on class
9
+ ([049a5f](https://github.com/shioyama/mobility/commit/049a5f90fd898d82984d2fe1af1646fda48ad142),
10
+ [d8fe42](https://github.com/shioyama/mobility/commit/d8fe42f81211640125e6a50bf681d45dbaa71c40),
11
+ [9cc3d0](https://github.com/shioyama/mobility/commit/9cc3d0e8c3f813c15213848f305e363c4eec6b8e))
12
+
5
13
  ### 0.1.18 (June 21, 2017)
6
14
  * Fix deprecation warnings when using ActiveRecord 5.1
7
15
  ([#44](https://github.com/shioyama/mobility/pull/44))
data/Gemfile CHANGED
@@ -5,17 +5,20 @@ gemspec
5
5
 
6
6
  group :development, :test do
7
7
  if ENV['ORM'] == 'active_record'
8
- if ENV['RAILS_VERSION'] == '5.1'
9
- gem 'activerecord', '>= 5.1', '< 5.2'
10
- else
8
+ if ENV['RAILS_VERSION'] == '5.0'
11
9
  gem 'activerecord', '>= 5.0', '< 5.1'
10
+ elsif ENV['RAILS_VERSION'] == '4.2'
11
+ gem 'activerecord', '>= 4.2.6', '< 5.0'
12
+ else
13
+ gem 'activerecord', '>= 5.1', '< 5.2'
12
14
  end
13
15
  gem "generator_spec", '~> 0.9.4'
14
- end
15
-
16
- if ENV['ORM'] == 'sequel'
17
- # some interal API changes are breaking specs, limit to 4.45.x for now
18
- gem 'sequel', '>= 4.41.0', '< 4.46.0'
16
+ elsif ENV['ORM'] == 'sequel'
17
+ if ENV['SEQUEL_VERSION'] == '4.41'
18
+ gem 'sequel', '>= 4.41.0', '< 4.46.0'
19
+ else
20
+ gem 'sequel', '>= 4.46.0'
21
+ end
19
22
  end
20
23
 
21
24
  platforms :ruby do
data/Gemfile.lock CHANGED
@@ -1,33 +1,33 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobility (0.1.17)
4
+ mobility (0.2.0.alpha)
5
5
  i18n (>= 0.6.10, < 0.9)
6
6
  request_store (~> 1.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionpack (5.1.1)
12
- actionview (= 5.1.1)
13
- activesupport (= 5.1.1)
11
+ actionpack (5.1.2)
12
+ actionview (= 5.1.2)
13
+ activesupport (= 5.1.2)
14
14
  rack (~> 2.0)
15
15
  rack-test (~> 0.6.3)
16
16
  rails-dom-testing (~> 2.0)
17
17
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
- actionview (5.1.1)
19
- activesupport (= 5.1.1)
18
+ actionview (5.1.2)
19
+ activesupport (= 5.1.2)
20
20
  builder (~> 3.1)
21
21
  erubi (~> 1.4)
22
22
  rails-dom-testing (~> 2.0)
23
23
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
- activemodel (5.1.1)
25
- activesupport (= 5.1.1)
26
- activerecord (5.1.1)
27
- activemodel (= 5.1.1)
28
- activesupport (= 5.1.1)
24
+ activemodel (5.1.2)
25
+ activesupport (= 5.1.2)
26
+ activerecord (5.1.2)
27
+ activemodel (= 5.1.2)
28
+ activesupport (= 5.1.2)
29
29
  arel (~> 8.0)
30
- activesupport (5.1.1)
30
+ activesupport (5.1.2)
31
31
  concurrent-ruby (~> 1.0, >= 1.0.2)
32
32
  i18n (~> 0.7)
33
33
  minitest (~> 5.1)
@@ -39,7 +39,7 @@ GEM
39
39
  concurrent-ruby (1.0.5)
40
40
  database_cleaner (1.6.1)
41
41
  diff-lcs (1.3)
42
- erubi (1.6.0)
42
+ erubi (1.6.1)
43
43
  ffi (1.9.18)
44
44
  formatador (0.2.5)
45
45
  generator_spec (0.9.4)
@@ -59,7 +59,7 @@ GEM
59
59
  guard (~> 2.1)
60
60
  guard-compat (~> 1.1)
61
61
  rspec (>= 2.99.0, < 4.0)
62
- i18n (0.8.4)
62
+ i18n (0.8.6)
63
63
  listen (3.1.5)
64
64
  rb-fsevent (~> 0.9, >= 0.9.4)
65
65
  rb-inotify (~> 0.9, >= 0.9.7)
@@ -93,14 +93,14 @@ GEM
93
93
  nokogiri (>= 1.6)
94
94
  rails-html-sanitizer (1.0.3)
95
95
  loofah (~> 2.0)
96
- railties (5.1.1)
97
- actionpack (= 5.1.1)
98
- activesupport (= 5.1.1)
96
+ railties (5.1.2)
97
+ actionpack (= 5.1.2)
98
+ activesupport (= 5.1.2)
99
99
  method_source
100
100
  rake (>= 0.8.7)
101
101
  thor (>= 0.18.1, < 2.0)
102
102
  rake (10.5.0)
103
- rb-fsevent (0.9.8)
103
+ rb-fsevent (0.10.2)
104
104
  rb-inotify (0.9.10)
105
105
  ffi (>= 0.5.0, < 2)
106
106
  request_store (1.3.2)
@@ -146,4 +146,4 @@ DEPENDENCIES
146
146
  yard (~> 0.9.0)
147
147
 
148
148
  BUNDLED WITH
149
- 1.15.0
149
+ 1.15.1
data/README.md CHANGED
@@ -47,7 +47,7 @@ Installation
47
47
  Add this line to your application's Gemfile:
48
48
 
49
49
  ```ruby
50
- gem 'mobility', '~> 0.1.18'
50
+ gem 'mobility', '~> 0.1.19'
51
51
  ```
52
52
 
53
53
  To translate attributes on a model, include (or extend) `Mobility`, then call
@@ -58,9 +58,9 @@ To translate attributes on a model, include (or extend) `Mobility`, then call
58
58
  Requirements:
59
59
  - ActiveRecord >= 5.0
60
60
 
61
- (Support for some backends is also supported with ActiveRecord/Rails 4.2, see
62
- the [active_record-4.2
63
- branch](https://github.com/shioyama/mobility/tree/active_record_4.2).)
61
+ (Support for most backends and features is also supported with
62
+ ActiveRecord/Rails 4.2, but there are some tests still failing. To see exactly
63
+ what might not work, check pending specs in Rails 4.2 Travis builds.)
64
64
 
65
65
  If using Mobility in a Rails project, you can run the generator to create an
66
66
  initializer and a migration to create shared translation tables for the
@@ -0,0 +1,14 @@
1
+ require "rails/generators/active_record"
2
+ require "active_record/migration"
3
+
4
+ module Mobility
5
+ module ActiveRecordMigrationCompatibility
6
+ def activerecord_migration_class
7
+ if ::ActiveRecord::Migration.respond_to?(:current_version)
8
+ "ActiveRecord::Migration[#{::ActiveRecord::Migration.current_version}]"
9
+ else
10
+ "ActiveRecord::Migration"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -6,6 +6,7 @@ module Mobility
6
6
  class Base < ::Rails::Generators::NamedBase
7
7
  argument :attributes, type: :array, default: []
8
8
  include ::ActiveRecord::Generators::Migration
9
+ include ::Mobility::ActiveRecordMigrationCompatibility
9
10
 
10
11
  def create_migration_file
11
12
  if self.class.migration_exists?(migration_dir, migration_file)
@@ -1,5 +1,6 @@
1
1
  require "rails/generators"
2
2
 
3
+ require_relative "./active_record_migration_compatibility"
3
4
  require_relative "./install_generator"
4
5
  require_relative "./translations_generator"
5
6
  require_relative "./backend_generators/base"
@@ -4,6 +4,7 @@ require "rails/generators/active_record"
4
4
  module Mobility
5
5
  class InstallGenerator < ::Rails::Generators::Base
6
6
  include ::Rails::Generators::Migration
7
+ include ::Mobility::ActiveRecordMigrationCompatibility
7
8
 
8
9
  desc "Generates migrations to add translations tables."
9
10
 
@@ -1,4 +1,4 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
2
  def change
3
3
  <% attributes.each do |attribute| -%>
4
4
  <% I18n.available_locales.each do |locale| -%>
@@ -1,4 +1,4 @@
1
- class CreateStringTranslations < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
1
+ class CreateStringTranslations < <%= activerecord_migration_class %>
2
2
 
3
3
  def change
4
4
  create_table :mobility_string_translations do |t|
@@ -1,4 +1,4 @@
1
- class CreateTextTranslations < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
1
+ class CreateTextTranslations < <%= activerecord_migration_class %>
2
2
 
3
3
  def change
4
4
  create_table :mobility_text_translations do |t|
@@ -1,9 +1,9 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
2
  def change
3
3
  <% attributes.each do |attribute| -%>
4
4
  <%- if attribute.reference? -%>
5
5
  add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
6
- <%- elsif attribute.token? -%>
6
+ <%- elsif attribute.respond_to?(:token?) && attribute.token? -%>
7
7
  add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
8
8
  add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
9
9
  <%- else -%>
@@ -1,10 +1,10 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
2
  def change
3
- create_table :<%= table_name %><%= primary_key_type %> do |t|
3
+ create_table :<%= table_name %><%= primary_key_type if respond_to?(:primary_key_type) %> do |t|
4
4
 
5
5
  # Translated attribute(s)
6
6
  <% attributes.each do |attribute| -%>
7
- <% if attribute.token? -%>
7
+ <% if attribute.respond_to?(:token?) && attribute.token? -%>
8
8
  t.string :<%= attribute.name %><%= attribute.inject_options %>
9
9
  <% else -%>
10
10
  t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
data/lib/mobility.rb CHANGED
@@ -48,7 +48,7 @@ module Mobility
48
48
 
49
49
  begin
50
50
  require "active_record"
51
- raise VersionNotSupportedError, "Mobility is only compatible with ActiveRecord 5.0 and greater" if ::ActiveRecord::VERSION::MAJOR < 5
51
+ raise VersionNotSupportedError, "Mobility is only compatible with ActiveRecord 4.2 and greater" if ::ActiveRecord::VERSION::STRING < "4.2"
52
52
  autoload :ActiveModel, "mobility/active_model"
53
53
  autoload :ActiveRecord, "mobility/active_record"
54
54
  Loaded::ActiveRecord = true
@@ -79,7 +79,7 @@ module Mobility
79
79
  class << self
80
80
  def extended(model_class)
81
81
  return if model_class.respond_to? :mobility_accessor
82
- model_class.class_eval do
82
+ mod = Module.new do
83
83
  # Fetch backend for an attribute
84
84
  # @param [String] attribute Attribute
85
85
  def mobility_backend_for(attribute)
@@ -90,14 +90,12 @@ module Mobility
90
90
  @mobility_backends = nil
91
91
  super
92
92
  end
93
+ end
94
+ model_class.include(mod)
93
95
 
94
- class << self
95
- include ClassMethods
96
-
97
- if translates = Mobility.config.accessor_method
98
- alias_method translates, :mobility_accessor
99
- end
100
- end
96
+ model_class.extend(ClassMethods)
97
+ if translates = Mobility.config.accessor_method
98
+ model_class.singleton_class.send(:alias_method, translates, :mobility_accessor)
101
99
  end
102
100
 
103
101
  if Loaded::ActiveRecord
@@ -13,13 +13,15 @@ ancestors.
13
13
  # @return [Array<String>] Model attributes
14
14
  # @!method attributes
15
15
  def self.included(model)
16
- model.class_eval do
17
- alias_method :untranslated_attributes, :attributes
18
-
16
+ attributes_method = Module.new do
19
17
  def attributes
20
18
  super.merge(translated_attributes)
21
19
  end
22
20
  end
21
+ model.class_eval do
22
+ alias_method :untranslated_attributes, :attributes
23
+ include attributes_method
24
+ end
23
25
  end
24
26
 
25
27
  # Translated attributes defined on model.
@@ -12,14 +12,12 @@ Backend resetter for ActiveRecord models. Adds hook on +reload+ event to
12
12
  def included(model_class)
13
13
  model_reset_method = @model_reset_method
14
14
 
15
- model_class.class_eval do
16
- mod = Module.new do
17
- define_method :reload do |*args|
18
- super(*args).tap { instance_eval(&model_reset_method) }
19
- end
15
+ mod = Module.new do
16
+ define_method :reload do |*args|
17
+ super(*args).tap { instance_eval(&model_reset_method) }
20
18
  end
21
- include mod
22
19
  end
20
+ model_class.include mod
23
21
  end
24
22
  end
25
23
  end
@@ -1,5 +1,11 @@
1
1
  module Mobility
2
2
  module Backend
3
+ =begin
4
+
5
+ Dirty tracking for AR models. See {Mobility::Backend::ActiveModel::Dirty} for
6
+ details on usage.
7
+
8
+ =end
3
9
  module ActiveRecord::Dirty
4
10
  include ActiveModel::Dirty
5
11
 
@@ -9,22 +15,24 @@ module Mobility
9
15
  backend_class.extend(ClassMethods)
10
16
  end
11
17
 
18
+ # Adds hook after {Backend::Setup#setup_model} to patch AR so that it
19
+ # handles changes to translated attributes just like normal attributes.
12
20
  module ClassMethods
21
+ # (see Mobility::Backend::Setup#setup_model)
13
22
  def setup_model(model_class, attributes, **options)
14
23
  super
15
24
 
16
25
  method_name_regex = /\A(#{attributes.join('|'.freeze)})_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze
17
- mod = Module.new do
26
+ has_attribute = Module.new do
18
27
  define_method :has_attribute? do |attr_name|
19
28
  super(attr_name) || !!method_name_regex.match(attr_name)
20
29
  end
21
30
  end
31
+ model_class.extend has_attribute
22
32
 
23
- model_class.class_eval do
24
- extend mod
25
-
26
- method_name = ::ActiveRecord::VERSION::STRING < '5.1' ? :changes_applied : :changes_internally_applied
27
- define_method method_name do
33
+ changes_applied_method = ::ActiveRecord::VERSION::STRING < '5.1' ? :changes_applied : :changes_internally_applied
34
+ mod = Module.new do
35
+ define_method changes_applied_method do
28
36
  @previously_changed = changes
29
37
  super()
30
38
  end
@@ -38,6 +46,7 @@ module Mobility
38
46
  (@previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new).merge(super)
39
47
  end
40
48
  end
49
+ model_class.include mod
41
50
  end
42
51
  end
43
52
  end
@@ -25,7 +25,7 @@ module Mobility
25
25
  ops ? ops.and(op) : op
26
26
  }
27
27
 
28
- opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query)
28
+ opts.empty? ? super(i18n_query) : super(opts, *rest).where(i18n_query)
29
29
  else
30
30
  super(opts, *rest)
31
31
  end
@@ -26,7 +26,7 @@ module Mobility
26
26
  ops ? ops.and(op) : op
27
27
  }
28
28
 
29
- opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query)
29
+ opts.empty? ? super(i18n_query) : super(opts, *rest).where(i18n_query)
30
30
  else
31
31
  super(opts, *rest)
32
32
  end
@@ -5,12 +5,16 @@ module Mobility
5
5
  super
6
6
  attributes_extractor = @attributes_extractor
7
7
 
8
- define_method :_filter_or_exclude do |invert, clause, cond, &block|
9
- if keys = attributes_extractor.call(cond)
10
- cond = cond.dup
11
- keys.each { |attr| cond[Column.column_name_for(attr)] = cond.delete(attr) }
8
+ %w[exclude or where].each do |method_name|
9
+ define_method method_name do |*conds, &block|
10
+ if keys = attributes_extractor.call(conds.first)
11
+ cond = conds.first.dup
12
+ keys.each { |attr| cond[Column.column_name_for(attr)] = cond.delete(attr) }
13
+ super(cond, &block)
14
+ else
15
+ super(*conds, &block)
16
+ end
12
17
  end
13
- super(invert, clause, cond, &block)
14
18
  end
15
19
 
16
20
  attributes.each do |attribute|
@@ -1,34 +1,16 @@
1
+ require 'mobility/backend/sequel/postgres_query_methods'
2
+
1
3
  Sequel.extension :pg_hstore, :pg_hstore_ops
2
4
 
3
5
  module Mobility
4
6
  module Backend
5
7
  class Sequel::Hstore::QueryMethods < Sequel::QueryMethods
8
+ include PostgresQueryMethods
9
+
6
10
  def initialize(attributes, **)
7
11
  super
8
- attributes_extractor = @attributes_extractor
9
-
10
- define_method :_filter_or_exclude do |invert, clause, *conds, &block|
11
- if (clause == :where) && i18n_keys = attributes_extractor.call(conds.first)
12
- locale = Mobility.locale.to_s
13
- table_name = model.table_name
14
- cond = conds.first
15
12
 
16
- i18n_query = i18n_keys.inject(::Sequel.expr(!invert)) do |expr, attr|
17
- value = cond.delete(attr)
18
- attr_hstore = ::Sequel.hstore_op(attr)
19
- contains_value = attr_hstore.contains({ locale => value.to_s })
20
- has_key = attr_hstore.has_key?(locale)
21
- if invert
22
- expr.|(has_key & ~contains_value)
23
- else
24
- expr.&(value.nil? ? ~has_key : contains_value)
25
- end
26
- end
27
- super(invert, clause, *conds, &block).where(i18n_query)
28
- else
29
- super(invert, clause, *conds, &block)
30
- end
31
- end
13
+ define_query_methods("hstore")
32
14
 
33
15
  attributes.each do |attribute|
34
16
  define_method :"first_by_#{attribute}" do |value|
@@ -1,34 +1,16 @@
1
+ require 'mobility/backend/sequel/postgres_query_methods'
2
+
1
3
  Sequel.extension :pg_json, :pg_json_ops
2
4
 
3
5
  module Mobility
4
6
  module Backend
5
7
  class Sequel::Jsonb::QueryMethods < Sequel::QueryMethods
8
+ include PostgresQueryMethods
9
+
6
10
  def initialize(attributes, **)
7
11
  super
8
- attributes_extractor = @attributes_extractor
9
-
10
- define_method :_filter_or_exclude do |invert, clause, *conds, &block|
11
- if (clause == :where) && i18n_keys = attributes_extractor.call(conds.first)
12
- locale = Mobility.locale.to_s
13
- table_name = model.table_name
14
- cond = conds.first
15
12
 
16
- i18n_query = i18n_keys.inject(::Sequel.expr(!invert)) do |expr, attr|
17
- value = cond.delete(attr)
18
- attr_jsonb = ::Sequel.pg_jsonb_op(attr)
19
- contains_value = attr_jsonb.contains({ locale => value })
20
- has_key = attr_jsonb.has_key?(locale)
21
- if invert
22
- expr.|(has_key & ~contains_value)
23
- else
24
- expr.&(value.nil? ? ~has_key : contains_value)
25
- end
26
- end
27
- super(invert, clause, *conds, &block).where(i18n_query)
28
- else
29
- super(invert, clause, *conds, &block)
30
- end
31
- end
13
+ define_query_methods("pg_jsonb")
32
14
 
33
15
  attributes.each do |attribute|
34
16
  define_method :"first_by_#{attribute}" do |value|
@@ -39,20 +39,20 @@ module Mobility
39
39
  #
40
40
  attributes_extractor = @attributes_extractor
41
41
 
42
- define_method :_filter_or_exclude do |invert, clause, *cond, &block|
43
- if i18n_keys = attributes_extractor.call(cond.first)
44
- cond = cond.first.dup
45
- i18n_nulls = i18n_keys.select { |key| cond[key].nil? }
46
- i18n_keys.each { |attr| cond[::Sequel[:"#{attr}_#{association_name}"][:value]] = cond.delete(attr) }
47
- super(invert, clause, cond, &block).
48
- send("join_#{association_name}", *(i18n_keys - i18n_nulls)).
49
- send("join_#{association_name}", *i18n_nulls, outer_join: true)
50
- else
51
- super(invert, clause, *cond, &block)
42
+ %w[exclude or where].each do |method_name|
43
+ define_method method_name do |*conds, &block|
44
+ if i18n_keys = attributes_extractor.call(conds.first)
45
+ cond = conds.first.dup
46
+ i18n_nulls = i18n_keys.select { |key| cond[key].nil? }
47
+ i18n_keys.each { |attr| cond[::Sequel[:"#{attr}_#{association_name}"][:value]] = cond.delete(attr) }
48
+ super(cond, &block).
49
+ send("join_#{association_name}", *(i18n_keys - i18n_nulls), outer_join: method_name == "or").
50
+ send("join_#{association_name}", *i18n_nulls, outer_join: true)
51
+ else
52
+ super(*conds, &block)
53
+ end
52
54
  end
53
55
  end
54
- private :_filter_or_exclude
55
-
56
56
  end
57
57
  end
58
58
  end
@@ -0,0 +1,42 @@
1
+
2
+ module Mobility
3
+ module Backend
4
+ module PostgresQueryMethods
5
+ private
6
+
7
+ def define_query_methods(column_type)
8
+ attributes_extractor = @attributes_extractor
9
+
10
+ %w[exclude or where].each do |method_name|
11
+ invert = method_name == "exclude"
12
+
13
+ define_method method_name do |*cond, &block|
14
+ if i18n_keys = attributes_extractor.call(cond.first)
15
+ locale = Mobility.locale.to_s
16
+ cond = cond.first
17
+
18
+ i18n_query = i18n_keys.inject(::Sequel.expr(!invert)) do |expr, attr|
19
+ value = cond.delete(attr)
20
+ op = ::Sequel.send(:"#{column_type}_op", attr)
21
+ contains_value = op.contains({ locale => value.to_s })
22
+ has_key = op.has_key?(locale)
23
+ if invert
24
+ expr.|(has_key & ~contains_value)
25
+ else
26
+ expr.&(value.nil? ? ~has_key : contains_value)
27
+ end
28
+ end
29
+ if method_name == "or"
30
+ cond.empty? ? super(i18n_query, &block) : super(::Sequel.&(cond, i18n_query), &block)
31
+ else
32
+ super(cond, &block).where(i18n_query)
33
+ end
34
+ else
35
+ super(*cond, &block)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -35,14 +35,16 @@ module Mobility
35
35
  # See note in AR Table QueryMethods class about limitations of
36
36
  # query methods on translated attributes when searching on nil values.
37
37
  #
38
- define_method :_filter_or_exclude do |invert, clause, *cond, &block|
39
- if i18n_keys = attributes_extractor.call(cond.first)
40
- cond = cond.first.dup
41
- outer_join = i18n_keys.all? { |key| cond[key].nil? }
42
- i18n_keys.each { |attr| cond[::Sequel[translation_class.table_name][attr]] = cond.delete(attr) }
43
- super(invert, clause, cond, &block).send("join_#{association_name}", outer_join: outer_join)
44
- else
45
- super(invert, clause, *cond, &block)
38
+ %w[exclude or where].each do |method_name|
39
+ define_method method_name do |*conds, &block|
40
+ if i18n_keys = attributes_extractor.call(conds.first)
41
+ cond = conds.first.dup
42
+ outer_join = method_name == "or" || i18n_keys.all? { |key| cond[key].nil? }
43
+ i18n_keys.each { |attr| cond[::Sequel[translation_class.table_name][attr]] = cond.delete(attr) }
44
+ super(cond, &block).send("join_#{association_name}", outer_join: outer_join)
45
+ else
46
+ super(*conds, &block)
47
+ end
46
48
  end
47
49
  end
48
50
  end
@@ -1,3 +1,3 @@
1
1
  module Mobility
2
- VERSION = "0.1.18"
2
+ VERSION = "0.1.19"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -135,6 +135,7 @@ files:
135
135
  - LICENSE.txt
136
136
  - README.md
137
137
  - Rakefile
138
+ - lib/generators/rails/mobility/active_record_migration_compatibility.rb
138
139
  - lib/generators/rails/mobility/backend_generators/base.rb
139
140
  - lib/generators/rails/mobility/backend_generators/column_backend.rb
140
141
  - lib/generators/rails/mobility/backend_generators/table_backend.rb
@@ -199,6 +200,7 @@ files:
199
200
  - lib/mobility/backend/sequel/jsonb/query_methods.rb
200
201
  - lib/mobility/backend/sequel/key_value.rb
201
202
  - lib/mobility/backend/sequel/key_value/query_methods.rb
203
+ - lib/mobility/backend/sequel/postgres_query_methods.rb
202
204
  - lib/mobility/backend/sequel/query_methods.rb
203
205
  - lib/mobility/backend/sequel/serialized.rb
204
206
  - lib/mobility/backend/sequel/serialized/query_methods.rb
@@ -245,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
245
247
  version: '0'
246
248
  requirements: []
247
249
  rubyforge_project:
248
- rubygems_version: 2.6.12
250
+ rubygems_version: 2.6.11
249
251
  signing_key:
250
252
  specification_version: 4
251
253
  summary: Pluggable Ruby translation framework