diff --git a/lib/mobility/backend/active_record/hstore/query_methods.rb b/lib/mobility/backend/active_record/hstore/query_methods.rb index 6d210a580..38ee8fab8 100644 --- a/lib/mobility/backend/active_record/hstore/query_methods.rb +++ b/lib/mobility/backend/active_record/hstore/query_methods.rb @@ -7,19 +7,25 @@ def initialize(attributes, **options) define_method :where! do |opts, *rest| if i18n_keys = attributes_extractor.call(opts) - locale = Mobility.locale + m = arel_table + locale = Arel::Nodes.build_quoted(Mobility.locale.to_s) opts = opts.with_indifferent_access + infix = Arel::Nodes::InfixOperation - result = i18n_keys.inject(all) do |scope, attr| + i18n_query = i18n_keys.inject(nil) { |ops, attr| + column = m[attr.to_sym] value = opts.delete(attr) - if value.nil? - scope.where.not("#{table_name}.#{attr} ? '#{locale}'") - else - scope.where!("#{table_name}.#{attr} @> hstore('#{locale}', ?)", value.to_s) - end - end - result = result.where!(opts, *rest) if opts.present? - result + + op = + if value.nil? + infix.new(:'?', column, locale).not + else + infix.new(:'->', m[attr.to_sym], locale).eq(value) + end + ops ? ops.and(op) : op + } + + opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query) else super(opts, *rest) end @@ -38,7 +44,7 @@ def extended(relation) opts = opts.with_indifferent_access infix = Arel::Nodes::InfixOperation - query = i18n_keys.inject(nil) { |ops, attr| + i18n_query = i18n_keys.inject(nil) { |ops, attr| column = m[attr.to_sym] value = Arel::Nodes.build_quoted(opts.delete(attr).to_s) has_key = infix.new(:'?', column, locale) @@ -47,7 +53,7 @@ def extended(relation) ops ? ops.and(op) : op } - super(opts, *rest).where(query) + super(opts, *rest).where(i18n_query) else super(opts, *rest) end diff --git a/lib/mobility/backend/active_record/jsonb/query_methods.rb b/lib/mobility/backend/active_record/jsonb/query_methods.rb index beaff01cf..b41c2196c 100644 --- a/lib/mobility/backend/active_record/jsonb/query_methods.rb +++ b/lib/mobility/backend/active_record/jsonb/query_methods.rb @@ -7,19 +7,26 @@ def initialize(attributes, **options) define_method :where! do |opts, *rest| if i18n_keys = attributes_extractor.call(opts) - locale = Mobility.locale + m = arel_table + locale = Arel::Nodes.build_quoted(Mobility.locale.to_s) opts = opts.with_indifferent_access + infix = Arel::Nodes::InfixOperation - result = i18n_keys.inject(all) do |scope, attr| + i18n_query = i18n_keys.inject(nil) { |ops, attr| + column = m[attr.to_sym] value = opts.delete(attr) - if value.nil? - scope.where.not("#{table_name}.#{attr} ? :locale", locale: locale) - else - scope.where!("#{table_name}.#{attr} @> (?)::jsonb", { locale => value }.to_json) - end - end - result = result.where!(opts, *rest) if opts.present? - result + + op = + if value.nil? + infix.new(:'?', column, locale).not + else + predicate = Arel::Nodes.build_quoted({ Mobility.locale => value }.to_json) + infix.new(:'@>', m[attr.to_sym], predicate) + end + ops ? ops.and(op) : op + } + + opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query) else super(opts, *rest) end @@ -38,7 +45,7 @@ def extended(relation) opts = opts.with_indifferent_access infix = Arel::Nodes::InfixOperation - query = i18n_keys.inject(nil) { |ops, attr| + i18n_query = i18n_keys.inject(nil) { |ops, attr| column = m[attr.to_sym] has_key = infix.new(:'?', column, locale) predicate = Arel::Nodes.build_quoted({ Mobility.locale => opts.delete(attr) }.to_json) @@ -47,7 +54,7 @@ def extended(relation) ops ? ops.and(op) : op } - super(opts, *rest).where(query) + super(opts, *rest).where(i18n_query) else super(opts, *rest) end