Sha256: 03d34fb5e4bf4f87292b588da9bfbac5410ff66a8198f03e0959307f3e97edd3
Contents?: true
Size: 1.96 KB
Versions: 2
Compression:
Stored size: 1.96 KB
Contents
require "mobility/backends/sequel/query_methods" module Mobility module Backends class Sequel::KeyValue::QueryMethods < Sequel::QueryMethods def initialize(attributes, association_name: nil, class_name: nil, **) super define_join_method(association_name, class_name) define_query_methods(association_name) end private def define_join_method(association_name, translation_class) define_method :"join_#{association_name}" do |*attributes, **options| attributes.inject(self) do |relation, attribute| join_type = options[:outer_join] ? :left_outer : :inner relation.join_table(join_type, translation_class.table_name, { key: attribute.to_s, locale: Mobility.locale.to_s, translatable_type: model.name, translatable_id: ::Sequel[:"#{model.table_name}"][:id] }, table_alias: "#{attribute}_#{association_name}") end end end def define_query_methods(association_name) q = self %w[exclude or where].each do |method_name| define_method method_name do |*conds, &block| if i18n_keys = q.extract_attributes(conds.first) cond = conds.first.dup i18n_nulls = i18n_keys.select { |key| cond[key].nil? } i18n_keys.each { |attr| cond[::Sequel[:"#{attr}_#{association_name}"][:value]] = cond.delete(attr) } super(cond, &block). send("join_#{association_name}", *(i18n_keys - i18n_nulls), outer_join: method_name == "or"). send("join_#{association_name}", *i18n_nulls, outer_join: true) else super(*conds, &block) end end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mobility-0.4.3 | lib/mobility/backends/sequel/key_value/query_methods.rb |
mobility-0.4.2 | lib/mobility/backends/sequel/key_value/query_methods.rb |